Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 43:80b67b1a653c
implement string lib
git-svn-id: https://luan-java.googlecode.com/svn/trunk@44 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 25 Dec 2012 03:42:42 +0000 |
parents | 786699c78837 |
children | 57054fa43189 |
comparison
equal
deleted
inserted
replaced
42:786699c78837 | 43:80b67b1a653c |
---|---|
119 public static Object[] do_file(LuaState lua,String fileName) throws LuaException { | 119 public static Object[] do_file(LuaState lua,String fileName) throws LuaException { |
120 LuaFunction fn = load_file(lua,fileName); | 120 LuaFunction fn = load_file(lua,fileName); |
121 return lua.call(fn,LuaElement.JAVA,null); | 121 return lua.call(fn,LuaElement.JAVA,null); |
122 } | 122 } |
123 | 123 |
124 private static class TableIter { | 124 public static LuaFunction pairs(LuaTable t) { |
125 private final Iterator<Map.Entry<Object,Object>> iter; | 125 final Iterator<Map.Entry<Object,Object>> iter = t.iterator(); |
126 | 126 return new LuaFunction() { |
127 TableIter(Iterator<Map.Entry<Object,Object>> iter) { | 127 public Object[] call(LuaState lua,Object[] args) { |
128 this.iter = iter; | 128 if( !iter.hasNext() ) |
129 } | 129 return LuaFunction.EMPTY_RTN; |
130 | 130 Map.Entry<Object,Object> entry = iter.next(); |
131 public Object[] next() { | 131 return new Object[]{entry.getKey(),entry.getValue()}; |
132 if( !iter.hasNext() ) | 132 } |
133 return LuaFunction.EMPTY_RTN; | 133 }; |
134 Map.Entry<Object,Object> entry = iter.next(); | |
135 return new Object[]{entry.getKey(),entry.getValue()}; | |
136 } | |
137 } | |
138 private static final Method nextTableIter; | |
139 static { | |
140 try { | |
141 nextTableIter = TableIter.class.getMethod("next"); | |
142 nextTableIter.setAccessible(true); | |
143 } catch(NoSuchMethodException e) { | |
144 throw new RuntimeException(e); | |
145 } | |
146 } | 134 } |
147 | 135 |
148 static LuaFunction pairs(Iterator<Map.Entry<Object,Object>> iter) { | 136 public static LuaFunction ipairs(final LuaTable t) { |
149 TableIter ti = new TableIter(iter); | 137 return new LuaFunction() { |
150 return new LuaJavaFunction(nextTableIter,ti); | 138 private double i = 0.0; |
151 } | 139 public Object[] call(LuaState lua,Object[] args) { |
152 | 140 LuaNumber n = new LuaNumber(++i); |
153 public static LuaFunction pairs(LuaTable t) { | 141 Object val = t.get(n); |
154 return pairs(t.iterator()); | 142 return val==null ? LuaFunction.EMPTY_RTN : new Object[]{n,val}; |
155 } | 143 } |
156 | 144 }; |
157 private static class ArrayIter { | |
158 private final LuaTable t; | |
159 private double i = 0.0; | |
160 | |
161 ArrayIter(LuaTable t) { | |
162 this.t = t; | |
163 } | |
164 | |
165 public Object[] next() { | |
166 LuaNumber n = new LuaNumber(++i); | |
167 Object val = t.get(n); | |
168 return val==null ? LuaFunction.EMPTY_RTN : new Object[]{n,val}; | |
169 } | |
170 } | |
171 private static final Method nextArrayIter; | |
172 static { | |
173 try { | |
174 nextArrayIter = ArrayIter.class.getMethod("next"); | |
175 nextArrayIter.setAccessible(true); | |
176 } catch(NoSuchMethodException e) { | |
177 throw new RuntimeException(e); | |
178 } | |
179 } | |
180 | |
181 public static LuaFunction ipairs(LuaTable t) { | |
182 ArrayIter ai = new ArrayIter(t); | |
183 return new LuaJavaFunction(nextArrayIter,ai); | |
184 } | 145 } |
185 | 146 |
186 public static LuaTable get_metatable(LuaState lua,Object obj) { | 147 public static LuaTable get_metatable(LuaState lua,Object obj) { |
187 return lua.getMetatable(obj); | 148 return lua.getMetatable(obj); |
188 } | 149 } |