Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- a/src/luan/interp/LuanStateImpl.java Mon Feb 25 03:53:54 2013 +0000 +++ b/src/luan/interp/LuanStateImpl.java Wed Feb 27 08:03:51 2013 +0000 @@ -2,6 +2,8 @@ import java.util.List; import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -53,10 +55,11 @@ } private Frame frame = null; - Object[] returnValues = LuanFunction.EMPTY_RTN; + Object[] returnValues = LuanFunction.EMPTY; Closure tailFn; + Map<UpValue.EnvGetter,UpValue> envs = new HashMap<UpValue.EnvGetter,UpValue>(); - public LuanStateImpl() {} + LuanStateImpl() {} private LuanStateImpl(LuanStateImpl luan) { super(luan); @@ -68,6 +71,15 @@ return new LuanStateImpl(this); } + @Override public void deepenClone(LuanState clone,DeepCloner cloner) { + super.deepenClone(clone,cloner); + LuanStateImpl cloneImpl = (LuanStateImpl)clone; + cloneImpl.envs = new HashMap<UpValue.EnvGetter,UpValue>(); + for( Map.Entry<UpValue.EnvGetter,UpValue> entry : envs.entrySet() ) { + cloneImpl.envs.put( entry.getKey(), cloner.deepClone(entry.getValue()) ); + } + } + // returns stack Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) { frame = new Frame(frame,closure,stackSize,varArgs); @@ -75,7 +87,7 @@ } void popFrame() { - returnValues = LuanFunction.EMPTY_RTN; + returnValues = LuanFunction.EMPTY; tailFn = null; frame = frame.previousFrame; } @@ -104,6 +116,22 @@ return frame.getUpValue(index); } + UpValue getUpValue(UpValue.EnvGetter getter) throws LuanException { + UpValue uv = envs.get(getter); + if( uv == null ) { + LuanTable env = newEnvironment(); + uv = new UpValue(env); + envs.put(getter,uv); + } + return uv; + } + + @Override public LuanTable currentEnvironment() { + if( frame==null ) + return null; + return (LuanTable)frame.closure.upValues()[0].get(); + } + final Object arithmetic(LuanElement el,String op,Object o1,Object o2) throws LuanException { LuanFunction fn = getBinHandler(el,op,o1,o2);