Mercurial Hosting > luan
comparison core/src/luan/modules/BasicLuan.java @ 221:ec016471c6eb
make LuanTable an interface
git-svn-id: https://luan-java.googlecode.com/svn/trunk@222 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 17 Jul 2014 07:49:26 +0000 |
parents | 75750ceb45ee |
children | ef39bc4d3f70 |
comparison
equal
deleted
inserted
replaced
220:61afe2a1ce96 | 221:ec016471c6eb |
---|---|
20 | 20 |
21 public final class BasicLuan { | 21 public final class BasicLuan { |
22 | 22 |
23 public static final LuanFunction LOADER = new LuanFunction() { | 23 public static final LuanFunction LOADER = new LuanFunction() { |
24 @Override public Object call(LuanState luan,Object[] args) { | 24 @Override public Object call(LuanState luan,Object[] args) { |
25 LuanTable module = new LuanTable(); | 25 LuanTable module = Luan.newTable(); |
26 try { | 26 try { |
27 module.put( "assert", new LuanJavaFunction(BasicLuan.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); | 27 module.put( "assert", new LuanJavaFunction(BasicLuan.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) ); |
28 add( module, "assert_boolean", LuanState.class, Boolean.TYPE ); | 28 add( module, "assert_boolean", LuanState.class, Boolean.TYPE ); |
29 add( module, "assert_nil", LuanState.class, Object.class ); | 29 add( module, "assert_nil", LuanState.class, Object.class ); |
30 add( module, "assert_number", LuanState.class, Number.class ); | 30 add( module, "assert_number", LuanState.class, Number.class ); |
83 public static Object do_file(LuanState luan,String fileName) throws LuanException { | 83 public static Object do_file(LuanState luan,String fileName) throws LuanException { |
84 LuanFunction fn = load_file(luan,fileName); | 84 LuanFunction fn = load_file(luan,fileName); |
85 return luan.call(fn); | 85 return luan.call(fn); |
86 } | 86 } |
87 | 87 |
88 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 88 public static LuanFunction pairs(LuanState luan,final LuanTable t) throws LuanException { |
89 return new LuanFunction() { | 89 Utils.checkNotNull(luan,t,"table"); |
90 return new LuanFunction() { | |
91 Iterator<Map.Entry<Object,Object>> iter = t.iterator(); | |
92 | |
90 @Override public Object[] call(LuanState luan,Object[] args) { | 93 @Override public Object[] call(LuanState luan,Object[] args) { |
91 if( !iter.hasNext() ) | 94 if( !iter.hasNext() ) |
92 return LuanFunction.NOTHING; | 95 return LuanFunction.NOTHING; |
93 Map.Entry<Object,Object> entry = iter.next(); | 96 Map.Entry<Object,Object> entry = iter.next(); |
94 return new Object[]{entry.getKey(),entry.getValue()}; | 97 return new Object[]{entry.getKey(),entry.getValue()}; |
95 } | 98 } |
96 }; | 99 }; |
97 } | 100 } |
98 | 101 |
99 public static LuanFunction pairs(LuanState luan,LuanTable t) throws LuanException { | 102 public static LuanFunction ipairs(LuanState luan,final LuanTable t) throws LuanException { |
100 Utils.checkNotNull(luan,t,"table"); | 103 Utils.checkNotNull(luan,t,"table"); |
101 return pairs( t.iterator() ); | 104 return new LuanFunction() { |
102 } | 105 List<Object> list = t.asList(); |
103 | 106 int i = 0; |
104 public static LuanFunction ipairs(LuanState luan,LuanTable t) throws LuanException { | 107 final int size = list.size(); |
105 Utils.checkNotNull(luan,t,"table"); | 108 |
106 return pairs( t.listIterator() ); | 109 @Override public Object[] call(LuanState luan,Object[] args) { |
110 if( i >= size ) | |
111 return LuanFunction.NOTHING; | |
112 Object val = list.get(i++); | |
113 return new Object[]{i,val}; | |
114 } | |
115 }; | |
107 } | 116 } |
108 | 117 |
109 public static LuanTable get_metatable(LuanState luan,Object obj) { | 118 public static LuanTable get_metatable(LuanState luan,Object obj) { |
110 return luan.getMetatable(obj); | 119 return luan.getMetatable(obj); |
111 } | 120 } |