Mercurial Hosting > luan
comparison src/luan/interp/IndexExpr.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 | |
children | 2a35154aec14 |
comparison
equal
deleted
inserted
replaced
34:0cdc1da466ee | 35:e51906de0f11 |
---|---|
1 package luan.interp; | |
2 | |
3 import luan.Lua; | |
4 import luan.LuaException; | |
5 import luan.LuaTable; | |
6 import luan.LuaFunction; | |
7 | |
8 | |
9 final class IndexExpr extends BinaryOpExpr { | |
10 | |
11 IndexExpr(Expr op1,Expr op2) { | |
12 super(op1,op2); | |
13 } | |
14 | |
15 @Override public Object eval(LuaStateImpl lua) throws LuaException { | |
16 return index(lua,op1.eval(lua),op2.eval(lua)); | |
17 } | |
18 | |
19 private static Object index(LuaStateImpl lua,Object t,Object key) throws LuaException { | |
20 Object h; | |
21 if( t instanceof LuaTable ) { | |
22 LuaTable tbl = (LuaTable)t; | |
23 Object value = tbl.get(key); | |
24 if( value != null ) | |
25 return value; | |
26 h = Utils.getHandlerObject("__index",t); | |
27 if( h==null ) | |
28 return null; | |
29 } else { | |
30 h = Utils.getHandlerObject("__index",t); | |
31 if( h==null ) | |
32 throw new LuaException( "attempt to index a " + Lua.type(t) + " value" ); | |
33 } | |
34 if( h instanceof LuaFunction ) { | |
35 LuaFunction fn = (LuaFunction)h; | |
36 return Utils.first(fn.call(lua,t,key)); | |
37 } | |
38 return index(lua,h,key); | |
39 } | |
40 } |