comparison src/luan/interp/IndexExpr.java @ 40:e3624b7cd603

implement stack trace git-svn-id: https://luan-java.googlecode.com/svn/trunk@41 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 21 Dec 2012 10:45:54 +0000
parents 8a57ebfdfd78
children 64ecb7a3aad7
comparison
equal deleted inserted replaced
39:e5bcb1eeafc1 40:e3624b7cd603
2 2
3 import luan.Lua; 3 import luan.Lua;
4 import luan.LuaException; 4 import luan.LuaException;
5 import luan.LuaTable; 5 import luan.LuaTable;
6 import luan.LuaFunction; 6 import luan.LuaFunction;
7 import luan.LuaSource;
7 8
8 9
9 final class IndexExpr extends BinaryOpExpr { 10 final class IndexExpr extends BinaryOpExpr {
10 11
11 IndexExpr(Expr op1,Expr op2) { 12 IndexExpr(LuaSource.Element se,Expr op1,Expr op2) {
12 super(op1,op2); 13 super(se,op1,op2);
13 } 14 }
14 15
15 @Override public Object eval(LuaStateImpl lua) throws LuaException { 16 @Override public Object eval(LuaStateImpl lua) throws LuaException {
16 return index(lua,op1.eval(lua),op2.eval(lua)); 17 return index(lua,op1.eval(lua),op2.eval(lua));
17 } 18 }
18 19
19 private static Object index(LuaStateImpl lua,Object t,Object key) throws LuaException { 20 private Object index(LuaStateImpl lua,Object t,Object key) throws LuaException {
20 Object h; 21 Object h;
21 if( t instanceof LuaTable ) { 22 if( t instanceof LuaTable ) {
22 LuaTable tbl = (LuaTable)t; 23 LuaTable tbl = (LuaTable)t;
23 Object value = tbl.get(key); 24 Object value = tbl.get(key);
24 if( value != null ) 25 if( value != null )
27 if( h==null ) 28 if( h==null )
28 return null; 29 return null;
29 } else { 30 } else {
30 h = lua.getHandler("__index",t); 31 h = lua.getHandler("__index",t);
31 if( h==null ) 32 if( h==null )
32 throw new LuaException( "attempt to index a " + Lua.type(t) + " value" ); 33 throw new LuaException( lua, se, "attempt to index a " + Lua.type(t) + " value" );
33 } 34 }
34 if( h instanceof LuaFunction ) { 35 if( h instanceof LuaFunction ) {
35 LuaFunction fn = (LuaFunction)h; 36 LuaFunction fn = (LuaFunction)h;
36 return Utils.first(fn.call(lua,t,key)); 37 return Lua.first(lua.call(fn,se,"__index",t,key));
37 } 38 }
38 return index(lua,h,key); 39 return index(lua,h,key);
39 } 40 }
40 } 41 }