view core/src/luan/impl/Closure.java @ 614:9ecf5673fb6d

start version 0.15
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Dec 2015 23:55:36 -0700
parents 1c64b1fd882b
children 859c0dedc8b6
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 {
	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(DeepCloneable clone,DeepCloner cloner) {
		((Closure)clone).upValues = (UpValue[])cloner.deepClone(upValues);
	}

	UpValue[] upValues() {
		return upValues;
	}

	@Override public Object call(LuanState luan,Object[] args) throws LuanException {
		try {
			return call(this,(LuanStateImpl)luan,args);
		} catch(StackOverflowError e) {
			throw new LuanException(luan, "stack overflow" );
		}
	}

	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);
		}
	}

}