Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan; | |
2 | |
3 import java.io.Closeable; | |
4 import java.io.IOException; | |
5 import java.lang.ref.Reference; | |
6 import java.lang.ref.WeakReference; | |
7 import java.util.List; | |
8 import java.util.ArrayList; | |
9 import java.util.Map; | |
10 import java.util.HashMap; | |
11 import luan.impl.LuanCompiler; | |
12 import luan.modules.BasicLuan; | |
13 import luan.modules.JavaLuan; | |
14 | |
15 | |
16 public final class LuanState implements DeepCloneable { | |
17 | |
18 public LuanJava java; | |
19 private Map registry; | |
20 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); | |
21 | |
22 public LuanState() { | |
23 java = new LuanJava(); | |
24 registry = new HashMap(); | |
25 } | |
26 | |
27 private LuanState(LuanState luan) {} | |
28 | |
29 @Override public LuanState shallowClone() { | |
30 return new LuanState(this); | |
31 } | |
32 | |
33 @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) { | |
34 LuanState clone = (LuanState)dc; | |
35 clone.registry = cloner.deepClone(registry); | |
36 clone.java = (LuanJava)cloner.deepClone(java); | |
37 } | |
38 | |
39 public final Map registry() { | |
40 return registry; | |
41 } | |
42 | |
43 public void onClose(Closeable c) { | |
44 onClose.add(new WeakReference<Closeable>(c)); | |
45 } | |
46 | |
47 public void close() throws IOException { | |
48 for( Reference<Closeable> ref : onClose ) { | |
49 Closeable c = ref.get(); | |
50 if( c != null ) | |
51 c.close(); | |
52 } | |
53 onClose.clear(); | |
54 } | |
55 /* | |
56 public final Object eval(String cmd) throws LuanException { | |
57 return eval(cmd,new LuanTable()); | |
58 } | |
59 | |
60 public final Object eval(String cmd,LuanTable env) throws LuanException { | |
61 LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true); | |
62 return fn.call(this); | |
63 } | |
64 */ | |
65 | |
66 public String toString(Object obj) throws LuanException { | |
67 if( obj instanceof LuanTable ) { | |
68 LuanTable tbl = (LuanTable)obj; | |
69 return tbl.toString(this); | |
70 } | |
71 if( obj == null ) | |
72 return "nil"; | |
73 if( obj instanceof Number ) | |
74 return Luan.toString((Number)obj); | |
75 if( obj instanceof byte[] ) | |
76 return "binary: " + Integer.toHexString(obj.hashCode()); | |
77 return obj.toString(); | |
78 } | |
79 | |
80 public Object index(Object obj,Object key) throws LuanException { | |
81 if( obj instanceof LuanTable ) { | |
82 LuanTable tbl = (LuanTable)obj; | |
83 return tbl.get(this,key); | |
84 } | |
85 if( obj != null && java.ok ) | |
86 return JavaLuan.__index(this,obj,key,false); | |
87 throw new LuanException("attempt to index a " + Luan.type(obj) + " value" ); | |
88 } | |
89 | |
90 /* | |
91 public Number checkNumber(Object obj) throws LuanException { | |
92 if( obj instanceof Number ) | |
93 return (Number)obj; | |
94 throw new LuanException( "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" ); | |
95 } | |
96 */ | |
97 } |