comparison core/src/luan/LuanTable.java @ 575:7c3ad6db8ac3

make LuanState.JAVA private
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jul 2015 18:34:31 -0600
parents f1601a4ce1aa
children 60c549d43988
comparison
equal deleted inserted replaced
574:6cc2f047019b 575:7c3ad6db8ac3
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.Collections; 10 import java.util.Collections;
11 import java.util.Comparator; 11 import java.util.Comparator;
12 import java.util.Set; 12 import java.util.Set;
13 import java.util.HashSet; 13 import java.util.HashSet;
14 import java.util.IdentityHashMap;
15 import java.util.regex.Pattern;
16 import luan.modules.StringLuan;
17 import luan.modules.BinaryLuan;
18 import luan.modules.JavaLuan;
19 14
20 15
21 public final class LuanTable implements DeepCloneable { 16 public final class LuanTable implements DeepCloneable {
22 private Map map = null; 17 private Map map = null;
23 private List list = null; 18 private List list = null;
90 85
91 public List<Object> asList() { 86 public List<Object> asList() {
92 return list!=null ? list : Collections.emptyList(); 87 return list!=null ? list : Collections.emptyList();
93 } 88 }
94 89
95 public String toString(LuanBit bit) throws LuanException { 90 public String toString(LuanState luan) throws LuanException {
96 Object h = getHandler("__to_string"); 91 Object h = getHandler("__to_string");
97 if( h == null ) 92 if( h == null )
98 return rawToString(); 93 return rawToString();
99 if( h instanceof LuanMeta ) { 94 if( h instanceof LuanMeta ) {
100 LuanMeta meta = (LuanMeta)h; 95 LuanMeta meta = (LuanMeta)h;
101 return meta.__to_string(bit.luan,this); 96 return meta.__to_string(luan,this);
102 } 97 }
103 LuanFunction fn = bit.checkFunction(h); 98 LuanFunction fn = luan.checkFunction(h);
104 return bit.checkString( Luan.first( bit.call(fn,"__to_string",new Object[]{this}) ) ); 99 return luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) );
105 } 100 }
106 101
107 public String rawToString() { 102 public String rawToString() {
108 return "table: " + Integer.toHexString(hashCode()); 103 return "table: " + Integer.toHexString(hashCode());
109 } 104 }
110 105
111 public Object get(LuanBit bit,Object key) throws LuanException { 106 public Object get(LuanState luan,Object key) throws LuanException {
112 Object value = rawGet(key); 107 Object value = rawGet(key);
113 if( value != null ) 108 if( value != null )
114 return value; 109 return value;
115 Object h = getHandler("__index"); 110 Object h = getHandler("__index");
116 if( h==null ) 111 if( h==null )
117 return null; 112 return null;
118 if( h instanceof LuanFunction ) { 113 if( h instanceof LuanFunction ) {
119 LuanFunction fn = (LuanFunction)h; 114 LuanFunction fn = (LuanFunction)h;
120 return Luan.first(bit.call(fn,"__index",new Object[]{this,key})); 115 return Luan.first(fn.call(luan,new Object[]{this,key}));
121 } 116 }
122 if( h instanceof LuanMeta ) { 117 if( h instanceof LuanMeta ) {
123 LuanMeta meta = (LuanMeta)h; 118 LuanMeta meta = (LuanMeta)h;
124 return meta.__index(bit.luan,this,key); 119 return meta.__index(luan,this,key);
125 } 120 }
126 return index(bit,h,key); 121 return luan.index(h,key);
127 }
128
129 public static Object index(LuanBit bit,Object obj,Object key) throws LuanException {
130 if( obj instanceof LuanTable ) {
131 LuanTable tbl = (LuanTable)obj;
132 return tbl.get(bit,key);
133 }
134 if( obj != null && bit.luan.hasJava() )
135 return JavaLuan.__index(bit,obj,key,false);
136 else if( bit.el==null )
137 throw bit.exception( "attempt to index a " + Luan.type(obj) + " value" );
138 else
139 throw bit.exception( "attempt to index a " + Luan.type(obj) + " value in '"+bit.el.text()+"'" );
140 } 122 }
141 123
142 public Object rawGet(Object key) { 124 public Object rawGet(Object key) {
143 if( list != null ) { 125 if( list != null ) {
144 Integer iT = Luan.asInteger(key); 126 Integer iT = Luan.asInteger(key);
155 key = Double.valueOf(n.doubleValue()); 137 key = Double.valueOf(n.doubleValue());
156 } 138 }
157 return map.get(key); 139 return map.get(key);
158 } 140 }
159 141
160 public void put(LuanBit bit,Object key,Object value) throws LuanException { 142 public void put(LuanState luan,Object key,Object value) throws LuanException {
161 Object h = getHandler("__new_index"); 143 Object h = getHandler("__new_index");
162 if( h==null || rawGet(key)!=null ) { 144 if( h==null || rawGet(key)!=null ) {
163 rawPut(key,value); 145 rawPut(key,value);
164 return; 146 return;
165 } 147 }
166 if( h instanceof LuanFunction ) { 148 if( h instanceof LuanFunction ) {
167 LuanFunction fn = (LuanFunction)h; 149 LuanFunction fn = (LuanFunction)h;
168 bit.call(fn,"__new_index",new Object[]{this,key,value}); 150 fn.call(luan,new Object[]{this,key,value});
169 return; 151 return;
170 } 152 }
171 if( h instanceof LuanMeta ) { 153 if( h instanceof LuanMeta ) {
172 LuanMeta meta = (LuanMeta)h; 154 LuanMeta meta = (LuanMeta)h;
173 meta.__new_index(bit.luan,this,key,value); 155 meta.__new_index(luan,this,key,value);
174 return; 156 return;
175 } 157 }
176 if( h instanceof LuanTable ) { 158 if( h instanceof LuanTable ) {
177 LuanTable tbl = (LuanTable)h; 159 LuanTable tbl = (LuanTable)h;
178 tbl.put(bit,key,value); 160 tbl.put(luan,key,value);
179 return; 161 return;
180 } 162 }
181 throw bit.exception("invalid type "+Luan.type(h)+" for metamethod __new_index"); 163 throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __new_index");
182 } 164 }
183 165
184 public void rawPut(Object key,Object val) { 166 public void rawPut(Object key,Object val) {
185 Integer iT = Luan.asInteger(key); 167 Integer iT = Luan.asInteger(key);
186 if( iT != null ) { 168 if( iT != null ) {
260 242
261 public void rawSort(Comparator<Object> cmp) { 243 public void rawSort(Comparator<Object> cmp) {
262 Collections.sort(list(),cmp); 244 Collections.sort(list(),cmp);
263 } 245 }
264 246
265 public int length(LuanBit bit) throws LuanException { 247 public int length(LuanState luan) throws LuanException {
266 Object h = getHandler("__len"); 248 Object h = getHandler("__len");
267 if( h != null ) { 249 if( h != null ) {
268 LuanFunction fn = bit.checkFunction(h); 250 LuanFunction fn = luan.checkFunction(h);
269 return (Integer)Luan.first(bit.call(fn,"__len",new Object[]{this})); 251 return (Integer)Luan.first(fn.call(luan,new Object[]{this}));
270 } 252 }
271 return rawLength(); 253 return rawLength();
272 } 254 }
273 255
274 public int rawLength() { 256 public int rawLength() {
275 return list==null ? 0 : list.size(); 257 return list==null ? 0 : list.size();
276 } 258 }
277 259
278 public Iterable<Map.Entry<Object,Object>> iterable(LuanBit bit) throws LuanException { 260 public Iterable<Map.Entry<Object,Object>> iterable(LuanState luan) throws LuanException {
279 final Iterator<Map.Entry<Object,Object>> iter = iterator(bit); 261 final Iterator<Map.Entry<Object,Object>> iter = iterator(luan);
280 return new Iterable<Map.Entry<Object,Object>>() { 262 return new Iterable<Map.Entry<Object,Object>>() {
281 public Iterator<Map.Entry<Object,Object>> iterator() { 263 public Iterator<Map.Entry<Object,Object>> iterator() {
282 return iter; 264 return iter;
283 } 265 }
284 }; 266 };
291 return iter; 273 return iter;
292 } 274 }
293 }; 275 };
294 } 276 }
295 277
296 public Iterator<Map.Entry<Object,Object>> iterator(final LuanBit bit) throws LuanException { 278 public Iterator<Map.Entry<Object,Object>> iterator(final LuanState luan) throws LuanException {
297 if( getHandler("__pairs") == null ) 279 if( getHandler("__pairs") == null )
298 return rawIterator(); 280 return rawIterator();
299 final LuanFunction fn = pairs(bit); 281 final LuanFunction fn = pairs(luan);
300 return new Iterator<Map.Entry<Object,Object>>() { 282 return new Iterator<Map.Entry<Object,Object>>() {
301 private Map.Entry<Object,Object> next = getNext(); 283 private Map.Entry<Object,Object> next = getNext();
302 284
303 private Map.Entry<Object,Object> getNext() { 285 private Map.Entry<Object,Object> getNext() {
304 try { 286 try {
305 Object obj = bit.call(fn,null,LuanFunction.NOTHING); 287 Object obj = fn.call(luan);
306 if( obj==null ) 288 if( obj==null )
307 return null; 289 return null;
308 Object[] a = (Object[])obj; 290 Object[] a = (Object[])obj;
309 if( a.length == 0 || a[0]==null ) 291 if( a.length == 0 || a[0]==null )
310 return null; 292 return null;
328 throw new UnsupportedOperationException(); 310 throw new UnsupportedOperationException();
329 } 311 }
330 }; 312 };
331 } 313 }
332 314
333 public LuanFunction pairs(LuanBit bit) throws LuanException { 315 public LuanFunction pairs(LuanState luan) throws LuanException {
334 Object h = getHandler("__pairs"); 316 Object h = getHandler("__pairs");
335 if( h != null ) { 317 if( h != null ) {
336 if( h instanceof LuanFunction ) { 318 if( h instanceof LuanFunction ) {
337 Object obj = Luan.first(bit.call((LuanFunction)h,"__pairs",new Object[]{this})); 319 LuanFunction fn = (LuanFunction)h;
320 Object obj = Luan.first(fn.call(luan,new Object[]{this}));
338 if( !(obj instanceof LuanFunction) ) 321 if( !(obj instanceof LuanFunction) )
339 throw bit.exception( "metamethod __pairs should return function but returned " + Luan.type(obj) ); 322 throw luan.exception( "metamethod __pairs should return function but returned " + Luan.type(obj) );
340 return (LuanFunction)obj; 323 return (LuanFunction)obj;
341 } 324 }
342 if( h instanceof LuanMeta ) { 325 if( h instanceof LuanMeta ) {
343 LuanMeta meta = (LuanMeta)h; 326 LuanMeta meta = (LuanMeta)h;
344 return meta.__pairs(bit.luan,this); 327 return meta.__pairs(luan,this);
345 } 328 }
346 throw bit.exception( "invalid type of metamethod __pairs: " + Luan.type(h) ); 329 throw luan.exception( "invalid type of metamethod __pairs: " + Luan.type(h) );
347 } 330 }
348 return rawPairs(); 331 return rawPairs();
349 } 332 }
350 333
351 private LuanFunction rawPairs() { 334 private LuanFunction rawPairs() {
434 417
435 private Map<Object,Object> newMap() { 418 private Map<Object,Object> newMap() {
436 return new LinkedHashMap<Object,Object>(); 419 return new LinkedHashMap<Object,Object>();
437 } 420 }
438 421
439 public boolean isSet(LuanBit bit) throws LuanException { 422 public boolean isSet(LuanState luan) throws LuanException {
440 for( Map.Entry<Object,Object> entry : iterable(bit) ) { 423 for( Map.Entry<Object,Object> entry : iterable(luan) ) {
441 if( !entry.getValue().equals(Boolean.TRUE) ) 424 if( !entry.getValue().equals(Boolean.TRUE) )
442 return false; 425 return false;
443 } 426 }
444 return true; 427 return true;
445 } 428 }
446 429
447 public Set<Object> asSet(LuanBit bit) throws LuanException { 430 public Set<Object> asSet(LuanState luan) throws LuanException {
448 Set<Object> set = new HashSet<Object>(); 431 Set<Object> set = new HashSet<Object>();
449 for( Map.Entry<Object,Object> entry : iterable(bit) ) { 432 for( Map.Entry<Object,Object> entry : iterable(luan) ) {
450 set.add(entry.getKey()); 433 set.add(entry.getKey());
451 } 434 }
452 return set; 435 return set;
453 } 436 }
454 437
455 public Map<Object,Object> asMap(LuanBit bit) throws LuanException { 438 public Map<Object,Object> asMap(LuanState luan) throws LuanException {
456 Map<Object,Object> map = newMap(); 439 Map<Object,Object> map = newMap();
457 for( Map.Entry<Object,Object> entry : iterable(bit) ) { 440 for( Map.Entry<Object,Object> entry : iterable(luan) ) {
458 map.put(entry.getKey(),entry.getValue()); 441 map.put(entry.getKey(),entry.getValue());
459 } 442 }
460 return map; 443 return map;
461 } 444 }
462 445