view src/luan/interp/Chunk.java @ 32:c3eab5a3ce3c

implement closures git-svn-id: https://luan-java.googlecode.com/svn/trunk@33 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 14 Dec 2012 05:40:35 +0000
parents 5cf15507d77e
children e3624b7cd603
line wrap: on
line source

package luan.interp;

import luan.LuaException;


final class Chunk implements Expr {
	final Stmt block;
	final int stackSize;
	final int numArgs;
	final boolean isVarArg;
	final UpValue.Getter[] upValueGetters;

	Chunk(Stmt block,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) {
		this.block = block;
		this.stackSize = stackSize;
		this.numArgs = numArgs;
		this.isVarArg = isVarArg;
		this.upValueGetters = upValueGetters;
		fixReturns(block);
	}

	private static void fixReturns(Stmt stmt) {
		if( stmt instanceof ReturnStmt ) {
			ReturnStmt rs = (ReturnStmt)stmt;
			rs.throwReturnException = false;
		} else if( stmt instanceof Block ) {
			Block b = (Block)stmt;
			fixReturns( b.stmts[b.stmts.length-1] );
		} else if( stmt instanceof IfStmt ) {
			IfStmt is = (IfStmt)stmt;
			fixReturns( is.thenStmt );
			fixReturns( is.elseStmt );
		}
	}

	LuaClosure newClosure(LuaStateImpl lua) {
		return new LuaClosure(this,lua);
	}

	@Override public Object eval(LuaStateImpl lua) {
		return newClosure(lua);
	}

}