comparison 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
comparison
equal deleted inserted replaced
34:0cdc1da466ee 35:e51906de0f11
1 package luan.interp; 1 package luan.interp;
2 2
3 import luan.LuaException; 3 import luan.LuaException;
4 import luan.LuaTable; 4 import luan.LuaTable;
5 import luan.Lua; 5 import luan.Lua;
6 import luan.LuaFunction;
6 7
7 8
8 final class SetTableEntry implements Settable { 9 final class SetTableEntry implements Settable {
9 private final Expr tableExpr; 10 private final Expr tableExpr;
10 private final Expr keyExpr; 11 private final Expr keyExpr;
13 this.tableExpr = tableExpr; 14 this.tableExpr = tableExpr;
14 this.keyExpr = keyExpr; 15 this.keyExpr = keyExpr;
15 } 16 }
16 17
17 @Override public void set(LuaStateImpl lua,Object value) throws LuaException { 18 @Override public void set(LuaStateImpl lua,Object value) throws LuaException {
18 Object t = tableExpr.eval(lua); 19 newindex( lua, tableExpr.eval(lua), keyExpr.eval(lua), value );
19 if( !(t instanceof LuaTable) ) 20 }
20 throw new LuaException( "attempt to index a " + Lua.type(t) + " value" ); 21
21 LuaTable table = (LuaTable)t; 22 private static void newindex(LuaStateImpl lua,Object t,Object key,Object value) throws LuaException {
22 Object key = keyExpr.eval(lua); 23 Object h;
23 table.set(key,value); 24 if( t instanceof LuaTable ) {
25 LuaTable table = (LuaTable)t;
26 Object old = table.put(key,value);
27 if( old != null )
28 return;
29 h = Utils.getHandlerObject("__newindex",t);
30 if( h==null )
31 return;
32 table.put(key,old);
33 } else {
34 h = Utils.getHandlerObject("__newindex",t);
35 if( h==null )
36 throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
37 }
38 if( h instanceof LuaFunction ) {
39 LuaFunction fn = (LuaFunction)h;
40 fn.call(lua,t,key,value);
41 }
42 newindex(lua,h,key,value);
24 } 43 }
25 44
26 } 45 }