Mercurial Hosting > luan
diff src/luan/impl/Closure.java @ 166:4eaee12f6c65
move luan/interp to impl
git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 04:17:38 +0000 |
parents | src/luan/interp/Closure.java@14281d5bd36f |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/impl/Closure.java Sun Jun 22 04:17:38 2014 +0000 @@ -0,0 +1,79 @@ +package luan.impl; + +import luan.Luan; +import luan.LuanFunction; +import luan.LuanState; +import luan.LuanElement; +import luan.LuanException; +import luan.DeepCloner; +import luan.DeepCloneable; + + +final class Closure extends LuanFunction implements DeepCloneable<Closure> { + private final FnDef fnDef; + private UpValue[] upValues; + + Closure(LuanStateImpl luan,FnDef fnDef) throws LuanException { + this.fnDef = fnDef; + UpValue.Getter[] upValueGetters = fnDef.upValueGetters; + upValues = new UpValue[upValueGetters.length]; + for( int i=0; i<upValues.length; i++ ) { + upValues[i] = upValueGetters[i].get(luan); + } + } + + private Closure(Closure c) { + this.fnDef = c.fnDef; + } + + @Override public Closure shallowClone() { + return new Closure(this); + } + + @Override public void deepenClone(Closure clone,DeepCloner cloner) { + clone.upValues = cloner.deepClone(upValues); + } + + UpValue[] upValues() { + return upValues; + } + + @Override public Object call(LuanState luan,Object[] args) throws LuanException { + return call(this,(LuanStateImpl)luan,args); + } + + private static Object call(Closure closure,LuanStateImpl luan,Object[] args) throws LuanException { + while(true) { + FnDef fnDef = closure.fnDef; + Object[] varArgs = null; + if( fnDef.isVarArg ) { + if( args.length > fnDef.numArgs ) { + varArgs = new Object[ args.length - fnDef.numArgs ]; + for( int i=0; i<varArgs.length; i++ ) { + varArgs[i] = args[fnDef.numArgs+i]; + } + } else { + varArgs = LuanFunction.NOTHING; + } + } + Object[] stack = luan.newFrame(closure,fnDef.stackSize,varArgs); + final int n = Math.min(args.length,fnDef.numArgs); + for( int i=0; i<n; i++ ) { + stack[i] = args[i]; + } + Object returnValues; + try { + fnDef.block.eval(luan); + } catch(ReturnException e) { + } finally { + returnValues = luan.returnValues; + closure = luan.tailFn; + luan.popFrame(); + } + if( closure == null ) + return returnValues; + args = Luan.array(returnValues); + } + } + +}