Mercurial Hosting > luan
view core/src/luan/impl/Closure.java @ 261:715c4a6e1169
simplify Io.stdin
git-svn-id: https://luan-java.googlecode.com/svn/trunk@262 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 28 Oct 2014 23:36:29 +0000 |
parents | 5ba136769034 |
children | a6bf8ff720f8 |
line wrap: on
line source
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; this.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); } } }