Mercurial Hosting > luan
comparison src/luan/LuanState.java @ 86:6db8f286fa6c
_ENV is per module, not global
git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 27 Feb 2013 08:03:51 +0000 |
parents | cca4f8522893 |
children | 6ca02b188dba |
comparison
equal
deleted
inserted
replaced
85:b2551f00bc51 | 86:6db8f286fa6c |
---|---|
2 | 2 |
3 import java.io.InputStream; | 3 import java.io.InputStream; |
4 import java.io.PrintStream; | 4 import java.io.PrintStream; |
5 import java.util.List; | 5 import java.util.List; |
6 import java.util.ArrayList; | 6 import java.util.ArrayList; |
7 import java.util.Map; | |
8 import java.util.LinkedHashMap; | |
7 import luan.interp.LuanCompiler; | 9 import luan.interp.LuanCompiler; |
8 import luan.lib.BasicLib; | 10 import luan.lib.BasicLib; |
9 import luan.lib.PackageLib; | 11 import luan.lib.PackageLib; |
10 import luan.lib.JavaLib; | 12 import luan.lib.JavaLib; |
11 import luan.lib.MathLib; | 13 import luan.lib.MathLib; |
13 import luan.lib.TableLib; | 15 import luan.lib.TableLib; |
14 import luan.lib.HtmlLib; | 16 import luan.lib.HtmlLib; |
15 | 17 |
16 | 18 |
17 public abstract class LuanState implements DeepCloneable<LuanState> { | 19 public abstract class LuanState implements DeepCloneable<LuanState> { |
18 | 20 public static final String _G = "_G"; |
19 private LuanTable global; | 21 |
20 private LuanTable loaded; | 22 private LuanTable loaded; |
21 private LuanTable preload; | 23 private LuanTable preload; |
24 private final List<String> defaultMods; | |
22 | 25 |
23 public InputStream in = System.in; | 26 public InputStream in = System.in; |
24 public PrintStream out = System.out; | 27 public PrintStream out = System.out; |
25 public PrintStream err = System.err; | 28 public PrintStream err = System.err; |
26 | 29 |
27 private final List<MetatableGetter> mtGetters; | 30 private final List<MetatableGetter> mtGetters; |
28 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); | 31 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); |
29 | 32 |
30 protected LuanState() { | 33 protected LuanState() { |
31 global = new LuanTable(); | |
32 loaded = new LuanTable(); | 34 loaded = new LuanTable(); |
33 preload = new LuanTable(); | 35 preload = new LuanTable(); |
36 defaultMods = new ArrayList<String>(); | |
34 mtGetters = new ArrayList<MetatableGetter>(); | 37 mtGetters = new ArrayList<MetatableGetter>(); |
35 } | 38 } |
36 | 39 |
37 protected LuanState(LuanState luan) { | 40 protected LuanState(LuanState luan) { |
38 mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters); | 41 mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters); |
42 defaultMods = new ArrayList<String>(luan.defaultMods); | |
39 } | 43 } |
40 | 44 |
41 public final LuanState deepClone() { | 45 public final LuanState deepClone() { |
42 return new DeepCloner().deepClone(this); | 46 return new DeepCloner().deepClone(this); |
43 } | 47 } |
44 | 48 |
45 @Override public void deepenClone(LuanState clone,DeepCloner cloner) { | 49 @Override public void deepenClone(LuanState clone,DeepCloner cloner) { |
46 clone.global = cloner.deepClone(global); | |
47 clone.loaded = cloner.deepClone(loaded); | 50 clone.loaded = cloner.deepClone(loaded); |
48 clone.preload = cloner.deepClone(preload); | 51 clone.preload = cloner.deepClone(preload); |
49 } | 52 } |
50 | 53 |
51 public final LuanTable global() { | 54 public abstract LuanTable currentEnvironment(); |
52 return global; | |
53 } | |
54 | 55 |
55 public final LuanTable loaded() { | 56 public final LuanTable loaded() { |
56 return loaded; | 57 return loaded; |
57 } | 58 } |
58 | 59 |
60 return preload; | 61 return preload; |
61 } | 62 } |
62 | 63 |
63 public final Object get(String name) { | 64 public final Object get(String name) { |
64 String[] a = name.split("\\."); | 65 String[] a = name.split("\\."); |
65 LuanTable t = global; | 66 LuanTable t = loaded; |
66 for( int i=0; i<a.length-1; i++ ) { | 67 for( int i=0; i<a.length-1; i++ ) { |
67 Object obj = t.get(a[i]); | 68 Object obj = t.get(a[i]); |
68 if( !(obj instanceof LuanTable) ) | 69 if( !(obj instanceof LuanTable) ) |
69 return null; | 70 return null; |
70 t = (LuanTable)obj; | 71 t = (LuanTable)obj; |
72 return t.get(a[a.length-1]); | 73 return t.get(a[a.length-1]); |
73 } | 74 } |
74 | 75 |
75 public final Object set(String name,Object value) { | 76 public final Object set(String name,Object value) { |
76 String[] a = name.split("\\."); | 77 String[] a = name.split("\\."); |
77 LuanTable t = global; | 78 LuanTable t = loaded; |
78 for( int i=0; i<a.length-1; i++ ) { | 79 for( int i=0; i<a.length-1; i++ ) { |
79 Object obj = t.get(a[i]); | 80 Object obj = t.get(a[i]); |
80 if( !(obj instanceof LuanTable) ) | 81 if( !(obj instanceof LuanTable) ) |
81 return null; | 82 return null; |
82 t = (LuanTable)obj; | 83 t = (LuanTable)obj; |
83 } | 84 } |
84 return t.put(a[a.length-1],value); | 85 return t.put(a[a.length-1],value); |
85 } | 86 } |
86 | 87 |
87 public final void load(String modName,LuanFunction loader) throws LuanException { | 88 public final void load(String modName,LuanFunction loader) throws LuanException { |
88 Object mod = Luan.first(call(loader,LuanElement.JAVA,"loader",modName)); | 89 preload.put(modName,loader); |
89 if( mod == null ) | 90 defaultMods.add(modName); |
90 mod = true; | 91 PackageLib.require(this,modName); |
91 loaded.put(modName,mod); | 92 } |
92 if( mod instanceof LuanTable ) | 93 |
93 global.put(modName,mod); | 94 public final LuanTable newEnvironment() throws LuanException { |
95 LuanTable env = new LuanTable(); | |
96 for( String modName : defaultMods ) { | |
97 PackageLib.require(this,modName,env); | |
98 LuanTable mod = (LuanTable)loaded.get(modName); | |
99 LuanTable global = (LuanTable)mod.get(_G); | |
100 if( global != null ) { | |
101 for( Map.Entry<Object,Object> entry : global ) { | |
102 env.put( entry.getKey(), entry.getValue() ); | |
103 } | |
104 } | |
105 } | |
106 return env; | |
94 } | 107 } |
95 | 108 |
96 public static LuanState newStandard() { | 109 public static LuanState newStandard() { |
97 try { | 110 try { |
98 LuanState luan = LuanCompiler.newLuanState(); | 111 LuanState luan = LuanCompiler.newLuanState(); |
107 } catch(LuanException e) { | 120 } catch(LuanException e) { |
108 throw new RuntimeException(e); | 121 throw new RuntimeException(e); |
109 } | 122 } |
110 } | 123 } |
111 | 124 |
112 public final Object[] eval(String cmd,String sourceName) throws LuanException { | 125 public final Object[] eval(String cmd,String sourceName,LuanTable env) throws LuanException { |
113 LuanFunction fn = BasicLib.load(this,cmd,sourceName); | 126 LuanFunction fn = BasicLib.load(this,cmd,sourceName,env); |
114 return call(fn,null,null); | 127 return call(fn,null,null); |
115 } | 128 } |
116 | 129 |
117 | 130 |
118 public final LuanTable getMetatable(Object obj) { | 131 public final LuanTable getMetatable(Object obj) { |