view src/luan/interp/FnCall.java @ 40:e3624b7cd603

implement stack trace git-svn-id: https://luan-java.googlecode.com/svn/trunk@41 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 21 Dec 2012 10:45:54 +0000
parents 8a57ebfdfd78
children 64ecb7a3aad7
line wrap: on
line source

package luan.interp;

import luan.Lua;
import luan.LuaFunction;
import luan.LuaException;
import luan.LuaSource;


final class FnCall extends CodeImpl implements Expressions {
	final Expr fnExpr;
	final Expressions args;
	final String fnName;

	FnCall(LuaSource.Element se,Expr fnExpr,Expressions args) {
		super(se);
		this.fnExpr = fnExpr;
		this.args = args;
		this.fnName = fnExpr.se().text();
	}

	@Override public Object[] eval(LuaStateImpl lua) throws LuaException {
		return call( lua, fnExpr.eval(lua) );
	}

	private Object[] call(LuaStateImpl lua,Object o) throws LuaException {
		if( o instanceof LuaFunction ) {
			LuaFunction fn = (LuaFunction)o;
			return lua.call( fn, se, fnName, args.eval(lua) );
		}
		Object h = lua.getHandler("__call",o);
		if( h != null )
			return call(lua,h);
		throw new LuaException( lua, se, "attempt to call a " + Lua.type(o) + " value" );
	}
}