comparison src/luan/LuanState.java @ 108:3c404a296995

make Package module more standard; return _ENV by default; add "import" statement; git-svn-id: https://luan-java.googlecode.com/svn/trunk@109 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 03:21:54 +0000
parents 1f8b6edc2b08
children 2428ecfed375
comparison
equal deleted inserted replaced
107:dbf459397217 108:3c404a296995
15 import luan.lib.TableLib; 15 import luan.lib.TableLib;
16 import luan.lib.HtmlLib; 16 import luan.lib.HtmlLib;
17 17
18 18
19 public abstract class LuanState implements DeepCloneable<LuanState> { 19 public abstract class LuanState implements DeepCloneable<LuanState> {
20 public static final String _G = "_G";
21
22 public final LuanBit JAVA = bit(LuanElement.JAVA); 20 public final LuanBit JAVA = bit(LuanElement.JAVA);
23 21
22 private LuanTable global;
24 private LuanTable loaded; 23 private LuanTable loaded;
25 private LuanTable preload; 24 private LuanTable preload;
26 private final List<String> defaultMods; 25 private LuanTable searchers;
27 26
28 public InputStream in = System.in; 27 public InputStream in = System.in;
29 public PrintStream out = System.out; 28 public PrintStream out = System.out;
30 public PrintStream err = System.err; 29 public PrintStream err = System.err;
31 30
32 private final List<MetatableGetter> mtGetters; 31 private final List<MetatableGetter> mtGetters;
33 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); 32 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
34 33
35 protected LuanState() { 34 protected LuanState() {
35 global = new LuanTable();
36 global.put("_G",global);
36 loaded = new LuanTable(); 37 loaded = new LuanTable();
37 preload = new LuanTable(); 38 preload = new LuanTable();
38 defaultMods = new ArrayList<String>(); 39 searchers = new LuanTable();
39 mtGetters = new ArrayList<MetatableGetter>(); 40 mtGetters = new ArrayList<MetatableGetter>();
40 } 41 }
41 42
42 protected LuanState(LuanState luan) { 43 protected LuanState(LuanState luan) {
43 mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters); 44 mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters);
44 defaultMods = new ArrayList<String>(luan.defaultMods);
45 } 45 }
46 46
47 public final LuanState deepClone() { 47 public final LuanState deepClone() {
48 return new DeepCloner().deepClone(this); 48 return new DeepCloner().deepClone(this);
49 } 49 }
50 50
51 @Override public void deepenClone(LuanState clone,DeepCloner cloner) { 51 @Override public void deepenClone(LuanState clone,DeepCloner cloner) {
52 clone.global = cloner.deepClone(global);
52 clone.loaded = cloner.deepClone(loaded); 53 clone.loaded = cloner.deepClone(loaded);
53 clone.preload = cloner.deepClone(preload); 54 clone.preload = cloner.deepClone(preload);
55 clone.searchers = cloner.deepClone(searchers);
54 } 56 }
55 57
56 public abstract LuanTable currentEnvironment(); 58 public abstract LuanTable currentEnvironment();
59
60 public final LuanTable global() {
61 return global;
62 }
57 63
58 public final LuanTable loaded() { 64 public final LuanTable loaded() {
59 return loaded; 65 return loaded;
60 } 66 }
61 67
62 public final LuanTable preload() { 68 public final LuanTable preload() {
63 return preload; 69 return preload;
70 }
71
72 public final LuanTable searchers() {
73 return searchers;
64 } 74 }
65 75
66 public final Object get(String name) { 76 public final Object get(String name) {
67 String[] a = name.split("\\."); 77 String[] a = name.split("\\.");
68 LuanTable t = loaded; 78 LuanTable t = loaded;
87 return t.put(a[a.length-1],value); 97 return t.put(a[a.length-1],value);
88 } 98 }
89 99
90 public final void load(String modName,LuanFunction loader) throws LuanException { 100 public final void load(String modName,LuanFunction loader) throws LuanException {
91 preload.put(modName,loader); 101 preload.put(modName,loader);
92 defaultMods.add(modName); 102 Object mod = PackageLib.require(this,modName);
93 PackageLib.require(this,modName); 103 if( mod==null )
94 } 104 throw new RuntimeException();
95 105 global.put(modName,mod);
96 public final LuanTable newEnvironment() throws LuanException {
97 LuanTable env = new LuanTable();
98 for( String modName : defaultMods ) {
99 PackageLib.require(this,modName,env);
100 LuanTable mod = (LuanTable)loaded.get(modName);
101 LuanTable global = (LuanTable)mod.get(_G);
102 if( global != null ) {
103 for( Map.Entry<Object,Object> entry : global ) {
104 env.put( entry.getKey(), entry.getValue() );
105 }
106 }
107 }
108 return env;
109 } 106 }
110 107
111 public static LuanState newStandard() { 108 public static LuanState newStandard() {
112 try { 109 try {
113 LuanState luan = LuanCompiler.newLuanState(); 110 LuanState luan = LuanCompiler.newLuanState();
122 } catch(LuanException e) { 119 } catch(LuanException e) {
123 throw new RuntimeException(e); 120 throw new RuntimeException(e);
124 } 121 }
125 } 122 }
126 123
127 public final Object[] eval(String cmd,String sourceName,LuanTable env) throws LuanException { 124 public final Object[] eval(String cmd,String sourceName,boolean interactive) throws LuanException {
128 LuanFunction fn = BasicLib.load(this,cmd,sourceName,env); 125 LuanFunction fn = BasicLib.load(this,cmd,sourceName,interactive);
129 return JAVA.call(fn,null); 126 return JAVA.call(fn,null);
130 } 127 }
131 128
132 129
133 public final LuanTable getMetatable(Object obj) { 130 public final LuanTable getMetatable(Object obj) {