comparison core/src/luan/impl/SetTableEntry.java @ 404:d55e873e1f0d

metatables now only apply to tables
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 07:04:40 -0600
parents 05eb2837ddbf
children 3e68917a0dc6
comparison
equal deleted inserted replaced
403:637f7ad85654 404:d55e873e1f0d
3 import luan.LuanException; 3 import luan.LuanException;
4 import luan.LuanTable; 4 import luan.LuanTable;
5 import luan.Luan; 5 import luan.Luan;
6 import luan.LuanFunction; 6 import luan.LuanFunction;
7 import luan.LuanSource; 7 import luan.LuanSource;
8 import luan.modules.JavaLuan;
8 9
9 10
10 final class SetTableEntry extends CodeImpl implements Settable { 11 final class SetTableEntry extends CodeImpl implements Settable {
11 private final Expr tableExpr; 12 private final Expr tableExpr;
12 private final Expr keyExpr; 13 private final Expr keyExpr;
20 @Override public void set(LuanStateImpl luan,Object value) throws LuanException { 21 @Override public void set(LuanStateImpl luan,Object value) throws LuanException {
21 newindex( luan, tableExpr.eval(luan), keyExpr.eval(luan), value ); 22 newindex( luan, tableExpr.eval(luan), keyExpr.eval(luan), value );
22 } 23 }
23 24
24 private void newindex(LuanStateImpl luan,Object t,Object key,Object value) throws LuanException { 25 private void newindex(LuanStateImpl luan,Object t,Object key,Object value) throws LuanException {
25 Object h;
26 if( t instanceof LuanTable ) { 26 if( t instanceof LuanTable ) {
27 LuanTable table = (LuanTable)t; 27 LuanTable table = (LuanTable)t;
28 h = luan.getHandler("__newindex",table); 28 Object h = luan.getHandler("__newindex",table);
29 if( h==null || table.get(key)!=null ) { 29 if( h==null || table.get(key)!=null ) {
30 try { 30 try {
31 table.put(key,value); 31 table.put(key,value);
32 } catch(IllegalArgumentException e) { 32 } catch(IllegalArgumentException e) {
33 throw luan.bit(se).exception(e); 33 throw luan.bit(se).exception(e);
34 } catch(UnsupportedOperationException e) { 34 } catch(UnsupportedOperationException e) {
35 throw luan.bit(se).exception(e); 35 throw luan.bit(se).exception(e);
36 } 36 }
37 return; 37 return;
38 } 38 }
39 } else { 39 if( h instanceof LuanFunction ) {
40 h = luan.getHandler("__newindex",t); 40 LuanFunction fn = (LuanFunction)h;
41 if( h==null ) 41 luan.bit(se).call(fn,"__newindex",new Object[]{t,key,value});
42 throw luan.bit(se).exception( "attempt to index '"+tableExpr.se().text()+"' (a " + Luan.type(t) + " value)" ); 42 return;
43 }
44 newindex(luan,h,key,value);
43 } 45 }
44 if( h instanceof LuanFunction ) { 46 if( !JavaLuan.__newindex(luan,t,key,value) )
45 LuanFunction fn = (LuanFunction)h; 47 throw luan.bit(se).exception( "attempt to index '"+tableExpr.se().text()+"' (a " + Luan.type(t) + " value)" );
46 luan.bit(se).call(fn,"__newindex",new Object[]{t,key,value});
47 return;
48 }
49 newindex(luan,h,key,value);
50 } 48 }
51 49
52 } 50 }