comparison core/src/luan/impl/LuanImpl.java @ 674:2994e46f62b7

some optimization
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Apr 2016 19:31:18 -0600
parents d3e5414bdf4c
children 41f791e4206d
comparison
equal deleted inserted replaced
673:33629d753cf9 674:2994e46f62b7
11 import luan.modules.JavaLuan; 11 import luan.modules.JavaLuan;
12 12
13 13
14 public final class LuanImpl { 14 public final class LuanImpl {
15 private LuanImpl() {} // never 15 private LuanImpl() {} // never
16
17 /*
18 private static List list = new ArrayList();
19
20 static int addObj(Object obj) {
21 int i = list.size();
22 list.add(obj);
23 return i;
24 }
25
26 public static Object getObj(int i) {
27 return list.get(i);
28 }
29 */
30 16
31 public static int len(LuanState luan,Object o) throws LuanException { 17 public static int len(LuanState luan,Object o) throws LuanException {
32 if( o instanceof String ) { 18 if( o instanceof String ) {
33 String s = (String)o; 19 String s = (String)o;
34 return s.length(); 20 return s.length();
189 return null; 175 return null;
190 Object[] a = (Object[])o; 176 Object[] a = (Object[])o;
191 return i<a.length ? a[i] : null; 177 return i<a.length ? a[i] : null;
192 } 178 }
193 179
194 public static Object[] varArgs(Object o,int i) { 180 public static Object[] varArgs(Object[] a,int i) {
195 if( !(o instanceof Object[]) )
196 return i==0 ? new Object[]{o} : LuanFunction.NOTHING;
197 Object[] a = (Object[])o;
198 if( i >= a.length ) 181 if( i >= a.length )
199 return LuanFunction.NOTHING; 182 return LuanFunction.NOTHING;
200 Object[] rtn = new Object[a.length - i]; 183 Object[] rtn = new Object[a.length - i];
201 System.arraycopy(a,i,rtn,0,rtn.length); 184 System.arraycopy(a,i,rtn,0,rtn.length);
202 return rtn; 185 return rtn;
231 return rtn; 214 return rtn;
232 } 215 }
233 } 216 }
234 } 217 }
235 218
236 public static LuanTable table(Object o) { 219 public static LuanTable table(Object[] a) {
237 LuanTable table = new LuanTable(); 220 LuanTable table = new LuanTable();
238 int i = 0; 221 int i = 0;
239 for( Object fld : Luan.array(o) ) { 222 for( Object fld : a ) {
240 if( fld instanceof TableField ) { 223 if( fld instanceof TableField ) {
241 TableField tblFld = (TableField)fld; 224 TableField tblFld = (TableField)fld;
242 Object key = tblFld.key; 225 Object key = tblFld.key;
243 Object value = tblFld.value; 226 Object value = tblFld.value;
244 if( key != null && value != null ) 227 if( key != null && value != null )
250 } 233 }
251 } 234 }
252 return table; 235 return table;
253 } 236 }
254 237
238 public static Object first(Object[] a) {
239 return a.length==0 ? null : a[0];
240 }
241
255 } 242 }