view src/luan/interp/FnCall.java @ 35:e51906de0f11

implement metatables git-svn-id: https://luan-java.googlecode.com/svn/trunk@36 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 18 Dec 2012 07:05:58 +0000
parents 5cf15507d77e
children 2a35154aec14
line wrap: on
line source

package luan.interp;

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


final class FnCall implements Expressions {
	final Expr fnExpr;
	final Expressions args;

	FnCall(Expr fnExpr,Expressions args) {
		this.fnExpr = fnExpr;
		this.args = args;
	}

	@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 fn.call( lua, args.eval(lua) );
		}
		Object h = Utils.getHandlerObject("__call",o);
		if( h != null )
			return call(lua,h);
		throw new LuaException( "attempt to call a " + Lua.type(o) + " value" );
	}
}