Mercurial Hosting > luan
diff src/luan/interp/LenExpr.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 diff
--- a/src/luan/interp/LenExpr.java Sun Dec 16 09:23:56 2012 +0000 +++ b/src/luan/interp/LenExpr.java Tue Dec 18 07:05:58 2012 +0000 @@ -3,6 +3,7 @@ import luan.Lua; import luan.LuaNumber; import luan.LuaTable; +import luan.LuaFunction; import luan.LuaException; @@ -13,18 +14,18 @@ } @Override public Object eval(LuaStateImpl lua) throws LuaException { - return new LuaNumber( length(op.eval(lua)) ); - } - - private static int length(Object obj) throws LuaException { - if( obj instanceof String ) { - String s = (String)obj; - return s.length(); + Object o = op.eval(lua); + if( o instanceof String ) { + String s = (String)o; + return new LuaNumber( s.length() ); } - if( obj instanceof LuaTable ) { - LuaTable t = (LuaTable)obj; + LuaFunction fn = Utils.getHandler("__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(obj) + " value" ); + throw new LuaException( "attempt to get length of a " + Lua.type(o) + " value" ); } }