Mercurial Hosting > luan
diff src/luan/LuanState.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/LuanState.java@b620b8e1010f |
children | fbbdd369a13a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/LuanState.java Fri Aug 26 14:36:40 2016 -0600 @@ -0,0 +1,97 @@ +package luan; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; +import luan.impl.LuanCompiler; +import luan.modules.BasicLuan; +import luan.modules.JavaLuan; + + +public final class LuanState implements DeepCloneable { + + public LuanJava java; + private Map registry; + private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); + + public LuanState() { + java = new LuanJava(); + registry = new HashMap(); + } + + private LuanState(LuanState luan) {} + + @Override public LuanState shallowClone() { + return new LuanState(this); + } + + @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) { + LuanState clone = (LuanState)dc; + clone.registry = cloner.deepClone(registry); + clone.java = (LuanJava)cloner.deepClone(java); + } + + public final Map registry() { + return registry; + } + + public void onClose(Closeable c) { + onClose.add(new WeakReference<Closeable>(c)); + } + + public void close() throws IOException { + for( Reference<Closeable> ref : onClose ) { + Closeable c = ref.get(); + if( c != null ) + c.close(); + } + onClose.clear(); + } +/* + public final Object eval(String cmd) throws LuanException { + return eval(cmd,new LuanTable()); + } + + public final Object eval(String cmd,LuanTable env) throws LuanException { + LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true); + return fn.call(this); + } +*/ + + public String toString(Object obj) throws LuanException { + if( obj instanceof LuanTable ) { + LuanTable tbl = (LuanTable)obj; + return tbl.toString(this); + } + if( obj == null ) + return "nil"; + if( obj instanceof Number ) + return Luan.toString((Number)obj); + if( obj instanceof byte[] ) + return "binary: " + Integer.toHexString(obj.hashCode()); + return obj.toString(); + } + + public Object index(Object obj,Object key) throws LuanException { + if( obj instanceof LuanTable ) { + LuanTable tbl = (LuanTable)obj; + return tbl.get(this,key); + } + if( obj != null && java.ok ) + return JavaLuan.__index(this,obj,key,false); + throw new LuanException("attempt to index a " + Luan.type(obj) + " value" ); + } + +/* + public Number checkNumber(Object obj) throws LuanException { + if( obj instanceof Number ) + return (Number)obj; + throw new LuanException( "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" ); + } +*/ +}