comparison core/src/luan/LuanState.java @ 202:75750ceb45ee

add LuanState.registry git-svn-id: https://luan-java.googlecode.com/svn/trunk@203 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 04 Jul 2014 17:18:39 +0000
parents 9fb218211763
children 5ba136769034
comparison
equal deleted inserted replaced
201:27abb3746917 202:75750ceb45ee
15 15
16 public abstract class LuanState implements DeepCloneable<LuanState> { 16 public abstract class LuanState implements DeepCloneable<LuanState> {
17 17
18 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); 18 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
19 19
20 private LuanTable registry;
20 private LuanTable global; 21 private LuanTable global;
21 private LuanTable loaded;
22 private LuanTable preload;
23 private LuanTable searchers;
24 public final Set<String> blocked;
25 22
26 protected LuanState() { 23 protected LuanState() {
24 registry = new LuanTable();
27 global = new LuanTable(); 25 global = new LuanTable();
28 global.put("_G",global); 26 global.put("_G",global);
29 loaded = new LuanTable();
30 preload = new LuanTable();
31 searchers = new LuanTable();
32 blocked = new HashSet<String>();
33 } 27 }
34 28
35 protected LuanState(LuanState luan) { 29 protected LuanState(LuanState luan) {}
36 blocked = new HashSet<String>(luan.blocked);
37 }
38 30
39 @Override public void deepenClone(LuanState clone,DeepCloner cloner) { 31 @Override public void deepenClone(LuanState clone,DeepCloner cloner) {
32 clone.registry = cloner.deepClone(registry);
40 clone.global = cloner.deepClone(global); 33 clone.global = cloner.deepClone(global);
41 clone.loaded = cloner.deepClone(loaded);
42 clone.preload = cloner.deepClone(preload);
43 clone.searchers = cloner.deepClone(searchers);
44 } 34 }
45 35
46 public abstract LuanTable currentEnvironment(); 36 public abstract LuanTable currentEnvironment();
47 37
38 public final LuanTable registry() {
39 return registry;
40 }
41
42 public final LuanTable registryTable(Object key) {
43 LuanTable tbl = (LuanTable)registry.get(key);
44 if( tbl == null ) {
45 tbl = new LuanTable();
46 registry.put(key,tbl);
47 }
48 return tbl;
49 }
50
48 public final LuanTable global() { 51 public final LuanTable global() {
49 return global; 52 return global;
50 }
51
52 public final LuanTable loaded() {
53 return loaded;
54 }
55
56 public final LuanTable preload() {
57 return preload;
58 }
59
60 public final LuanTable searchers() {
61 return searchers;
62 }
63
64 public final Object get(String name) {
65 String[] a = name.split("\\.");
66 LuanTable t = loaded;
67 for( int i=0; i<a.length-1; i++ ) {
68 Object obj = t.get(a[i]);
69 if( !(obj instanceof LuanTable) )
70 return null;
71 t = (LuanTable)obj;
72 }
73 return t.get(a[a.length-1]);
74 }
75
76 public final Object set(String name,Object value) {
77 String[] a = name.split("\\.");
78 LuanTable t = loaded;
79 for( int i=0; i<a.length-1; i++ ) {
80 Object obj = t.get(a[i]);
81 if( !(obj instanceof LuanTable) )
82 return null;
83 t = (LuanTable)obj;
84 }
85 return t.put(a[a.length-1],value);
86 } 53 }
87 54
88 public static LuanState newStandard() { 55 public static LuanState newStandard() {
89 try { 56 try {
90 LuanState luan = LuanCompiler.newLuanState(); 57 LuanState luan = LuanCompiler.newLuanState();