comparison src/luan/LuanTable.java @ 1166:7ef40e1923b7

add back Thread.global allow metatables to have metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 08 Feb 2018 02:22:51 -0700
parents ba4daf107e07
children 73d754b1889f
comparison
equal deleted inserted replaced
1165:668f29bc52ea 1166:7ef40e1923b7
112 check(); 112 check();
113 return list!=null ? list : Collections.emptyList(); 113 return list!=null ? list : Collections.emptyList();
114 } 114 }
115 115
116 public String toString(LuanState luan) throws LuanException { 116 public String toString(LuanState luan) throws LuanException {
117 Object h = getHandler("__to_string"); 117 Object h = getHandler(luan,"__to_string");
118 if( h == null ) 118 if( h == null )
119 return rawToString(); 119 return rawToString();
120 LuanFunction fn = Luan.checkFunction(h); 120 LuanFunction fn = Luan.checkFunction(h);
121 return Luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) ); 121 return Luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) );
122 } 122 }
127 127
128 public Object get(LuanState luan,Object key) throws LuanException { 128 public Object get(LuanState luan,Object key) throws LuanException {
129 Object value = rawGet(key); 129 Object value = rawGet(key);
130 if( value != null ) 130 if( value != null )
131 return value; 131 return value;
132 Object h = getHandler("__index"); 132 Object h = getHandler(luan,"__index");
133 if( h==null ) 133 if( h==null )
134 return null; 134 return null;
135 if( h instanceof LuanFunction ) { 135 if( h instanceof LuanFunction ) {
136 LuanFunction fn = (LuanFunction)h; 136 LuanFunction fn = (LuanFunction)h;
137 return Luan.first(fn.call(luan,new Object[]{this,key})); 137 return Luan.first(fn.call(luan,new Object[]{this,key}));
157 } 157 }
158 return map.get(key); 158 return map.get(key);
159 } 159 }
160 160
161 public void put(LuanState luan,Object key,Object value) throws LuanException { 161 public void put(LuanState luan,Object key,Object value) throws LuanException {
162 Object h = getHandler("__new_index"); 162 Object h = getHandler(luan,"__new_index");
163 if( h==null || rawGet(key)!=null ) { 163 if( h==null || rawGet(key)!=null ) {
164 rawPut(key,value); 164 rawPut(key,value);
165 return; 165 return;
166 } 166 }
167 if( h instanceof LuanFunction ) { 167 if( h instanceof LuanFunction ) {
261 check(); 261 check();
262 Collections.sort(list(),cmp); 262 Collections.sort(list(),cmp);
263 } 263 }
264 264
265 public int length(LuanState luan) throws LuanException { 265 public int length(LuanState luan) throws LuanException {
266 Object h = getHandler("__len"); 266 Object h = getHandler(luan,"__len");
267 if( h != null ) { 267 if( h != null ) {
268 LuanFunction fn = Luan.checkFunction(h); 268 LuanFunction fn = Luan.checkFunction(h);
269 return (Integer)Luan.first(fn.call(luan,new Object[]{this})); 269 return (Integer)Luan.first(fn.call(luan,new Object[]{this}));
270 } 270 }
271 return rawLength(); 271 return rawLength();
293 } 293 }
294 }; 294 };
295 } 295 }
296 296
297 public Iterator<Map.Entry> iterator(final LuanState luan) throws LuanException { 297 public Iterator<Map.Entry> iterator(final LuanState luan) throws LuanException {
298 if( getHandler("__pairs") == null ) 298 if( getHandler(luan,"__pairs") == null )
299 return rawIterator(); 299 return rawIterator();
300 final LuanFunction fn = pairs(luan); 300 final LuanFunction fn = pairs(luan);
301 return new Iterator<Map.Entry>() { 301 return new Iterator<Map.Entry>() {
302 private Map.Entry<Object,Object> next = getNext(); 302 private Map.Entry<Object,Object> next = getNext();
303 303
330 } 330 }
331 }; 331 };
332 } 332 }
333 333
334 public LuanFunction pairs(LuanState luan) throws LuanException { 334 public LuanFunction pairs(LuanState luan) throws LuanException {
335 Object h = getHandler("__pairs"); 335 Object h = getHandler(luan,"__pairs");
336 if( h != null ) { 336 if( h != null ) {
337 if( h instanceof LuanFunction ) { 337 if( h instanceof LuanFunction ) {
338 LuanFunction fn = (LuanFunction)h; 338 LuanFunction fn = (LuanFunction)h;
339 Object obj = Luan.first(fn.call(luan,new Object[]{this})); 339 Object obj = Luan.first(fn.call(luan,new Object[]{this}));
340 if( !(obj instanceof LuanFunction) ) 340 if( !(obj instanceof LuanFunction) )
428 public void setMetatable(LuanTable metatable) { 428 public void setMetatable(LuanTable metatable) {
429 check(); 429 check();
430 this.metatable = metatable; 430 this.metatable = metatable;
431 } 431 }
432 432
433 public Object getHandler(String op) { 433 public Object getHandler(LuanState luan,String op) throws LuanException {
434 check(); 434 check();
435 return metatable==null ? null : metatable.rawGet(op); 435 return metatable==null ? null : metatable.get(luan,op);
436 } 436 }
437 437
438 private Map<Object,Object> newMap() { 438 private Map<Object,Object> newMap() {
439 return new LinkedHashMap<Object,Object>(); 439 return new LinkedHashMap<Object,Object>();
440 } 440 }