Mercurial Hosting > luan
view src/luan/interp/LenExpr.java @ 36:2a35154aec14
implement more basic lib functions
git-svn-id: https://luan-java.googlecode.com/svn/trunk@37 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 18 Dec 2012 09:53:42 +0000 |
parents | e51906de0f11 |
children | 8a57ebfdfd78 |
line wrap: on
line source
package luan.interp; import luan.Lua; import luan.LuaNumber; import luan.LuaTable; import luan.LuaFunction; import luan.LuaException; final class LenExpr extends UnaryOpExpr { LenExpr(Expr op) { super(op); } @Override public Object eval(LuaStateImpl lua) throws LuaException { Object o = op.eval(lua); if( o instanceof String ) { String s = (String)o; return new LuaNumber( s.length() ); } LuaFunction fn = Utils.getHandlerFunction("__len",o); if( fn != null ) return Utils.first(fn.call(lua,o)); if( o instanceof LuaTable ) { LuaTable t = (LuaTable)o; return t.length(); } throw new LuaException( "attempt to get length of a " + Lua.type(o) + " value" ); } }