Mercurial Hosting > luan
diff src/luan/interp/SetTableEntry.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/SetTableEntry.java Sun Dec 16 09:23:56 2012 +0000 +++ b/src/luan/interp/SetTableEntry.java Tue Dec 18 07:05:58 2012 +0000 @@ -3,6 +3,7 @@ import luan.LuaException; import luan.LuaTable; import luan.Lua; +import luan.LuaFunction; final class SetTableEntry implements Settable { @@ -15,12 +16,30 @@ } @Override public void set(LuaStateImpl lua,Object value) throws LuaException { - Object t = tableExpr.eval(lua); - if( !(t instanceof LuaTable) ) - throw new LuaException( "attempt to index a " + Lua.type(t) + " value" ); - LuaTable table = (LuaTable)t; - Object key = keyExpr.eval(lua); - table.set(key,value); + newindex( lua, tableExpr.eval(lua), keyExpr.eval(lua), value ); + } + + private static void newindex(LuaStateImpl lua,Object t,Object key,Object value) throws LuaException { + Object h; + if( t instanceof LuaTable ) { + LuaTable table = (LuaTable)t; + Object old = table.put(key,value); + if( old != null ) + return; + h = Utils.getHandlerObject("__newindex",t); + if( h==null ) + return; + table.put(key,old); + } else { + h = Utils.getHandlerObject("__newindex",t); + if( h==null ) + throw new LuaException( "attempt to index a " + Lua.type(t) + " value" ); + } + if( h instanceof LuaFunction ) { + LuaFunction fn = (LuaFunction)h; + fn.call(lua,t,key,value); + } + newindex(lua,h,key,value); } }