comparison src/luan/LuanState.java @ 77:4bf3d0c0b6b9

make LuanState cloneable git-svn-id: https://luan-java.googlecode.com/svn/trunk@78 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 15 Feb 2013 09:55:17 +0000
parents aa7538ae5fb6
children cca4f8522893
comparison
equal deleted inserted replaced
76:97b03fc807ad 77:4bf3d0c0b6b9
12 import luan.lib.StringLib; 12 import luan.lib.StringLib;
13 import luan.lib.TableLib; 13 import luan.lib.TableLib;
14 import luan.lib.HtmlLib; 14 import luan.lib.HtmlLib;
15 15
16 16
17 public abstract class LuanState { 17 public abstract class LuanState implements DeepCloneable<LuanState> {
18 18
19 public final LuanTable global = new LuanTable(); 19 private LuanTable global;
20 public final LuanTable loaded = new LuanTable(); 20 private LuanTable loaded;
21 public final LuanTable preload = new LuanTable(); 21 private LuanTable preload;
22 22
23 public InputStream in = System.in; 23 public InputStream in = System.in;
24 public PrintStream out = System.out; 24 public PrintStream out = System.out;
25 public PrintStream err = System.err; 25 public PrintStream err = System.err;
26 26
27 private final List<MetatableGetter> mtGetters = new ArrayList<MetatableGetter>(); 27 private final List<MetatableGetter> mtGetters;
28 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); 28 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
29 29
30 30 protected LuanState() {
31 public Object get(String name) { 31 global = new LuanTable();
32 loaded = new LuanTable();
33 preload = new LuanTable();
34 mtGetters = new ArrayList<MetatableGetter>();
35 }
36
37 protected LuanState(LuanState luan) {
38 mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters);
39 }
40
41 public final LuanState deepClone() {
42 return new DeepCloner().deepClone(this);
43 }
44
45 @Override public void deepenClone(LuanState clone,DeepCloner cloner) {
46 clone.global = cloner.deepClone(global);
47 clone.loaded = cloner.deepClone(loaded);
48 clone.preload = cloner.deepClone(preload);
49 }
50
51 public final LuanTable global() {
52 return global;
53 }
54
55 public final LuanTable loaded() {
56 return loaded;
57 }
58
59 public final LuanTable preload() {
60 return preload;
61 }
62
63 public final Object get(String name) {
32 String[] a = name.split("\\."); 64 String[] a = name.split("\\.");
33 LuanTable t = global; 65 LuanTable t = global;
34 for( int i=0; i<a.length-1; i++ ) { 66 for( int i=0; i<a.length-1; i++ ) {
35 Object obj = t.get(a[i]); 67 Object obj = t.get(a[i]);
36 if( !(obj instanceof LuanTable) ) 68 if( !(obj instanceof LuanTable) )
38 t = (LuanTable)obj; 70 t = (LuanTable)obj;
39 } 71 }
40 return t.get(a[a.length-1]); 72 return t.get(a[a.length-1]);
41 } 73 }
42 74
43 public Object set(String name,Object value) { 75 public final Object set(String name,Object value) {
44 String[] a = name.split("\\."); 76 String[] a = name.split("\\.");
45 LuanTable t = global; 77 LuanTable t = global;
46 for( int i=0; i<a.length-1; i++ ) { 78 for( int i=0; i<a.length-1; i++ ) {
47 Object obj = t.get(a[i]); 79 Object obj = t.get(a[i]);
48 if( !(obj instanceof LuanTable) ) 80 if( !(obj instanceof LuanTable) )
50 t = (LuanTable)obj; 82 t = (LuanTable)obj;
51 } 83 }
52 return t.put(a[a.length-1],value); 84 return t.put(a[a.length-1],value);
53 } 85 }
54 86
55 public void load(LuanFunction loader,String modName) throws LuanException { 87 public final void load(LuanFunction loader,String modName) throws LuanException {
56 Object mod = Luan.first(call(loader,LuanElement.JAVA,"loader",modName)); 88 Object mod = Luan.first(call(loader,LuanElement.JAVA,"loader",modName));
57 if( mod == null ) 89 if( mod == null )
58 mod = true; 90 mod = true;
59 loaded.put(modName,mod); 91 loaded.put(modName,mod);
60 if( mod instanceof LuanTable ) 92 if( mod instanceof LuanTable )
75 } catch(LuanException e) { 107 } catch(LuanException e) {
76 throw new RuntimeException(e); 108 throw new RuntimeException(e);
77 } 109 }
78 } 110 }
79 111
80 public Object[] eval(String cmd,String sourceName) throws LuanException { 112 public final Object[] eval(String cmd,String sourceName) throws LuanException {
81 LuanFunction fn = BasicLib.load(this,cmd,sourceName); 113 LuanFunction fn = BasicLib.load(this,cmd,sourceName);
82 return call(fn,null,null); 114 return call(fn,null,null);
83 } 115 }
84 116
85 117
98 130
99 public final void addMetatableGetter(MetatableGetter mg) { 131 public final void addMetatableGetter(MetatableGetter mg) {
100 mtGetters.add(mg); 132 mtGetters.add(mg);
101 } 133 }
102 134
103 public Object[] call(LuanFunction fn,LuanElement calledFrom,String fnName,Object... args) throws LuanException { 135 public final Object[] call(LuanFunction fn,LuanElement calledFrom,String fnName,Object... args) throws LuanException {
104 stackTrace.add( new StackTraceElement(calledFrom,fnName) ); 136 stackTrace.add( new StackTraceElement(calledFrom,fnName) );
105 try { 137 try {
106 return fn.call(this,args); 138 return fn.call(this,args);
107 } finally { 139 } finally {
108 stackTrace.remove(stackTrace.size()-1); 140 stackTrace.remove(stackTrace.size()-1);