view core/src/luan/impl/FnCall.java @ 646:cdc70de628b5

simplify LuanException
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 19:58:39 -0600
parents 859c0dedc8b6
children 37f0cf43f191
line wrap: on
line source

package luan.impl;

import luan.Luan;
import luan.LuanFunction;
import luan.LuanException;
import luan.LuanTable;


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

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

	@Override public Object eval(LuanStateImpl luan) throws LuanException {
		return call( luan, fnExpr.eval(luan), Luan.array(args.eval(luan)) );
	}

	private Object call(LuanStateImpl luan,Object o,Object[] argVals) throws LuanException {
		if( o instanceof LuanFunction ) {
			LuanFunction fn = (LuanFunction)o;
			return fn.call( luan, argVals );
		}
		if( o instanceof LuanTable ) {
			LuanTable t = (LuanTable)o;
			Object h = t.getHandler("__call");
			if( h != null )
				return call(luan,h,argVals);
		}
		throw new LuanException( "attempt to call a " + Luan.type(o) + " value" );
	}

	@Override public String toString() {
		return "(FnCall "+fnExpr+" "+args+")";
	}
}