comparison src/luan/impl/SetTableEntry.java @ 166:4eaee12f6c65

move luan/interp to impl git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:17:38 +0000
parents src/luan/interp/SetTableEntry.java@f5af13062b10
children
comparison
equal deleted inserted replaced
165:94bbc4cbc106 166:4eaee12f6c65
1 package luan.impl;
2
3 import luan.LuanException;
4 import luan.LuanTable;
5 import luan.Luan;
6 import luan.LuanFunction;
7 import luan.LuanSource;
8
9
10 final class SetTableEntry extends CodeImpl implements Settable {
11 private final Expr tableExpr;
12 private final Expr keyExpr;
13
14 SetTableEntry(LuanSource.Element se,Expr tableExpr,Expr keyExpr) {
15 super(se);
16 this.tableExpr = tableExpr;
17 this.keyExpr = keyExpr;
18 }
19
20 @Override public void set(LuanStateImpl luan,Object value) throws LuanException {
21 newindex( luan, tableExpr.eval(luan), keyExpr.eval(luan), value );
22 }
23
24 private void newindex(LuanStateImpl luan,Object t,Object key,Object value) throws LuanException {
25 Object h;
26 if( t instanceof LuanTable ) {
27 LuanTable table = (LuanTable)t;
28 Object old = table.put(key,value);
29 if( old != null )
30 return;
31 h = luan.getHandler("__newindex",t);
32 if( h==null )
33 return;
34 table.put(key,old);
35 } else {
36 h = luan.getHandler("__newindex",t);
37 if( h==null )
38 throw luan.bit(se).exception( "attempt to index a " + Luan.type(t) + " value" );
39 }
40 if( h instanceof LuanFunction ) {
41 LuanFunction fn = (LuanFunction)h;
42 luan.bit(se).call(fn,"__newindex",new Object[]{t,key,value});
43 return;
44 }
45 newindex(luan,h,key,value);
46 }
47
48 }