Mercurial Hosting > luan
comparison core/src/luan/LuanMeta.java @ 413:8937263f59f6
add __pairs;
add back HttpServicer.get_parameter_values;
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 29 Apr 2015 19:01:18 -0600 |
parents | 1b38de2b1845 |
children | ce8e19567911 |
comparison
equal
deleted
inserted
replaced
412:094f37c5e6f9 | 413:8937263f59f6 |
---|---|
1 package luan; | 1 package luan; |
2 | 2 |
3 import java.util.Map; | 3 import java.util.Map; |
4 import java.util.Iterator; | |
5 import java.util.Set; | |
6 import java.util.HashSet; | |
4 | 7 |
5 | 8 |
6 public abstract class LuanMeta /*implements Iterable<Map.Entry<Object,Object>>*/ { | 9 public abstract class LuanMeta { |
7 | 10 |
8 public abstract Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException; | 11 public abstract Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException; |
9 | 12 |
10 // public abstract LuanFunction __pairs(LuanState luan,LuanTable tbl) throws LuanException; | 13 protected abstract Iterator keys(); |
14 | |
15 public LuanFunction __pairs(final LuanState luan,final LuanTable tbl) { | |
16 return new LuanFunction() { | |
17 final Iterator<Map.Entry<Object,Object>> iter1 = tbl.iterator(); | |
18 final Iterator<Object> iter2 = keys(); | |
19 final Set<Object> set = new HashSet<Object>(); | |
20 | |
21 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { | |
22 if( iter1.hasNext() ) { | |
23 Map.Entry<Object,Object> entry = iter1.next(); | |
24 Object key = entry.getKey(); | |
25 set.add(key); | |
26 return new Object[]{key,entry.getValue()}; | |
27 } | |
28 while( iter2.hasNext() ) { | |
29 Object key = iter2.next(); | |
30 if( set.add(key) ) { | |
31 Object value = __index(luan,tbl,key); | |
32 return new Object[]{key,value}; | |
33 } | |
34 } | |
35 return LuanFunction.NOTHING; | |
36 } | |
37 }; | |
38 } | |
11 | 39 |
12 public boolean canNewindex() { | 40 public boolean canNewindex() { |
13 return false; | 41 return false; |
14 } | 42 } |
15 | 43 |
17 throw new UnsupportedOperationException(); | 45 throw new UnsupportedOperationException(); |
18 } | 46 } |
19 | 47 |
20 public LuanTable newMetatable() { | 48 public LuanTable newMetatable() { |
21 LuanTable mt = new LuanTable(); | 49 LuanTable mt = new LuanTable(); |
22 /* | |
23 try { | |
24 mt.put( "__index", new LuanJavaFunction( | |
25 LuanMeta.class.getMethod( "__index", LuanState.class, LuanTable.class, Object.class ), this | |
26 ) ); | |
27 if( canNewindex() ) { | |
28 mt.put( "__newindex", new LuanJavaFunction( | |
29 LuanMeta.class.getMethod( "__newindex", LuanState.class, LuanTable.class, Object.class, Object.class ), mt | |
30 ) ); | |
31 } | |
32 } catch(NoSuchMethodException e) { | |
33 throw new RuntimeException(e); | |
34 } | |
35 */ | |
36 mt.put( "__index", this ); | 50 mt.put( "__index", this ); |
37 // mt.put( "__pairs", this ); | 51 mt.put( "__pairs", this ); |
38 if( canNewindex() ) | 52 if( canNewindex() ) |
39 mt.put( "__newindex", this ); | 53 mt.put( "__newindex", this ); |
40 return mt; | 54 return mt; |
41 } | 55 } |
42 | 56 |