comparison src/luan/interp/LuanStateImpl.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 4bf3d0c0b6b9
children 6ca02b188dba
comparison
equal deleted inserted replaced
85:b2551f00bc51 86:6db8f286fa6c
1 package luan.interp; 1 package luan.interp;
2 2
3 import java.util.List; 3 import java.util.List;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 import java.util.Map;
6 import java.util.HashMap;
5 import luan.Luan; 7 import luan.Luan;
6 import luan.LuanState; 8 import luan.LuanState;
7 import luan.LuanTable; 9 import luan.LuanTable;
8 import luan.LuanFunction; 10 import luan.LuanFunction;
9 import luan.MetatableGetter; 11 import luan.MetatableGetter;
51 return downValues[index]; 53 return downValues[index];
52 } 54 }
53 } 55 }
54 56
55 private Frame frame = null; 57 private Frame frame = null;
56 Object[] returnValues = LuanFunction.EMPTY_RTN; 58 Object[] returnValues = LuanFunction.EMPTY;
57 Closure tailFn; 59 Closure tailFn;
60 Map<UpValue.EnvGetter,UpValue> envs = new HashMap<UpValue.EnvGetter,UpValue>();
58 61
59 public LuanStateImpl() {} 62 LuanStateImpl() {}
60 63
61 private LuanStateImpl(LuanStateImpl luan) { 64 private LuanStateImpl(LuanStateImpl luan) {
62 super(luan); 65 super(luan);
63 } 66 }
64 67
66 if( frame != null ) 69 if( frame != null )
67 throw new IllegalStateException("frame isn't null"); 70 throw new IllegalStateException("frame isn't null");
68 return new LuanStateImpl(this); 71 return new LuanStateImpl(this);
69 } 72 }
70 73
74 @Override public void deepenClone(LuanState clone,DeepCloner cloner) {
75 super.deepenClone(clone,cloner);
76 LuanStateImpl cloneImpl = (LuanStateImpl)clone;
77 cloneImpl.envs = new HashMap<UpValue.EnvGetter,UpValue>();
78 for( Map.Entry<UpValue.EnvGetter,UpValue> entry : envs.entrySet() ) {
79 cloneImpl.envs.put( entry.getKey(), cloner.deepClone(entry.getValue()) );
80 }
81 }
82
71 // returns stack 83 // returns stack
72 Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) { 84 Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) {
73 frame = new Frame(frame,closure,stackSize,varArgs); 85 frame = new Frame(frame,closure,stackSize,varArgs);
74 return frame.stack; 86 return frame.stack;
75 } 87 }
76 88
77 void popFrame() { 89 void popFrame() {
78 returnValues = LuanFunction.EMPTY_RTN; 90 returnValues = LuanFunction.EMPTY;
79 tailFn = null; 91 tailFn = null;
80 frame = frame.previousFrame; 92 frame = frame.previousFrame;
81 } 93 }
82 94
83 Object stackGet(int index) { 95 Object stackGet(int index) {
102 114
103 UpValue getUpValue(int index) { 115 UpValue getUpValue(int index) {
104 return frame.getUpValue(index); 116 return frame.getUpValue(index);
105 } 117 }
106 118
119 UpValue getUpValue(UpValue.EnvGetter getter) throws LuanException {
120 UpValue uv = envs.get(getter);
121 if( uv == null ) {
122 LuanTable env = newEnvironment();
123 uv = new UpValue(env);
124 envs.put(getter,uv);
125 }
126 return uv;
127 }
128
129 @Override public LuanTable currentEnvironment() {
130 if( frame==null )
131 return null;
132 return (LuanTable)frame.closure.upValues()[0].get();
133 }
134
107 135
108 final Object arithmetic(LuanElement el,String op,Object o1,Object o2) throws LuanException { 136 final Object arithmetic(LuanElement el,String op,Object o1,Object o2) throws LuanException {
109 LuanFunction fn = getBinHandler(el,op,o1,o2); 137 LuanFunction fn = getBinHandler(el,op,o1,o2);
110 if( fn != null ) 138 if( fn != null )
111 return Luan.first(call(fn,el,op,o1,o2)); 139 return Luan.first(call(fn,el,op,o1,o2));