Mercurial Hosting > luan
comparison src/luan/LuanTable.java @ 1335:e0cf0d108a77
major cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Feb 2019 03:10:45 -0700 |
parents | 25746915a241 |
children | 2024d23ddd64 |
comparison
equal
deleted
inserted
replaced
1334:c88b486a9511 | 1335:e0cf0d108a77 |
---|---|
71 check(); | 71 check(); |
72 LuanTable clone = (LuanTable)dc; | 72 LuanTable clone = (LuanTable)dc; |
73 clone.security = security; | 73 clone.security = security; |
74 switch( cloner.type ) { | 74 switch( cloner.type ) { |
75 case COMPLETE: | 75 case COMPLETE: |
76 deepenClone(clone,cloner); | 76 completeClone(clone,cloner); |
77 return; | 77 return; |
78 case INCREMENTAL: | 78 case INCREMENTAL: |
79 clone.cloner = cloner; | 79 clone.cloner = cloner; |
80 clone.map = map; | 80 clone.map = map; |
81 clone.list = list; | 81 clone.list = list; |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 private void check() { | 88 private void check() { |
89 if( cloner != null ) { | 89 if( cloner != null ) { |
90 deepenClone(this,cloner); | 90 completeClone(this,cloner); |
91 cloner = null; | 91 cloner = null; |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 private void deepenClone(LuanTable clone,LuanCloner cloner) { | 95 public Luan luan() { |
96 check(); | |
97 return luan; | |
98 } | |
99 | |
100 private void completeClone(LuanTable clone,LuanCloner cloner) { | |
96 clone.luan = (Luan)cloner.clone(luan); | 101 clone.luan = (Luan)cloner.clone(luan); |
97 if( map != null ) { | 102 if( map != null ) { |
98 Map newMap = newMap(); | 103 Map newMap = newMap(); |
99 for( Object stupid : map.entrySet() ) { | 104 for( Object stupid : map.entrySet() ) { |
100 Map.Entry entry = (Map.Entry)stupid; | 105 Map.Entry entry = (Map.Entry)stupid; |
130 public String toStringLuan() throws LuanException { | 135 public String toStringLuan() throws LuanException { |
131 Object h = getHandler("__to_string"); | 136 Object h = getHandler("__to_string"); |
132 if( h == null ) | 137 if( h == null ) |
133 return rawToString(); | 138 return rawToString(); |
134 LuanFunction fn = Luan.checkFunction(h); | 139 LuanFunction fn = Luan.checkFunction(h); |
135 return Luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) ); | 140 return Luan.checkString( Luan.first( fn.call(this) ) ); |
136 } | 141 } |
137 | 142 |
138 public String rawToString() { | 143 public String rawToString() { |
139 return "table: " + Integer.toHexString(hashCode()); | 144 return "table: " + Integer.toHexString(hashCode()); |
140 } | 145 } |
146 Object h = getHandler("__index"); | 151 Object h = getHandler("__index"); |
147 if( h==null ) | 152 if( h==null ) |
148 return null; | 153 return null; |
149 if( h instanceof LuanFunction ) { | 154 if( h instanceof LuanFunction ) { |
150 LuanFunction fn = (LuanFunction)h; | 155 LuanFunction fn = (LuanFunction)h; |
151 return Luan.first(fn.call(luan,new Object[]{this,key})); | 156 return Luan.first(fn.call(this,key)); |
152 } | 157 } |
153 return luan.index(h,key); | 158 return luan.index(h,key); |
154 } | 159 } |
155 | 160 |
156 public Object rawGet(Object key) { | 161 public Object rawGet(Object key) { |
178 rawPut(key,value); | 183 rawPut(key,value); |
179 return; | 184 return; |
180 } | 185 } |
181 if( h instanceof LuanFunction ) { | 186 if( h instanceof LuanFunction ) { |
182 LuanFunction fn = (LuanFunction)h; | 187 LuanFunction fn = (LuanFunction)h; |
183 fn.call(luan,new Object[]{this,key,value}); | 188 fn.call(this,key,value); |
184 return; | 189 return; |
185 } | 190 } |
186 if( h instanceof LuanTable ) { | 191 if( h instanceof LuanTable ) { |
187 LuanTable tbl = (LuanTable)h; | 192 LuanTable tbl = (LuanTable)h; |
188 tbl.put(key,value); | 193 tbl.put(key,value); |
284 | 289 |
285 public int length() throws LuanException { | 290 public int length() throws LuanException { |
286 Object h = getHandler("__len"); | 291 Object h = getHandler("__len"); |
287 if( h != null ) { | 292 if( h != null ) { |
288 LuanFunction fn = Luan.checkFunction(h); | 293 LuanFunction fn = Luan.checkFunction(h); |
289 return (Integer)Luan.first(fn.call(luan,new Object[]{this})); | 294 return (Integer)Luan.first(fn.call(this)); |
290 } | 295 } |
291 return rawLength(); | 296 return rawLength(); |
292 } | 297 } |
293 | 298 |
294 public int rawLength() { | 299 public int rawLength() { |
321 return new Iterator<Map.Entry>() { | 326 return new Iterator<Map.Entry>() { |
322 private Map.Entry<Object,Object> next = getNext(); | 327 private Map.Entry<Object,Object> next = getNext(); |
323 | 328 |
324 private Map.Entry<Object,Object> getNext() { | 329 private Map.Entry<Object,Object> getNext() { |
325 try { | 330 try { |
326 Object obj = fn.call(luan); | 331 Object obj = fn.call(); |
327 if( obj==null ) | 332 if( obj==null ) |
328 return null; | 333 return null; |
329 Object[] a = (Object[])obj; | 334 Object[] a = (Object[])obj; |
330 if( a.length == 0 || a[0]==null ) | 335 if( a.length == 0 || a[0]==null ) |
331 return null; | 336 return null; |
354 public LuanFunction pairs() throws LuanException { | 359 public LuanFunction pairs() throws LuanException { |
355 Object h = getHandler("__pairs"); | 360 Object h = getHandler("__pairs"); |
356 if( h != null ) { | 361 if( h != null ) { |
357 if( h instanceof LuanFunction ) { | 362 if( h instanceof LuanFunction ) { |
358 LuanFunction fn = (LuanFunction)h; | 363 LuanFunction fn = (LuanFunction)h; |
359 Object obj = Luan.first(fn.call(luan,new Object[]{this})); | 364 Object obj = Luan.first(fn.call(this)); |
360 if( !(obj instanceof LuanFunction) ) | 365 if( !(obj instanceof LuanFunction) ) |
361 throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) ); | 366 throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) ); |
362 return (LuanFunction)obj; | 367 return (LuanFunction)obj; |
363 } | 368 } |
364 throw new LuanException( "invalid type of metamethod __pairs: " + Luan.type(h) ); | 369 throw new LuanException( "invalid type of metamethod __pairs: " + Luan.type(h) ); |
365 } | 370 } |
366 return rawPairs(); | 371 return rawPairs(); |
367 } | 372 } |
368 | 373 |
369 private LuanFunction rawPairs() { | 374 private LuanFunction rawPairs() { |
370 return new LuanFunction() { | 375 return new LuanFunction(false) { // ??? |
371 final Iterator<Map.Entry> iter = rawIterator(); | 376 final Iterator<Map.Entry> iter = rawIterator(); |
372 | 377 |
373 @Override public Object[] call(Luan luan,Object[] args) { | 378 @Override public Object[] call(Object[] args) { |
374 if( !iter.hasNext() ) | 379 if( !iter.hasNext() ) |
375 return LuanFunction.NOTHING; | 380 return LuanFunction.NOTHING; |
376 Map.Entry<Object,Object> entry = iter.next(); | 381 Map.Entry<Object,Object> entry = iter.next(); |
377 return new Object[]{entry.getKey(),entry.getValue()}; | 382 return new Object[]{entry.getKey(),entry.getValue()}; |
378 } | 383 } |
515 | 520 |
516 protected void finalize() throws Throwable { | 521 protected void finalize() throws Throwable { |
517 Object h = getHandler("__gc"); | 522 Object h = getHandler("__gc"); |
518 if( h != null ) { | 523 if( h != null ) { |
519 LuanFunction fn = Luan.checkFunction(h); | 524 LuanFunction fn = Luan.checkFunction(h); |
520 fn.call(luan,new Object[]{this}); | 525 fn.call(this); |
521 } | 526 } |
522 super.finalize(); | 527 super.finalize(); |
523 } | 528 } |
524 | 529 |
525 public Object call(String fnName,Object... args) throws LuanException { | 530 public LuanFunction fn(String fnName) throws LuanException { |
526 LuanFunction fn = (LuanFunction)get(fnName); | 531 return (LuanFunction)get(fnName); |
527 return fn.call(luan,args); | |
528 } | 532 } |
529 | 533 |
530 public static void setSecurity(LuanTable tbl,String security) { | 534 public static void setSecurity(LuanTable tbl,String security) { |
531 tbl.security = security; | 535 tbl.security = security; |
532 } | 536 } |