Mercurial Hosting > luan
comparison src/luan/LuanJavaFunction.java @ 1333:25746915a241
merge Luan and LuanState
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 22:33:40 -0700 |
parents | 9fa8b8389578 |
children | e0cf0d108a77 |
comparison
equal
deleted
inserted
replaced
1332:11b7e11f9ed5 | 1333:25746915a241 |
---|---|
53 | 53 |
54 public boolean isVarArgs() { | 54 public boolean isVarArgs() { |
55 return method.isVarArgs(); | 55 return method.isVarArgs(); |
56 } | 56 } |
57 | 57 |
58 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | 58 @Override public Object call(Luan luan,Object[] args) throws LuanException { |
59 try { | 59 try { |
60 args = fixArgs(luan,args); | 60 args = fixArgs(luan,args); |
61 return doCall(luan,args); | 61 return doCall(luan,args); |
62 } catch(IllegalArgumentException e) { | 62 } catch(IllegalArgumentException e) { |
63 checkArgs(args); | 63 checkArgs(args); |
64 throw e; | 64 throw e; |
65 } | 65 } |
66 } | 66 } |
67 | 67 |
68 public Object rawCall(LuanState luan,Object[] args) throws LuanException { | 68 public Object rawCall(Luan luan,Object[] args) throws LuanException { |
69 args = fixArgs(luan,args); | 69 args = fixArgs(luan,args); |
70 return doCall(luan,args); | 70 return doCall(luan,args); |
71 } | 71 } |
72 | 72 |
73 private Object doCall(LuanState luan,Object[] args) throws LuanException { | 73 private Object doCall(Luan luan,Object[] args) throws LuanException { |
74 Object rtn; | 74 Object rtn; |
75 try { | 75 try { |
76 rtn = method.invoke(obj,args); | 76 rtn = method.invoke(obj,args); |
77 } catch(IllegalAccessException e) { | 77 } catch(IllegalAccessException e) { |
78 throw new RuntimeException("method = "+method,e); | 78 throw new RuntimeException("method = "+method,e); |
144 if( type.equals("LuanJavaFunction") ) | 144 if( type.equals("LuanJavaFunction") ) |
145 return "function"; | 145 return "function"; |
146 return type; | 146 return type; |
147 } | 147 } |
148 | 148 |
149 private Object[] fixArgs(LuanState luan,Object[] args) throws LuanException { | 149 private Object[] fixArgs(Luan luan,Object[] args) throws LuanException { |
150 int n = argConverters.length; | 150 int n = argConverters.length; |
151 Object[] rtn; | 151 Object[] rtn; |
152 int start = 0; | 152 int start = 0; |
153 if( !takesLuaState && varArgCls==null && args.length == n ) { | 153 if( !takesLuaState && varArgCls==null && args.length == n ) { |
154 rtn = args; | 154 rtn = args; |
187 return rtn; | 187 return rtn; |
188 } | 188 } |
189 | 189 |
190 | 190 |
191 private interface RtnConverter { | 191 private interface RtnConverter { |
192 public Object convert(LuanState luan,Object obj); | 192 public Object convert(Luan luan,Object obj); |
193 } | 193 } |
194 | 194 |
195 private static final RtnConverter RTN_NOTHING = new RtnConverter() { | 195 private static final RtnConverter RTN_NOTHING = new RtnConverter() { |
196 @Override public Object[] convert(LuanState luan,Object obj) { | 196 @Override public Object[] convert(Luan luan,Object obj) { |
197 return NOTHING; | 197 return NOTHING; |
198 } | 198 } |
199 }; | 199 }; |
200 | 200 |
201 private static final RtnConverter RTN_SAME = new RtnConverter() { | 201 private static final RtnConverter RTN_SAME = new RtnConverter() { |
202 @Override public Object convert(LuanState luan,Object obj) { | 202 @Override public Object convert(Luan luan,Object obj) { |
203 return obj; | 203 return obj; |
204 } | 204 } |
205 }; | 205 }; |
206 | 206 |
207 private static final RtnConverter RTN_ARRAY = new RtnConverter() { | 207 private static final RtnConverter RTN_ARRAY = new RtnConverter() { |
208 @Override public Object convert(LuanState luan,Object obj) { | 208 @Override public Object convert(Luan luan,Object obj) { |
209 if( obj == null ) | 209 if( obj == null ) |
210 return null; | 210 return null; |
211 Object[] a = new Object[Array.getLength(obj)]; | 211 Object[] a = new Object[Array.getLength(obj)]; |
212 for( int i=0; i<a.length; i++ ) { | 212 for( int i=0; i<a.length; i++ ) { |
213 a[i] = Array.get(obj,i); | 213 a[i] = Array.get(obj,i); |
225 } | 225 } |
226 return RTN_SAME; | 226 return RTN_SAME; |
227 } | 227 } |
228 | 228 |
229 private interface ArgConverter { | 229 private interface ArgConverter { |
230 public Object convert(LuanState luan,Object obj) throws LuanException; | 230 public Object convert(Luan luan,Object obj) throws LuanException; |
231 } | 231 } |
232 | 232 |
233 private static final ArgConverter ARG_SAME = new ArgConverter() { | 233 private static final ArgConverter ARG_SAME = new ArgConverter() { |
234 public Object convert(LuanState luan,Object obj) { | 234 public Object convert(Luan luan,Object obj) { |
235 return obj; | 235 return obj; |
236 } | 236 } |
237 @Override public String toString() { | 237 @Override public String toString() { |
238 return "ARG_SAME"; | 238 return "ARG_SAME"; |
239 } | 239 } |
240 }; | 240 }; |
241 | 241 |
242 private static final ArgConverter ARG_DOUBLE = new ArgConverter() { | 242 private static final ArgConverter ARG_DOUBLE = new ArgConverter() { |
243 public Object convert(LuanState luan,Object obj) { | 243 public Object convert(Luan luan,Object obj) { |
244 if( obj instanceof Double ) | 244 if( obj instanceof Double ) |
245 return obj; | 245 return obj; |
246 if( obj instanceof Number ) { | 246 if( obj instanceof Number ) { |
247 Number n = (Number)obj; | 247 Number n = (Number)obj; |
248 return n.doubleValue(); | 248 return n.doubleValue(); |
253 return "ARG_DOUBLE"; | 253 return "ARG_DOUBLE"; |
254 } | 254 } |
255 }; | 255 }; |
256 | 256 |
257 private static final ArgConverter ARG_FLOAT = new ArgConverter() { | 257 private static final ArgConverter ARG_FLOAT = new ArgConverter() { |
258 public Object convert(LuanState luan,Object obj) { | 258 public Object convert(Luan luan,Object obj) { |
259 if( obj instanceof Float ) | 259 if( obj instanceof Float ) |
260 return obj; | 260 return obj; |
261 if( obj instanceof Number ) { | 261 if( obj instanceof Number ) { |
262 Number n = (Number)obj; | 262 Number n = (Number)obj; |
263 return n.floatValue(); | 263 return n.floatValue(); |
268 return "ARG_FLOAT"; | 268 return "ARG_FLOAT"; |
269 } | 269 } |
270 }; | 270 }; |
271 | 271 |
272 private static final ArgConverter ARG_LONG = new ArgConverter() { | 272 private static final ArgConverter ARG_LONG = new ArgConverter() { |
273 public Object convert(LuanState luan,Object obj) { | 273 public Object convert(Luan luan,Object obj) { |
274 if( obj instanceof Long ) | 274 if( obj instanceof Long ) |
275 return obj; | 275 return obj; |
276 if( obj instanceof Number ) { | 276 if( obj instanceof Number ) { |
277 Number n = (Number)obj; | 277 Number n = (Number)obj; |
278 long r = n.longValue(); | 278 long r = n.longValue(); |
285 return "ARG_LONG"; | 285 return "ARG_LONG"; |
286 } | 286 } |
287 }; | 287 }; |
288 | 288 |
289 private static final ArgConverter ARG_INTEGER = new ArgConverter() { | 289 private static final ArgConverter ARG_INTEGER = new ArgConverter() { |
290 public Object convert(LuanState luan,Object obj) { | 290 public Object convert(Luan luan,Object obj) { |
291 if( obj instanceof Integer ) | 291 if( obj instanceof Integer ) |
292 return obj; | 292 return obj; |
293 if( obj instanceof Number ) { | 293 if( obj instanceof Number ) { |
294 Number n = (Number)obj; | 294 Number n = (Number)obj; |
295 int r = n.intValue(); | 295 int r = n.intValue(); |
302 return "ARG_INTEGER"; | 302 return "ARG_INTEGER"; |
303 } | 303 } |
304 }; | 304 }; |
305 | 305 |
306 private static final ArgConverter ARG_SHORT = new ArgConverter() { | 306 private static final ArgConverter ARG_SHORT = new ArgConverter() { |
307 public Object convert(LuanState luan,Object obj) { | 307 public Object convert(Luan luan,Object obj) { |
308 if( obj instanceof Short ) | 308 if( obj instanceof Short ) |
309 return obj; | 309 return obj; |
310 if( obj instanceof Number ) { | 310 if( obj instanceof Number ) { |
311 Number n = (Number)obj; | 311 Number n = (Number)obj; |
312 short r = n.shortValue(); | 312 short r = n.shortValue(); |
319 return "ARG_SHORT"; | 319 return "ARG_SHORT"; |
320 } | 320 } |
321 }; | 321 }; |
322 | 322 |
323 private static final ArgConverter ARG_BYTE = new ArgConverter() { | 323 private static final ArgConverter ARG_BYTE = new ArgConverter() { |
324 public Object convert(LuanState luan,Object obj) { | 324 public Object convert(Luan luan,Object obj) { |
325 if( obj instanceof Byte ) | 325 if( obj instanceof Byte ) |
326 return obj; | 326 return obj; |
327 if( obj instanceof Number ) { | 327 if( obj instanceof Number ) { |
328 Number n = (Number)obj; | 328 Number n = (Number)obj; |
329 byte r = n.byteValue(); | 329 byte r = n.byteValue(); |
336 return "ARG_BYTE"; | 336 return "ARG_BYTE"; |
337 } | 337 } |
338 }; | 338 }; |
339 | 339 |
340 private static final ArgConverter ARG_TABLE = new ArgConverter() { | 340 private static final ArgConverter ARG_TABLE = new ArgConverter() { |
341 public Object convert(LuanState luan,Object obj) { | 341 public Object convert(Luan luan,Object obj) { |
342 LuanTable tbl = luan.toTable(obj); | 342 LuanTable tbl = luan.toTable(obj); |
343 return tbl!=null ? tbl : obj; | 343 return tbl!=null ? tbl : obj; |
344 } | 344 } |
345 @Override public String toString() { | 345 @Override public String toString() { |
346 return "ARG_TABLE"; | 346 return "ARG_TABLE"; |
347 } | 347 } |
348 }; | 348 }; |
349 | 349 |
350 private static final ArgConverter ARG_MAP = new ArgConverter() { | 350 private static final ArgConverter ARG_MAP = new ArgConverter() { |
351 public Object convert(LuanState luan,Object obj) throws LuanException { | 351 public Object convert(Luan luan,Object obj) throws LuanException { |
352 if( obj instanceof LuanTable ) { | 352 if( obj instanceof LuanTable ) { |
353 LuanTable t = (LuanTable)obj; | 353 LuanTable t = (LuanTable)obj; |
354 return t.asMap(); | 354 return t.asMap(); |
355 } | 355 } |
356 return obj; | 356 return obj; |
359 return "ARG_MAP"; | 359 return "ARG_MAP"; |
360 } | 360 } |
361 }; | 361 }; |
362 | 362 |
363 private static final ArgConverter ARG_LIST = new ArgConverter() { | 363 private static final ArgConverter ARG_LIST = new ArgConverter() { |
364 public Object convert(LuanState luan,Object obj) { | 364 public Object convert(Luan luan,Object obj) { |
365 if( obj instanceof LuanTable ) { | 365 if( obj instanceof LuanTable ) { |
366 LuanTable t = (LuanTable)obj; | 366 LuanTable t = (LuanTable)obj; |
367 if( t.isList() ) | 367 if( t.isList() ) |
368 return t.asList(); | 368 return t.asList(); |
369 } | 369 } |
373 return "ARG_LIST"; | 373 return "ARG_LIST"; |
374 } | 374 } |
375 }; | 375 }; |
376 | 376 |
377 private static final ArgConverter ARG_SET = new ArgConverter() { | 377 private static final ArgConverter ARG_SET = new ArgConverter() { |
378 public Object convert(LuanState luan,Object obj) throws LuanException { | 378 public Object convert(Luan luan,Object obj) throws LuanException { |
379 if( obj instanceof LuanTable ) { | 379 if( obj instanceof LuanTable ) { |
380 LuanTable t = (LuanTable)obj; | 380 LuanTable t = (LuanTable)obj; |
381 if( t.isSet() ) | 381 if( t.isSet() ) |
382 return t.asSet(); | 382 return t.asSet(); |
383 } | 383 } |
387 return "ARG_SET"; | 387 return "ARG_SET"; |
388 } | 388 } |
389 }; | 389 }; |
390 | 390 |
391 private static final ArgConverter ARG_COLLECTION = new ArgConverter() { | 391 private static final ArgConverter ARG_COLLECTION = new ArgConverter() { |
392 public Object convert(LuanState luan,Object obj) throws LuanException { | 392 public Object convert(Luan luan,Object obj) throws LuanException { |
393 if( obj instanceof LuanTable ) { | 393 if( obj instanceof LuanTable ) { |
394 LuanTable t = (LuanTable)obj; | 394 LuanTable t = (LuanTable)obj; |
395 if( t.isList() ) | 395 if( t.isList() ) |
396 return t.asList(); | 396 return t.asList(); |
397 if( t.isSet() ) | 397 if( t.isSet() ) |
409 | 409 |
410 ArgArray(Class cls) { | 410 ArgArray(Class cls) { |
411 a = (Object[])Array.newInstance(cls.getComponentType(),0); | 411 a = (Object[])Array.newInstance(cls.getComponentType(),0); |
412 } | 412 } |
413 | 413 |
414 public Object convert(LuanState luan,Object obj) { | 414 public Object convert(Luan luan,Object obj) { |
415 if( obj instanceof LuanTable ) { | 415 if( obj instanceof LuanTable ) { |
416 LuanTable t = (LuanTable)obj; | 416 LuanTable t = (LuanTable)obj; |
417 if( t.isList() ) { | 417 if( t.isList() ) { |
418 try { | 418 try { |
419 return t.asList().toArray(a); | 419 return t.asList().toArray(a); |
424 } | 424 } |
425 } | 425 } |
426 | 426 |
427 private static boolean takesLuaState(JavaMethod m) { | 427 private static boolean takesLuaState(JavaMethod m) { |
428 Class[] paramTypes = m.getParameterTypes(); | 428 Class[] paramTypes = m.getParameterTypes(); |
429 return paramTypes.length > 0 && paramTypes[0].equals(LuanState.class); | 429 return paramTypes.length > 0 && paramTypes[0].equals(Luan.class); |
430 } | 430 } |
431 | 431 |
432 private static ArgConverter[] getArgConverters(boolean takesLuaState,JavaMethod m) { | 432 private static ArgConverter[] getArgConverters(boolean takesLuaState,JavaMethod m) { |
433 final boolean isVarArgs = m.isVarArgs(); | 433 final boolean isVarArgs = m.isVarArgs(); |
434 Class[] paramTypes = m.getParameterTypes(); | 434 Class[] paramTypes = m.getParameterTypes(); |