comparison src/luan/modules/BasicLuan.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 e38f5869e9df
children 623dfe0e2e73
comparison
equal deleted inserted replaced
1266:05934fbf635a 1267:9fa8b8389578
41 if( src == null ) 41 if( src == null )
42 throw new LuanException("file '"+fileName+"' not found" ); 42 throw new LuanException("file '"+fileName+"' not found" );
43 return load(src,fileName,null); 43 return load(src,fileName,null);
44 } 44 }
45 45
46 public static LuanFunction pairs(final LuanState luan,final LuanTable t) throws LuanException { 46 public static LuanFunction pairs(final LuanTable t) throws LuanException {
47 Utils.checkNotNull(t); 47 Utils.checkNotNull(t);
48 return t.pairs(luan); 48 return t.pairs();
49 } 49 }
50 50
51 public static LuanFunction ipairs(final LuanTable t) throws LuanException { 51 public static LuanFunction ipairs(final LuanTable t) throws LuanException {
52 Utils.checkNotNull(t); 52 Utils.checkNotNull(t);
53 return new LuanFunction() { 53 return new LuanFunction() {
71 return null; 71 return null;
72 Object obj = metatable.rawGet("__metatable"); 72 Object obj = metatable.rawGet("__metatable");
73 return obj!=null ? obj : metatable; 73 return obj!=null ? obj : metatable;
74 } 74 }
75 75
76 public static void set_metatable(LuanState luan,LuanTable table,LuanTable metatable) throws LuanException { 76 public static void set_metatable(LuanTable table,LuanTable metatable) throws LuanException {
77 Utils.checkNotNull(table); 77 Utils.checkNotNull(table);
78 if( table.getHandler(luan,"__metatable") != null ) 78 if( table.getHandler("__metatable") != null )
79 throw new LuanException("cannot change a protected metatable"); 79 throw new LuanException("cannot change a protected metatable");
80 table.setMetatable(metatable); 80 table.setMetatable(metatable);
81 } 81 }
82 82
83 public static boolean raw_equal(Object v1,Object v2) { 83 public static boolean raw_equal(Object v1,Object v2) {
108 return luan.toString(v); 108 return luan.toString(v);
109 } 109 }
110 110
111 public static LuanTable new_error(LuanState luan,Object msg) throws LuanException { 111 public static LuanTable new_error(LuanState luan,Object msg) throws LuanException {
112 String s = luan.toString(msg); 112 String s = luan.toString(msg);
113 LuanTable tbl = new LuanException(s).table(); 113 LuanTable tbl = new LuanException(s).table(luan);
114 tbl.rawPut( "message", msg ); 114 tbl.rawPut( "message", msg );
115 return tbl; 115 return tbl;
116 } 116 }
117 117
118 public static int assert_integer(int v) { 118 public static int assert_integer(int v) {
164 return obj instanceof LuanFunction ? (LuanFunction)obj : null; 164 return obj instanceof LuanFunction ? (LuanFunction)obj : null;
165 } 165 }
166 166
167 public static Object try_(LuanState luan,LuanTable blocks,Object... args) throws LuanException { 167 public static Object try_(LuanState luan,LuanTable blocks,Object... args) throws LuanException {
168 Utils.checkNotNull(blocks); 168 Utils.checkNotNull(blocks);
169 Object obj = blocks.get(luan,1); 169 Object obj = blocks.get(1);
170 if( obj == null ) 170 if( obj == null )
171 throw new LuanException("missing 'try' value"); 171 throw new LuanException("missing 'try' value");
172 if( !(obj instanceof LuanFunction) ) 172 if( !(obj instanceof LuanFunction) )
173 throw new LuanException("bad 'try' value (function expected, got "+Luan.type(obj)+")"); 173 throw new LuanException("bad 'try' value (function expected, got "+Luan.type(obj)+")");
174 LuanFunction tryFn = (LuanFunction)obj; 174 LuanFunction tryFn = (LuanFunction)obj;
175 LuanFunction catchFn = null; 175 LuanFunction catchFn = null;
176 obj = blocks.get(luan,"catch"); 176 obj = blocks.get("catch");
177 if( obj != null ) { 177 if( obj != null ) {
178 if( !(obj instanceof LuanFunction) ) 178 if( !(obj instanceof LuanFunction) )
179 throw new LuanException("bad 'catch' value (function expected, got "+Luan.type(obj)+")"); 179 throw new LuanException("bad 'catch' value (function expected, got "+Luan.type(obj)+")");
180 catchFn = (LuanFunction)obj; 180 catchFn = (LuanFunction)obj;
181 } 181 }
182 LuanFunction finallyFn = null; 182 LuanFunction finallyFn = null;
183 obj = blocks.get(luan,"finally"); 183 obj = blocks.get("finally");
184 if( obj != null ) { 184 if( obj != null ) {
185 if( !(obj instanceof LuanFunction) ) 185 if( !(obj instanceof LuanFunction) )
186 throw new LuanException("bad 'finally' value (function expected, got "+Luan.type(obj)+")"); 186 throw new LuanException("bad 'finally' value (function expected, got "+Luan.type(obj)+")");
187 finallyFn = (LuanFunction)obj; 187 finallyFn = (LuanFunction)obj;
188 } 188 }
189 try { 189 try {
190 return tryFn.call(luan,args); 190 return tryFn.call(luan,args);
191 } catch(LuanException e) { 191 } catch(LuanException e) {
192 if( catchFn == null ) 192 if( catchFn == null )
193 throw e; 193 throw e;
194 return catchFn.call(luan,new Object[]{e.table()}); 194 return catchFn.call(luan,new Object[]{e.table(luan)});
195 } finally { 195 } finally {
196 if( finallyFn != null ) 196 if( finallyFn != null )
197 finallyFn.call(luan); 197 finallyFn.call(luan);
198 } 198 }
199 } 199 }
206 for( int i=0; i<r.length; i++ ) { 206 for( int i=0; i<r.length; i++ ) {
207 rtn[i+1] = r[i]; 207 rtn[i+1] = r[i];
208 } 208 }
209 return rtn; 209 return rtn;
210 } catch(LuanException e) { 210 } catch(LuanException e) {
211 return new Object[]{false,e.table()}; 211 return new Object[]{false,e.table(luan)};
212 } 212 }
213 } 213 }
214 214
215 public static String number_type(Number v) throws LuanException { 215 public static String number_type(Number v) throws LuanException {
216 Utils.checkNotNull(v); 216 Utils.checkNotNull(v);