Mercurial Hosting > luan
annotate src/luan/LuanTable.java @ 940:b77d631b9e28
remove scheduleTimeout() and cancelTimeout()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 11 Oct 2016 00:13:30 -0600 |
parents | 6b8ea0a9b7c9 |
children | 2443152dc2f1 |
rev | line source |
---|---|
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
1 package luan; |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
2 |
27
9de9be530625
minor
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
19
diff
changeset
|
3 import java.util.Iterator; |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
4 import java.util.ListIterator; |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
5 import java.util.Map; |
222
b76fcb72d97d
add AbstractLuanTable and improve HttpLuan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
221
diff
changeset
|
6 import java.util.AbstractMap; |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
7 import java.util.LinkedHashMap; |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
8 import java.util.List; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
9 import java.util.ArrayList; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
10 import java.util.Collections; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
11 import java.util.Comparator; |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
12 import java.util.Set; |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
13 import java.util.HashSet; |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
14 |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
15 |
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
16 public final class LuanTable implements LuanCloneable { |
434 | 17 private Map map = null; |
18 private List list = null; | |
48
64ecb7a3aad7
rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
47
diff
changeset
|
19 private LuanTable metatable = null; |
672
d3e5414bdf4c
better java permission handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
647
diff
changeset
|
20 public LuanJava java; |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
21 private LuanCloner cloner; |
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
22 |
408
1b38de2b1845
merge LuanTableImpl into LuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
406
diff
changeset
|
23 public LuanTable() {} |
364 | 24 |
434 | 25 public LuanTable(List list) { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
26 int n = list.size(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
27 for( int i=0; i<n; i++ ) { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
28 Object val = list.get(i); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
29 if( val != null ) |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
30 rawPut(i+1,val); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
31 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
32 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
33 |
434 | 34 public LuanTable(Map map) { |
35 for( Object stupid : map.entrySet() ) { | |
36 Map.Entry entry = (Map.Entry)stupid; | |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
37 Object key = entry.getKey(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
38 Object value = entry.getValue(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
39 if( key != null && value != null ) |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
40 rawPut(key,value); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
41 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
42 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
43 |
434 | 44 public LuanTable(Set set) { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
45 for( Object el : set ) { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
46 if( el != null ) |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
47 rawPut(el,Boolean.TRUE); |
118
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
48 } |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
49 } |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
50 |
425
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
51 public LuanTable(LuanTable tbl) { |
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
52 if( tbl.map != null && !tbl.map.isEmpty() ) |
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
53 this.map = new LinkedHashMap<Object,Object>(tbl.map); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
54 if( tbl.rawLength() > 0 ) |
425
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
55 this.list = new ArrayList<Object>(tbl.list); |
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
56 this.metatable = tbl.metatable; |
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
57 } |
0a2fb80907f9
minor LuanTable cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
423
diff
changeset
|
58 |
784
6a7c6879158d
revert shallowClone()
Franklin Schmidt <fschmidt@gmail.com>
parents:
782
diff
changeset
|
59 @Override public LuanTable shallowClone() { |
6a7c6879158d
revert shallowClone()
Franklin Schmidt <fschmidt@gmail.com>
parents:
782
diff
changeset
|
60 return new LuanTable(); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
61 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
62 |
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
63 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
64 check(); |
433
c6bcb8859b93
make LuanState.registry a Map;
Franklin Schmidt <fschmidt@gmail.com>
parents:
432
diff
changeset
|
65 LuanTable clone = (LuanTable)dc; |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
66 switch( cloner.type ) { |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
67 case COMPLETE: |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
68 deepenClone(clone,cloner); |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
69 return; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
70 case INCREMENTAL: |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
71 clone.cloner = cloner; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
72 clone.map = map; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
73 clone.list = list; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
74 clone.metatable = metatable; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
75 clone.java = java; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
76 return; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
77 } |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
78 } |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
79 |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
80 private void check() { |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
81 if( cloner != null ) { |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
82 deepenClone(this,cloner); |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
83 cloner = null; |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
84 } |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
85 } |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
86 |
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
87 private void deepenClone(LuanTable clone,LuanCloner cloner) { |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
88 if( map != null ) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
89 Map newMap = newMap(); |
434 | 90 for( Object stupid : map.entrySet() ) { |
91 Map.Entry entry = (Map.Entry)stupid; | |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
92 newMap.put( cloner.get(entry.getKey()), cloner.get(entry.getValue()) ); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
93 } |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
94 clone.map = newMap; |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
95 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
96 if( list != null ) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
97 List newList = new ArrayList<Object>(); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
98 for( Object obj : list ) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
99 newList.add(cloner.get(obj)); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
100 } |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
101 clone.list = newList; |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
102 } |
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
103 clone.metatable = (LuanTable)cloner.clone(metatable); |
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
104 clone.java = (LuanJava)cloner.clone(java); |
77
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
105 } |
4bf3d0c0b6b9
make LuanState cloneable
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
72
diff
changeset
|
106 |
408
1b38de2b1845
merge LuanTableImpl into LuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
406
diff
changeset
|
107 public boolean isList() { |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
108 return map==null || map.isEmpty(); |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
109 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
110 |
408
1b38de2b1845
merge LuanTableImpl into LuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
406
diff
changeset
|
111 public List<Object> asList() { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
112 check(); |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
113 return list!=null ? list : Collections.emptyList(); |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
114 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
115 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
116 public String toString(LuanState luan) throws LuanException { |
430
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
117 Object h = getHandler("__to_string"); |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
118 if( h == null ) |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
119 return rawToString(); |
647
8e8c30b72e9b
move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
120 LuanFunction fn = Luan.checkFunction(h); |
8e8c30b72e9b
move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
121 return Luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) ); |
430
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
122 } |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
123 |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
124 public String rawToString() { |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
125 return "table: " + Integer.toHexString(hashCode()); |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
126 } |
f28320fd671d
fix LuanTable.toString() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
428
diff
changeset
|
127 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
128 public Object get(LuanState luan,Object key) throws LuanException { |
426
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
129 Object value = rawGet(key); |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
130 if( value != null ) |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
131 return value; |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
132 Object h = getHandler("__index"); |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
133 if( h==null ) |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
134 return null; |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
135 if( h instanceof LuanFunction ) { |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
136 LuanFunction fn = (LuanFunction)h; |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
137 return Luan.first(fn.call(luan,new Object[]{this,key})); |
426
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
138 } |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
139 return luan.index(h,key); |
426
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
140 } |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
141 |
23a93c118042
fix LuanTable.get() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
425
diff
changeset
|
142 public Object rawGet(Object key) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
143 check(); |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
144 if( list != null ) { |
48
64ecb7a3aad7
rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
47
diff
changeset
|
145 Integer iT = Luan.asInteger(key); |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
146 if( iT != null ) { |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
147 int i = iT - 1; |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
148 if( i>=0 && i<list.size() ) |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
149 return list.get(i); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
150 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
151 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
152 if( map==null ) |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
153 return null; |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
533
diff
changeset
|
154 if( key instanceof Number && !(key instanceof Double) ) { |
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
533
diff
changeset
|
155 Number n = (Number)key; |
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
533
diff
changeset
|
156 key = Double.valueOf(n.doubleValue()); |
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
533
diff
changeset
|
157 } |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
158 return map.get(key); |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
159 } |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
160 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
161 public void put(LuanState luan,Object key,Object value) throws LuanException { |
428
df95199ca4c0
rename __newindex to __new_index
Franklin Schmidt <fschmidt@gmail.com>
parents:
427
diff
changeset
|
162 Object h = getHandler("__new_index"); |
427
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
163 if( h==null || rawGet(key)!=null ) { |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
164 rawPut(key,value); |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
165 return; |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
166 } |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
167 if( h instanceof LuanFunction ) { |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
168 LuanFunction fn = (LuanFunction)h; |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
169 fn.call(luan,new Object[]{this,key,value}); |
427
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
170 return; |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
171 } |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
172 if( h instanceof LuanTable ) { |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
173 LuanTable tbl = (LuanTable)h; |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
174 tbl.put(luan,key,value); |
427
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
175 return; |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
176 } |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
578
diff
changeset
|
177 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index"); |
427
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
178 } |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
179 |
dae264ad6a7b
fix LuanTable.put() to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
426
diff
changeset
|
180 public void rawPut(Object key,Object val) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
181 check(); |
48
64ecb7a3aad7
rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
47
diff
changeset
|
182 Integer iT = Luan.asInteger(key); |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
183 if( iT != null ) { |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
184 int i = iT - 1; |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
185 if( list != null || i == 0 ) { |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
186 if( i == list().size() ) { |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
187 if( val != null ) { |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
188 list.add(val); |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
189 mapToList(); |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
190 } |
224
05eb2837ddbf
change LuanTable.put() to not return old value
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
222
diff
changeset
|
191 return; |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
192 } else if( i>=0 && i<list.size() ) { |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
193 list.set(i,val); |
118
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
194 if( val == null ) { |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
195 listToMap(i); |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
196 } |
224
05eb2837ddbf
change LuanTable.put() to not return old value
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
222
diff
changeset
|
197 return; |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
198 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
199 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
200 } |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
533
diff
changeset
|
201 if( map==null ) |
284
8870840251ea
use LinkedHashMap in LuanTable to preserve order
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
233
diff
changeset
|
202 map = newMap(); |
46
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
203 if( key instanceof Number && !(key instanceof Double) ) { |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
204 Number n = (Number)key; |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
205 key = Double.valueOf(n.doubleValue()); |
a443637829c1
remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
44
diff
changeset
|
206 } |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
207 if( val == null ) { |
224
05eb2837ddbf
change LuanTable.put() to not return old value
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
222
diff
changeset
|
208 map.remove(key); |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
209 } else { |
224
05eb2837ddbf
change LuanTable.put() to not return old value
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
222
diff
changeset
|
210 map.put(key,val); |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
211 } |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
212 } |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
213 |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
214 private void mapToList() { |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
215 if( map != null ) { |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
216 while(true) { |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
217 Object v = map.remove(Double.valueOf(list.size()+1)); |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
218 if( v == null ) |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
219 break; |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
220 list.add(v); |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
221 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
222 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
223 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
224 |
118
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
225 private void listToMap(int from) { |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
226 if( list != null ) { |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
227 while( list.size() > from ) { |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
228 int i = list.size() - 1; |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
229 Object v = list.remove(i); |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
230 if( v != null ) { |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
231 if( map==null ) |
284
8870840251ea
use LinkedHashMap in LuanTable to preserve order
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
233
diff
changeset
|
232 map = newMap(); |
118
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
233 map.put(i+1,v); |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
234 } |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
235 } |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
236 } |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
237 } |
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
108
diff
changeset
|
238 |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
239 private List<Object> list() { |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
240 if( list == null ) { |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
241 list = new ArrayList<Object>(); |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
242 mapToList(); |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
243 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
244 return list; |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
245 } |
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
246 |
431 | 247 public void rawInsert(int pos,Object value) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
248 check(); |
108
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
88
diff
changeset
|
249 if( value==null ) |
222
b76fcb72d97d
add AbstractLuanTable and improve HttpLuan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
221
diff
changeset
|
250 throw new IllegalArgumentException("can't insert a nil value"); |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
251 list().add(pos-1,value); |
108
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
88
diff
changeset
|
252 mapToList(); |
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
88
diff
changeset
|
253 } |
3c404a296995
make Package module more standard;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
88
diff
changeset
|
254 |
431 | 255 public Object rawRemove(int pos) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
256 check(); |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
257 return list().remove(pos-1); |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
258 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
259 |
431 | 260 public void rawSort(Comparator<Object> cmp) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
261 check(); |
47
659c7139e903
better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
46
diff
changeset
|
262 Collections.sort(list(),cmp); |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
263 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
264 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
265 public int length(LuanState luan) throws LuanException { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
266 Object h = getHandler("__len"); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
267 if( h != null ) { |
647
8e8c30b72e9b
move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
646
diff
changeset
|
268 LuanFunction fn = Luan.checkFunction(h); |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
269 return (Integer)Luan.first(fn.call(luan,new Object[]{this})); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
270 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
271 return rawLength(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
272 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
273 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
274 public int rawLength() { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
275 check(); |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
276 return list==null ? 0 : list.size(); |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
277 } |
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
278 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
279 public Iterable<Map.Entry<Object,Object>> iterable(LuanState luan) throws LuanException { |
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
280 final Iterator<Map.Entry<Object,Object>> iter = iterator(luan); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
281 return new Iterable<Map.Entry<Object,Object>>() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
282 public Iterator<Map.Entry<Object,Object>> iterator() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
283 return iter; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
284 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
285 }; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
286 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
287 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
288 public Iterable<Map.Entry<Object,Object>> rawIterable() throws LuanException { |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
289 final Iterator<Map.Entry<Object,Object>> iter = rawIterator(); |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
290 return new Iterable<Map.Entry<Object,Object>>() { |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
291 public Iterator<Map.Entry<Object,Object>> iterator() { |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
292 return iter; |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
293 } |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
294 }; |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
295 } |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
296 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
297 public Iterator<Map.Entry<Object,Object>> iterator(final LuanState luan) throws LuanException { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
298 if( getHandler("__pairs") == null ) |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
299 return rawIterator(); |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
300 final LuanFunction fn = pairs(luan); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
301 return new Iterator<Map.Entry<Object,Object>>() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
302 private Map.Entry<Object,Object> next = getNext(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
303 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
304 private Map.Entry<Object,Object> getNext() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
305 try { |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
306 Object obj = fn.call(luan); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
307 if( obj==null ) |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
308 return null; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
309 Object[] a = (Object[])obj; |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
434
diff
changeset
|
310 if( a.length == 0 || a[0]==null ) |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
311 return null; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
312 return new AbstractMap.SimpleEntry<Object,Object>(a[0],a[1]); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
313 } catch(LuanException e) { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
314 throw new LuanRuntimeException(e); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
315 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
316 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
317 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
318 public boolean hasNext() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
319 return next != null; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
320 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
321 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
322 public Map.Entry<Object,Object> next() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
323 Map.Entry<Object,Object> rtn = next; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
324 next = getNext(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
325 return rtn; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
326 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
327 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
328 public void remove() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
329 throw new UnsupportedOperationException(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
330 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
331 }; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
332 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
333 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
334 public LuanFunction pairs(LuanState luan) throws LuanException { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
335 Object h = getHandler("__pairs"); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
336 if( h != null ) { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
337 if( h instanceof LuanFunction ) { |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
338 LuanFunction fn = (LuanFunction)h; |
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
339 Object obj = Luan.first(fn.call(luan,new Object[]{this})); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
340 if( !(obj instanceof LuanFunction) ) |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
578
diff
changeset
|
341 throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) ); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
342 return (LuanFunction)obj; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
343 } |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
578
diff
changeset
|
344 throw new LuanException( "invalid type of metamethod __pairs: " + Luan.type(h) ); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
345 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
346 return rawPairs(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
347 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
348 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
349 private LuanFunction rawPairs() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
350 return new LuanFunction() { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
351 final Iterator<Map.Entry<Object,Object>> iter = rawIterator(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
352 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
353 @Override public Object[] call(LuanState luan,Object[] args) { |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
354 if( !iter.hasNext() ) |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
355 return LuanFunction.NOTHING; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
356 Map.Entry<Object,Object> entry = iter.next(); |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
357 return new Object[]{entry.getKey(),entry.getValue()}; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
358 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
359 }; |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
360 } |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
361 |
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
362 public Iterator<Map.Entry<Object,Object>> rawIterator() { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
363 check(); |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
364 if( list == null ) { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
365 if( map == null ) |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
366 return Collections.<Map.Entry<Object,Object>>emptyList().iterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
367 return map.entrySet().iterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
368 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
369 if( map == null ) |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
370 return listIterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
371 return new Iterator<Map.Entry<Object,Object>>() { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
372 Iterator<Map.Entry<Object,Object>> iter = listIterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
373 boolean isList = true; |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
374 |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
375 public boolean hasNext() { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
376 boolean b = iter.hasNext(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
377 if( !b && isList ) { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
378 iter = map.entrySet().iterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
379 isList = false; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
380 b = iter.hasNext(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
381 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
382 return b; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
383 } |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
384 |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
385 public Map.Entry<Object,Object> next() { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
386 return iter.next(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
387 } |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
388 |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
389 public void remove() { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
390 throw new UnsupportedOperationException(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
391 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
392 }; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
393 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
394 |
221
ec016471c6eb
make LuanTable an interface
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
202
diff
changeset
|
395 private Iterator<Map.Entry<Object,Object>> listIterator() { |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
396 if( list == null ) |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
397 return Collections.<Map.Entry<Object,Object>>emptyList().iterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
398 final ListIterator iter = list.listIterator(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
399 return new Iterator<Map.Entry<Object,Object>>() { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
400 |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
401 public boolean hasNext() { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
402 return iter.hasNext(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
403 } |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
404 |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
405 public Map.Entry<Object,Object> next() { |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
406 Integer key = iter.nextIndex()+1; |
222
b76fcb72d97d
add AbstractLuanTable and improve HttpLuan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
221
diff
changeset
|
407 return new AbstractMap.SimpleEntry<Object,Object>(key,iter.next()); |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
408 } |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
409 |
44
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
410 public void remove() { |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
411 throw new UnsupportedOperationException(); |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
412 } |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
413 }; |
57054fa43189
implement table lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
35
diff
changeset
|
414 } |
221
ec016471c6eb
make LuanTable an interface
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
202
diff
changeset
|
415 |
431 | 416 public LuanTable rawSubList(int from,int to) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
417 check(); |
784
6a7c6879158d
revert shallowClone()
Franklin Schmidt <fschmidt@gmail.com>
parents:
782
diff
changeset
|
418 LuanTable tbl = new LuanTable(); |
221
ec016471c6eb
make LuanTable an interface
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
202
diff
changeset
|
419 tbl.list = new ArrayList<Object>(list().subList(from-1,to-1)); |
ec016471c6eb
make LuanTable an interface
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
202
diff
changeset
|
420 return tbl; |
27
9de9be530625
minor
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
19
diff
changeset
|
421 } |
9de9be530625
minor
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
19
diff
changeset
|
422 |
408
1b38de2b1845
merge LuanTableImpl into LuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
406
diff
changeset
|
423 public LuanTable getMetatable() { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
424 check(); |
35
e51906de0f11
implement metatables
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
27
diff
changeset
|
425 return metatable; |
e51906de0f11
implement metatables
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
27
diff
changeset
|
426 } |
e51906de0f11
implement metatables
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
27
diff
changeset
|
427 |
408
1b38de2b1845
merge LuanTableImpl into LuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
406
diff
changeset
|
428 public void setMetatable(LuanTable metatable) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
429 check(); |
35
e51906de0f11
implement metatables
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
27
diff
changeset
|
430 this.metatable = metatable; |
e51906de0f11
implement metatables
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
27
diff
changeset
|
431 } |
300
9fb523472035
add java() to control java access
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
284
diff
changeset
|
432 |
419
8fbb961aabd5
improve repr() to check metamethod recursively
Franklin Schmidt <fschmidt@gmail.com>
parents:
414
diff
changeset
|
433 public Object getHandler(String op) { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
434 check(); |
432
d9df6d6cb927
finish fixing LuanTable to use metatables
Franklin Schmidt <fschmidt@gmail.com>
parents:
431
diff
changeset
|
435 return metatable==null ? null : metatable.rawGet(op); |
419
8fbb961aabd5
improve repr() to check metamethod recursively
Franklin Schmidt <fschmidt@gmail.com>
parents:
414
diff
changeset
|
436 } |
8fbb961aabd5
improve repr() to check metamethod recursively
Franklin Schmidt <fschmidt@gmail.com>
parents:
414
diff
changeset
|
437 |
431 | 438 private Map<Object,Object> newMap() { |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
439 return new LinkedHashMap<Object,Object>(); |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
440 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
441 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
442 public boolean isSet(LuanState luan) throws LuanException { |
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
443 for( Map.Entry<Object,Object> entry : iterable(luan) ) { |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
444 if( !entry.getValue().equals(Boolean.TRUE) ) |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
445 return false; |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
446 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
447 return true; |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
448 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
449 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
450 public Set<Object> asSet(LuanState luan) throws LuanException { |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
451 Set<Object> set = new HashSet<Object>(); |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
452 for( Map.Entry<Object,Object> entry : iterable(luan) ) { |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
453 set.add(entry.getKey()); |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
454 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
455 return set; |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
456 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
457 |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
458 public Map<Object,Object> asMap(LuanState luan) throws LuanException { |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
459 Map<Object,Object> map = newMap(); |
575
7c3ad6db8ac3
make LuanState.JAVA private
Franklin Schmidt <fschmidt@gmail.com>
parents:
572
diff
changeset
|
460 for( Map.Entry<Object,Object> entry : iterable(luan) ) { |
406
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
461 map.put(entry.getKey(),entry.getValue()); |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
462 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
463 return map; |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
464 } |
9321a33b9b1c
remove AbstractLuanTable
Franklin Schmidt <fschmidt@gmail.com>
parents:
397
diff
changeset
|
465 |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
434
diff
changeset
|
466 public void rawClear() { |
785
d69d3c51c44e
more work on incremental cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
784
diff
changeset
|
467 check(); |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
434
diff
changeset
|
468 map = null; |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
434
diff
changeset
|
469 list = null; |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
434
diff
changeset
|
470 } |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
434
diff
changeset
|
471 |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
472 } |