Mercurial Hosting > luan
comparison src/luan/LuanTable.java @ 1607:fa066aaa068c
nginx caching
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 30 Apr 2021 20:23:28 -0600 |
parents | c922446f53aa |
children | 92beba8bf1c8 |
comparison
equal
deleted
inserted
replaced
1606:7c7f28c724e8 | 1607:fa066aaa068c |
---|---|
103 public Map rawMap() { | 103 public Map rawMap() { |
104 return map!=null ? map : Collections.emptyMap(); | 104 return map!=null ? map : Collections.emptyMap(); |
105 } | 105 } |
106 | 106 |
107 public String toStringLuan(Luan luan) throws LuanException { | 107 public String toStringLuan(Luan luan) throws LuanException { |
108 Object h = getHandler(luan,"__to_string"); | 108 LuanFunction fn = luan.getHandlerFunction("__to_string",this); |
109 if( h == null ) | 109 if( fn == null ) |
110 return rawToString(); | 110 return rawToString(); |
111 LuanFunction fn = Luan.checkFunction(h); | |
112 return Luan.checkString( Luan.first( fn.call(luan,this) ) ); | 111 return Luan.checkString( Luan.first( fn.call(luan,this) ) ); |
113 } | 112 } |
114 | 113 |
115 public String rawToString() { | 114 public String rawToString() { |
116 return "table: " + Integer.toHexString(hashCode()); | 115 return "table: " + Integer.toHexString(hashCode()); |
266 checkMutable(); | 265 checkMutable(); |
267 Collections.sort(list(),cmp); | 266 Collections.sort(list(),cmp); |
268 } | 267 } |
269 | 268 |
270 public int length(Luan luan) throws LuanException { | 269 public int length(Luan luan) throws LuanException { |
271 Object h = getHandler(luan,"__len"); | 270 LuanFunction fn = luan.getHandlerFunction("__len",this); |
272 if( h != null ) { | 271 if( fn != null ) { |
273 LuanFunction fn = Luan.checkFunction(h); | |
274 return (Integer)Luan.first(fn.call(luan,this)); | 272 return (Integer)Luan.first(fn.call(luan,this)); |
275 } | 273 } |
276 return rawLength(); | 274 return rawLength(); |
277 } | 275 } |
278 | 276 |
297 } | 295 } |
298 }; | 296 }; |
299 } | 297 } |
300 | 298 |
301 public Iterator<Map.Entry> iterator(final Luan luan) throws LuanException { | 299 public Iterator<Map.Entry> iterator(final Luan luan) throws LuanException { |
302 if( getHandler(luan,"__pairs") == null ) | 300 LuanFunction h = luan.getHandlerFunction("__pairs",this); |
301 if( h == null ) | |
303 return rawIterator(); | 302 return rawIterator(); |
304 final LuanFunction fn = pairs(luan); | 303 final LuanFunction fn = pairs(luan,h); |
305 return new Iterator<Map.Entry>() { | 304 return new Iterator<Map.Entry>() { |
306 private Map.Entry<Object,Object> next = getNext(); | 305 private Map.Entry<Object,Object> next = getNext(); |
307 | 306 |
308 private Map.Entry<Object,Object> getNext() { | 307 private Map.Entry<Object,Object> getNext() { |
309 try { | 308 try { |
334 } | 333 } |
335 }; | 334 }; |
336 } | 335 } |
337 | 336 |
338 public LuanFunction pairs(Luan luan) throws LuanException { | 337 public LuanFunction pairs(Luan luan) throws LuanException { |
339 Object h = getHandler(luan,"__pairs"); | 338 return pairs( luan, luan.getHandlerFunction("__pairs",this) ); |
340 if( h != null ) { | 339 } |
341 if( h instanceof LuanFunction ) { | 340 |
342 LuanFunction fn = (LuanFunction)h; | 341 private LuanFunction pairs(Luan luan,LuanFunction fn) throws LuanException { |
343 Object obj = Luan.first(fn.call(luan,this)); | 342 if( fn==null ) |
344 if( !(obj instanceof LuanFunction) ) | 343 return rawPairs(); |
345 throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) ); | 344 Object obj = Luan.first(fn.call(luan,this)); |
346 return (LuanFunction)obj; | 345 if( !(obj instanceof LuanFunction) ) |
347 } | 346 throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) ); |
348 throw new LuanException( "invalid type of metamethod __pairs: " + Luan.type(h) ); | 347 return (LuanFunction)obj; |
349 } | |
350 return rawPairs(); | |
351 } | 348 } |
352 | 349 |
353 private LuanFunction rawPairs() { | 350 private LuanFunction rawPairs() { |
354 return new LuanFunction() { | 351 return new LuanFunction() { |
355 final Iterator<Map.Entry> iter = rawIterator(); | 352 final Iterator<Map.Entry> iter = rawIterator(); |