Mercurial Hosting > luan
diff core/src/luan/impl/Closure.java @ 670:58ebfec6178b
all luan now compiles
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Apr 2016 01:05:57 -0600 |
parents | 7780cafca27f |
children | d3e5414bdf4c |
line wrap: on
line diff
--- a/core/src/luan/impl/Closure.java Mon Apr 11 16:00:44 2016 -0600 +++ b/core/src/luan/impl/Closure.java Tue Apr 12 01:05:57 2016 -0600 @@ -9,19 +9,10 @@ public abstract class Closure extends LuanFunction implements DeepCloneable, Cloneable { - private final int stackSize; - private final int numArgs; - private final boolean isVarArg; - private UpValue[] upValues; + public Pointer[] upValues; - public Closure(LuanStateImpl luan,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) throws LuanException { - this.stackSize = stackSize; - this.numArgs = numArgs; - this.isVarArg = isVarArg; - this.upValues = new UpValue[upValueGetters.length]; - for( int i=0; i<upValues.length; i++ ) { - upValues[i] = upValueGetters[i].get(luan); - } + public Closure(int nUpValues) throws LuanException { + this.upValues = new Pointer[nUpValues]; } @Override public Closure shallowClone() { @@ -33,39 +24,19 @@ } @Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) { - ((Closure)clone).upValues = (UpValue[])cloner.deepClone(upValues); - } - - UpValue[] upValues() { - return upValues; + ((Closure)clone).upValues = (Pointer[])cloner.deepClone(upValues); } - @Override public Object call(LuanState ls,Object[] args) throws LuanException { + @Override public final Object call(LuanState ls,Object[] args) throws LuanException { LuanStateImpl luan = (LuanStateImpl)ls; - Object[] varArgs = null; - if( isVarArg ) { - if( args.length > numArgs ) { - varArgs = new Object[ args.length - numArgs ]; - for( int i=0; i<varArgs.length; i++ ) { - varArgs[i] = args[numArgs+i]; - } - } else { - varArgs = LuanFunction.NOTHING; - } - } - Object[] stack = luan.newFrame(this,stackSize,varArgs); - final int n = Math.min(args.length,numArgs); - for( int i=0; i<n; i++ ) { - stack[i] = args[i]; - } + Closure old = luan.closure; + luan.closure = this; try { - return run(luan); - } catch(StackOverflowError e) { - throw new LuanException( "stack overflow", e ); + return doCall(luan,args); } finally { - luan.popFrame(); - } + luan.closure = old; + } } - public abstract Object run(LuanStateImpl luan) throws LuanException; + public abstract Object doCall(LuanState luan,Object[] args) throws LuanException; }