comparison src/luan/modules/ThreadLuan.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 3ffc7c4a3b85
children a9cc35c71eb0
comparison
equal deleted inserted replaced
1266:05934fbf635a 1267:9fa8b8389578
123 Unsafe(String reason) { 123 Unsafe(String reason) {
124 this.reason = reason; 124 this.reason = reason;
125 } 125 }
126 } 126 }
127 127
128 private static Object makeSafe(Object v) { 128 private static Object makeSafe(LuanState luan,Object v) {
129 if( v instanceof LuanTable ) { 129 if( v instanceof LuanTable ) {
130 LuanTable tbl = (LuanTable)v; 130 LuanTable tbl = (LuanTable)v;
131 if( tbl.getMetatable() != null ) 131 if( tbl.getMetatable() != null )
132 return new Unsafe("table with metatable"); 132 return new Unsafe("table with metatable");
133 LuanTable rtn = new LuanTable(); 133 LuanTable rtn = new LuanTable(luan);
134 for( Map.Entry entry : tbl.rawIterable() ) { 134 for( Map.Entry entry : tbl.rawIterable() ) {
135 Object key = makeSafe( entry.getKey() ); 135 Object key = makeSafe( luan, entry.getKey() );
136 if( key instanceof Unsafe ) 136 if( key instanceof Unsafe )
137 return key; 137 return key;
138 Object value = makeSafe( entry.getValue() ); 138 Object value = makeSafe( luan, entry.getValue() );
139 if( value instanceof Unsafe ) 139 if( value instanceof Unsafe )
140 return value; 140 return value;
141 rtn.rawPut(key,value); 141 rtn.rawPut(key,value);
142 } 142 }
143 return rtn; 143 return rtn;
144 } else if( v instanceof Object[] ) { 144 } else if( v instanceof Object[] ) {
145 Object[] a = (Object[])v; 145 Object[] a = (Object[])v;
146 for( int i=0; i<a.length; i++ ) { 146 for( int i=0; i<a.length; i++ ) {
147 Object obj = makeSafe(a[i]); 147 Object obj = makeSafe(luan,a[i]);
148 if( obj instanceof Unsafe ) 148 if( obj instanceof Unsafe )
149 return obj; 149 return obj;
150 a[i] = obj; 150 a[i] = obj;
151 } 151 }
152 return a; 152 return a;
165 Callable(LuanState luan,LuanTable fns) { 165 Callable(LuanState luan,LuanTable fns) {
166 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 166 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
167 this.fns = (LuanTable)cloner.get(fns); 167 this.fns = (LuanTable)cloner.get(fns);
168 } 168 }
169 169
170 public synchronized Object call(String fnName,Object... args) throws LuanException { 170 public synchronized Object call(LuanState callerLuan,String fnName,Object... args) throws LuanException {
171 Object obj = makeSafe(args); 171 Object obj = makeSafe(luan,args);
172 if( obj instanceof Unsafe ) 172 if( obj instanceof Unsafe )
173 throw new LuanException("can't pass "+((Unsafe)obj).reason+" to global_callable "+Arrays.asList(args)); 173 throw new LuanException("can't pass "+((Unsafe)obj).reason+" to global_callable "+Arrays.asList(args));
174 args = (Object[])obj; 174 args = (Object[])obj;
175 Object f = fns.get(luan,fnName); 175 Object f = fns.get(fnName);
176 if( f == null ) 176 if( f == null )
177 throw new LuanException("function '"+fnName+"' not found in global_callable"); 177 throw new LuanException("function '"+fnName+"' not found in global_callable");
178 if( !(f instanceof LuanFunction) ) 178 if( !(f instanceof LuanFunction) )
179 throw new LuanException("value of '"+fnName+"' not a function in global_callable"); 179 throw new LuanException("value of '"+fnName+"' not a function in global_callable");
180 LuanFunction fn = (LuanFunction)f; 180 LuanFunction fn = (LuanFunction)f;
181 Object rtn = fn.call(luan,args); 181 Object rtn = fn.call(luan,args);
182 rtn = makeSafe(rtn); 182 rtn = makeSafe(callerLuan,rtn);
183 if( rtn instanceof Unsafe ) 183 if( rtn instanceof Unsafe )
184 throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable"); 184 throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable");
185 return rtn; 185 return rtn;
186 } 186 }
187 } 187 }