view src/luan/interp/LenExpr.java @ 46:a443637829c1

remove LuaNumber git-svn-id: https://luan-java.googlecode.com/svn/trunk@47 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 27 Dec 2012 01:48:36 +0000
parents 57054fa43189
children 64ecb7a3aad7
line wrap: on
line source

package luan.interp;

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


final class LenExpr extends UnaryOpExpr {

	LenExpr(LuaSource.Element se,Expr op) {
		super(se,op);
	}

	@Override public Object eval(LuaStateImpl lua) throws LuaException {
		Object o = op.eval(lua);
		if( o instanceof String ) {
			String s = (String)o;
			return s.length();
		}
		LuaFunction fn = lua.getHandlerFunction(se,"__len",o);
		if( fn != null )
			return Lua.first(lua.call(fn,se,"__len",o));
		if( o instanceof LuaTable ) {
			LuaTable t = (LuaTable)o;
			return t.length();
		}
		throw new LuaException( lua, se, "attempt to get length of a " + Lua.type(o) + " value" );
	}
}