Mercurial Hosting > luan
comparison core/src/luan/impl/IndexExpr.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 | 3dcb0f9bee82 |
children | 3e68917a0dc6 |
comparison
equal
deleted
inserted
replaced
403:637f7ad85654 | 404:d55e873e1f0d |
---|---|
3 import luan.Luan; | 3 import luan.Luan; |
4 import luan.LuanException; | 4 import luan.LuanException; |
5 import luan.LuanTable; | 5 import luan.LuanTable; |
6 import luan.LuanFunction; | 6 import luan.LuanFunction; |
7 import luan.LuanSource; | 7 import luan.LuanSource; |
8 import luan.modules.StringLuan; | |
9 import luan.modules.BinaryLuan; | |
10 import luan.modules.JavaLuan; | |
8 | 11 |
9 | 12 |
10 final class IndexExpr extends BinaryOpExpr { | 13 final class IndexExpr extends BinaryOpExpr { |
11 | 14 |
12 IndexExpr(LuanSource.Element se,Expr op1,Expr op2) { | 15 IndexExpr(LuanSource.Element se,Expr op1,Expr op2) { |
15 | 18 |
16 @Override public Object eval(LuanStateImpl luan) throws LuanException { | 19 @Override public Object eval(LuanStateImpl luan) throws LuanException { |
17 return index(luan,op1.eval(luan),op2.eval(luan)); | 20 return index(luan,op1.eval(luan),op2.eval(luan)); |
18 } | 21 } |
19 | 22 |
20 private Object index(LuanStateImpl luan,Object t,Object key) throws LuanException { | 23 private Object index(LuanStateImpl luan,Object obj,Object key) throws LuanException { |
21 Object h; | 24 if( obj instanceof LuanTable ) { |
22 if( t instanceof LuanTable ) { | 25 LuanTable tbl = (LuanTable)obj; |
23 LuanTable tbl = (LuanTable)t; | |
24 Object value = tbl.get(key); | 26 Object value = tbl.get(key); |
25 if( value != null ) | 27 if( value != null ) |
26 return value; | 28 return value; |
27 h = luan.getHandler("__index",t); | 29 Object h = luan.getHandler("__index",tbl); |
28 if( h==null ) | 30 if( h==null ) |
29 return null; | 31 return null; |
30 } else { | 32 if( h instanceof LuanFunction ) { |
31 h = luan.getHandler("__index",t); | 33 LuanFunction fn = (LuanFunction)h; |
32 if( h==null ) | 34 return Luan.first(luan.bit(se).call(fn,"__index",new Object[]{tbl,key})); |
33 throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(t) + " value)" ); | 35 } |
36 return index(luan,h,key); | |
34 } | 37 } |
35 if( h instanceof LuanFunction ) { | 38 if( obj instanceof String ) |
36 LuanFunction fn = (LuanFunction)h; | 39 return StringLuan.__index(luan,(String)obj,key); |
37 return Luan.first(luan.bit(se).call(fn,"__index",new Object[]{t,key})); | 40 if( obj instanceof byte[] ) |
38 } | 41 return BinaryLuan.__index(luan,(byte[])obj,key); |
39 return index(luan,h,key); | 42 Object value = JavaLuan.__index(luan,obj,key); |
43 if( value != null ) | |
44 return value; | |
45 throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(obj) + " value)" ); | |
40 } | 46 } |
41 } | 47 } |