Mercurial Hosting > luan
diff core/src/luan/LuanTable.java @ 426:23a93c118042
fix LuanTable.get() to use metatables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2015 18:44:20 -0600 |
parents | 0a2fb80907f9 |
children | dae264ad6a7b |
line wrap: on
line diff
--- a/core/src/luan/LuanTable.java Fri May 01 17:18:23 2015 -0600 +++ b/core/src/luan/LuanTable.java Fri May 01 18:44:20 2015 -0600 @@ -90,7 +90,29 @@ return list!=null ? list : Collections.emptyList(); } - public Object get(Object key) { + public Object get(LuanState luan,Object key) throws LuanException { + Object value = rawGet(key); + if( value != null ) + return value; + Object h = getHandler("__index"); + if( h==null ) + return null; + if( h instanceof LuanFunction ) { + LuanFunction fn = (LuanFunction)h; + return Luan.first(luan.call(fn,"__index",new Object[]{this,key})); + } + if( h instanceof LuanMeta ) { + LuanMeta meta = (LuanMeta)h; + return meta.__index(luan,this,key); + } + if( h instanceof LuanTable ) { + LuanTable tbl = (LuanTable)h; + return tbl.get(luan,key); + } + throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __index"); + } + + public Object rawGet(Object key) { if( list != null ) { Integer iT = Luan.asInteger(key); if( iT != null ) { @@ -260,7 +282,7 @@ public Object getHandler(String op) { LuanTable t = getMetatable(); - return t==null ? null : t.get(op); + return t==null ? null : t.rawGet(op); } public boolean hasJava() {