comparison 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
comparison
equal deleted inserted replaced
425:0a2fb80907f9 426:23a93c118042
88 88
89 public List<Object> asList() { 89 public List<Object> asList() {
90 return list!=null ? list : Collections.emptyList(); 90 return list!=null ? list : Collections.emptyList();
91 } 91 }
92 92
93 public Object get(Object key) { 93 public Object get(LuanState luan,Object key) throws LuanException {
94 Object value = rawGet(key);
95 if( value != null )
96 return value;
97 Object h = getHandler("__index");
98 if( h==null )
99 return null;
100 if( h instanceof LuanFunction ) {
101 LuanFunction fn = (LuanFunction)h;
102 return Luan.first(luan.call(fn,"__index",new Object[]{this,key}));
103 }
104 if( h instanceof LuanMeta ) {
105 LuanMeta meta = (LuanMeta)h;
106 return meta.__index(luan,this,key);
107 }
108 if( h instanceof LuanTable ) {
109 LuanTable tbl = (LuanTable)h;
110 return tbl.get(luan,key);
111 }
112 throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __index");
113 }
114
115 public Object rawGet(Object key) {
94 if( list != null ) { 116 if( list != null ) {
95 Integer iT = Luan.asInteger(key); 117 Integer iT = Luan.asInteger(key);
96 if( iT != null ) { 118 if( iT != null ) {
97 int i = iT - 1; 119 int i = iT - 1;
98 if( i>=0 && i<list.size() ) 120 if( i>=0 && i<list.size() )
258 this.metatable = metatable; 280 this.metatable = metatable;
259 } 281 }
260 282
261 public Object getHandler(String op) { 283 public Object getHandler(String op) {
262 LuanTable t = getMetatable(); 284 LuanTable t = getMetatable();
263 return t==null ? null : t.get(op); 285 return t==null ? null : t.rawGet(op);
264 } 286 }
265 287
266 public boolean hasJava() { 288 public boolean hasJava() {
267 return hasJava; 289 return hasJava;
268 } 290 }