comparison src/luan/modules/TableLuan.java @ 1267:9fa8b8389578

add LuanTable.luan; support metatable __gc(); add luan.sql;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 02:10:41 -0700
parents 7ef40e1923b7
children 5ba660381bd5
comparison
equal deleted inserted replaced
1266:05934fbf635a 1267:9fa8b8389578
15 15
16 public final class TableLuan { 16 public final class TableLuan {
17 17
18 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { 18 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException {
19 int first = i==null ? 1 : i; 19 int first = i==null ? 1 : i;
20 int last = j==null ? list.length(luan) : j; 20 int last = j==null ? list.length() : j;
21 StringBuilder buf = new StringBuilder(); 21 StringBuilder buf = new StringBuilder();
22 for( int k=first; k<=last; k++ ) { 22 for( int k=first; k<=last; k++ ) {
23 Object val = list.get(luan,k); 23 Object val = list.get(k);
24 if( val==null ) 24 if( val==null )
25 break; 25 break;
26 if( sep!=null && k > first ) 26 if( sep!=null && k > first )
27 buf.append(sep); 27 buf.append(sep);
28 String s = luan.toString(val); 28 String s = luan.toString(val);
82 } catch(LuanRuntimeException e) { 82 } catch(LuanRuntimeException e) {
83 throw (LuanException)e.getCause(); 83 throw (LuanException)e.getCause();
84 } 84 }
85 } 85 }
86 86
87 public static LuanTable pack(Object... args) { 87 public static LuanTable pack(LuanState luan,Object... args) {
88 LuanTable tbl = new LuanTable(Arrays.asList(args)); 88 LuanTable tbl = new LuanTable(luan,Arrays.asList(args));
89 tbl.rawPut( "n", args.length ); 89 tbl.rawPut( "n", args.length );
90 return tbl; 90 return tbl;
91 } 91 }
92 92
93 @LuanMethod public static Object[] unpack(LuanState luan,LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException { 93 @LuanMethod public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException {
94 int from = iFrom!=null ? iFrom : 1; 94 int from = iFrom!=null ? iFrom : 1;
95 int to = iTo!=null ? iTo : tbl.length(luan); 95 int to = iTo!=null ? iTo : tbl.length();
96 List<Object> list = new ArrayList<Object>(); 96 List<Object> list = new ArrayList<Object>();
97 for( int i=from; i<=to; i++ ) { 97 for( int i=from; i<=to; i++ ) {
98 list.add( tbl.get(luan,i) ); 98 list.add( tbl.get(i) );
99 } 99 }
100 return list.toArray(); 100 return list.toArray();
101 } 101 }
102 102
103 public static LuanTable copy(LuanTable list,Integer from,Integer to) { 103 public static LuanTable copy(LuanState luan,LuanTable list,Integer from,Integer to) {
104 if( from == null ) 104 if( from == null )
105 return new LuanTable(list); 105 return new LuanTable(list);
106 if( to == null ) 106 if( to == null )
107 to = list.rawLength(); 107 to = list.rawLength();
108 return list.rawSubList(from,to); 108 return list.rawSubList(from,to);
122 122
123 public static int size(LuanTable tbl) throws LuanException { 123 public static int size(LuanTable tbl) throws LuanException {
124 return tbl.rawSize(); 124 return tbl.rawSize();
125 } 125 }
126 126
127 public static LuanTable toTable(LuanState luan,Object obj) {
128 return luan.toTable(obj);
129 }
130
127 } 131 }