annotate src/luan/LuanJavaFunction.java @ 115:eacf6ce1b47d

add IoLib git-svn-id: https://luan-java.googlecode.com/svn/trunk@116 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 29 May 2014 09:26:44 +0000
parents 8c706d6eb5dc
children d7be9b3abf1a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 package luan;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
3 import java.lang.reflect.Array;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
4 import java.lang.reflect.Method;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
5 import java.lang.reflect.Constructor;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
6 import java.lang.reflect.InvocationTargetException;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
7 import java.util.List;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
8 import java.util.Map;
95
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
9 import java.util.HashMap;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
10 import java.util.Set;
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
11 import java.util.Arrays;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
12
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
13
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
14 public final class LuanJavaFunction extends LuanFunction {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
15 private final JavaMethod method;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
16 private final Object obj;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
17 private final RtnConverter rtnConverter;
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
18 private final boolean takesLuaState;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
19 private final ArgConverter[] argConverters;
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
20 private final Class<?> varArgCls;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
21
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
22 public LuanJavaFunction(Method method,Object obj) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
23 this( JavaMethod.of(method), obj );
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
24 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
25
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
26 public LuanJavaFunction(Constructor constr,Object obj) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
27 this( JavaMethod.of(constr), obj );
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
28 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
29
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
30 LuanJavaFunction(JavaMethod method,Object obj) {
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
31 this.method = method;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
32 this.obj = obj;
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
33 this.rtnConverter = getRtnConverter(method);
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
34 this.takesLuaState = takesLuaState(method);
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
35 this.argConverters = getArgConverters(takesLuaState,method);
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
36 if( method.isVarArgs() ) {
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
37 Class<?>[] paramTypes = method.getParameterTypes();
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
38 this.varArgCls = paramTypes[paramTypes.length-1].getComponentType();
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
39 } else {
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
40 this.varArgCls = null;
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
41 }
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
42 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
43
60
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 57
diff changeset
44 @Override public String toString() {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 57
diff changeset
45 return "java-function: " + method;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 57
diff changeset
46 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 57
diff changeset
47
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
48 public Class<?>[] getParameterTypes() {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
49 return method.getParameterTypes();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
50 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
51
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
52 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
68
877288464542 better type checking
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 67
diff changeset
53 args = fixArgs(luan,args);
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
54 try {
68
877288464542 better type checking
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 67
diff changeset
55 return doCall(luan,args);
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
56 } catch(IllegalArgumentException e) {
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
57 checkArgs(luan,args);
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
58 throw e;
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
59 }
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
60 }
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
61
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
62 public Object rawCall(LuanState luan,Object[] args) throws LuanException {
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
63 args = fixArgs(luan,args);
68
877288464542 better type checking
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 67
diff changeset
64 return doCall(luan,args);
877288464542 better type checking
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 67
diff changeset
65 }
877288464542 better type checking
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 67
diff changeset
66
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
67 private Object doCall(LuanState luan,Object[] args) throws LuanException {
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
68 Object rtn;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
69 try {
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
70 rtn = method.invoke(obj,args);
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
71 } catch(IllegalAccessException e) {
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
72 throw new RuntimeException(e);
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
73 } catch(InvocationTargetException e) {
40
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
74 Throwable cause = e.getCause();
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
75 if( cause instanceof Error )
e3624b7cd603 implement stack trace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 37
diff changeset
76 throw (Error)cause;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
77 if( cause instanceof LuanException )
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
78 throw (LuanException)cause;
115
eacf6ce1b47d add IoLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 113
diff changeset
79 throw luan.JAVA.exception(cause);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
80 } catch(InstantiationException e) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
81 throw new RuntimeException(e);
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
82 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
83 return rtnConverter.convert(rtn);
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
84 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
85
95
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
86 private static final Map<Class,Class> primitiveMap = new HashMap<Class,Class>();
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
87 static {
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
88 primitiveMap.put(Boolean.TYPE,Boolean.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
89 primitiveMap.put(Character.TYPE,Character.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
90 primitiveMap.put(Byte.TYPE,Byte.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
91 primitiveMap.put(Short.TYPE,Short.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
92 primitiveMap.put(Integer.TYPE,Integer.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
93 primitiveMap.put(Long.TYPE,Long.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
94 primitiveMap.put(Float.TYPE,Float.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
95 primitiveMap.put(Double.TYPE,Double.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
96 primitiveMap.put(Void.TYPE,Void.class);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
97 }
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
98
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
99 private void checkArgs(LuanState luan,Object[] args) throws LuanException {
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
100 Class<?>[] a = getParameterTypes();
69
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 68
diff changeset
101 int start = takesLuaState ? 1 : 0;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 68
diff changeset
102 for( int i=start; i<a.length; i++ ) {
67
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
103 Class<?> paramType = a[i];
95
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
104 Class<?> type = paramType;
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
105 if( type.isPrimitive() )
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
106 type = primitiveMap.get(type);
67
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
107 Object arg = args[i];
95
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
108 if( !type.isInstance(arg) ) {
67
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
109 String expected = paramType.getSimpleName();
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
110 if( arg==null ) {
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
111 if( paramType.isPrimitive() )
88
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 86
diff changeset
112 throw luan.JAVA.exception("bad argument #"+(i+1-start)+" ("+expected+" expected, got nil)");
67
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
113 } else {
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
114 String got = arg.getClass().getSimpleName();
88
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 86
diff changeset
115 throw luan.JAVA.exception("bad argument #"+(i+1-start)+" ("+expected+" expected, got "+got+")");
67
8ca58ab6919b fix LuanJavaFunction.checkArgs() for nulls
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 64
diff changeset
116 }
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
117 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
118 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
119 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
120
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
121 private Object[] fixArgs(LuanState luan,Object[] args) {
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
122 int n = argConverters.length;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
123 Object[] rtn;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
124 int start = 0;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
125 if( !takesLuaState && varArgCls==null && args.length == n ) {
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
126 rtn = args;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
127 } else {
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
128 if( takesLuaState )
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
129 n++;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
130 rtn = new Object[n];
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
131 if( takesLuaState ) {
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
132 rtn[start++] = luan;
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
133 }
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
134 n = argConverters.length;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
135 if( varArgCls != null ) {
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
136 n--;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
137 if( args.length < argConverters.length ) {
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
138 rtn[rtn.length-1] = Array.newInstance(varArgCls,0);
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
139 } else {
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
140 int len = args.length - n;
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
141 Object varArgs = Array.newInstance(varArgCls,len);
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
142 ArgConverter ac = argConverters[n];
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
143 for( int i=0; i<len; i++ ) {
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
144 Array.set( varArgs, i, ac.convert(args[n+i]) );
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
145 }
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
146 rtn[rtn.length-1] = varArgs;
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
147 }
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
148 }
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
149 System.arraycopy(args,0,rtn,start,Math.min(args.length,n));
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
150 }
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
151 for( int i=0; i<n; i++ ) {
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
152 rtn[start+i] = argConverters[i].convert(rtn[start+i]);
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
153 }
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
154 return rtn;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
155 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
156
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
157
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
158 private interface RtnConverter {
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
159 public Object convert(Object obj);
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
160 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
161
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
162 private static final RtnConverter RTN_EMPTY = new RtnConverter() {
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
163 @Override public Object[] convert(Object obj) {
86
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 69
diff changeset
164 return EMPTY;
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
165 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
166 };
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
167
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
168 private static final RtnConverter RTN_SAME = new RtnConverter() {
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
169 @Override public Object convert(Object obj) {
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
170 return obj;
12
9cea1aea5eef CmdLine can run files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 4
diff changeset
171 }
9cea1aea5eef CmdLine can run files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 4
diff changeset
172 };
9cea1aea5eef CmdLine can run files
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 4
diff changeset
173
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
174 private static final RtnConverter RTN_NUMBER_ARRAY = new RtnConverter() {
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
175 @Override public Object convert(Object obj) {
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
176 if( obj == null )
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
177 return null;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
178 Object[] rtn = new Object[Array.getLength(obj)];
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
179 for( int i=0; i<rtn.length; i++ ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
180 rtn[i] = Array.get(obj,i);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
181 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
182 return rtn;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
183 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
184 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
185
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
186 private static RtnConverter getRtnConverter(JavaMethod m) {
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
187 Class<?> rtnType = m.getReturnType();
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
188 if( rtnType == Void.TYPE )
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
189 return RTN_EMPTY;
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
190 if( rtnType.isArray() && isNumber(rtnType.getComponentType()) )
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
191 return RTN_NUMBER_ARRAY;
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 98
diff changeset
192 return RTN_SAME;
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
193 }
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
194
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
195 private static boolean isNumber(Class<?> rtnType) {
113
8c706d6eb5dc add binary type
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 111
diff changeset
196 return rtnType == Short.TYPE
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
197 || rtnType == Integer.TYPE
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
198 || rtnType == Long.TYPE
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
199 || rtnType == Float.TYPE
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
200 || rtnType == Double.TYPE
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
201 ;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
202 }
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
203
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
204 private interface ArgConverter {
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
205 public Object convert(Object obj);
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
206 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
207
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
208 private static final ArgConverter ARG_SAME = new ArgConverter() {
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
209 public Object convert(Object obj) {
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
210 return obj;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
211 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
212 };
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
213
95
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
214 private static final ArgConverter ARG_BOOLEAN = new ArgConverter() {
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
215 public Object convert(Object obj) {
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
216 return Luan.toBoolean(obj);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
217 }
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
218 };
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
219
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
220 private static final ArgConverter ARG_BOOLEAN_OBJ = new ArgConverter() {
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
221 public Object convert(Object obj) {
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
222 return obj==null ? null : Luan.toBoolean(obj);
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
223 }
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
224 };
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
225
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
226 private static final ArgConverter ARG_DOUBLE = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
227 public Object convert(Object obj) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
228 if( obj instanceof Double )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
229 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
230 if( obj instanceof Number ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
231 Number n = (Number)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
232 return n.doubleValue();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
233 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
234 if( obj instanceof String ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
235 String s = (String)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
236 try {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
237 return Double.valueOf(s);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
238 } catch(NumberFormatException e) {}
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
239 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
240 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
241 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
242 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
243
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
244 private static final ArgConverter ARG_FLOAT = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
245 public Object convert(Object obj) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
246 if( obj instanceof Float )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
247 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
248 if( obj instanceof Number ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
249 Number n = (Number)obj;
98
3a0ff21f0c96 fix LuanJavaFunction.ARG_FLOAT
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 95
diff changeset
250 return n.floatValue();
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
251 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
252 if( obj instanceof String ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
253 String s = (String)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
254 try {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
255 return Float.valueOf(s);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
256 } catch(NumberFormatException e) {}
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
257 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
258 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
259 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
260 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
261
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
262 private static final ArgConverter ARG_LONG = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
263 public Object convert(Object obj) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
264 if( obj instanceof Long )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
265 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
266 if( obj instanceof Number ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
267 Number n = (Number)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
268 long r = n.longValue();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
269 if( r==n.doubleValue() )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
270 return r;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
271 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
272 else if( obj instanceof String ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
273 String s = (String)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
274 try {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
275 return Long.valueOf(s);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
276 } catch(NumberFormatException e) {}
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
277 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
278 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
279 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
280 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
281
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
282 private static final ArgConverter ARG_INTEGER = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
283 public Object convert(Object obj) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
284 if( obj instanceof Integer )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
285 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
286 if( obj instanceof Number ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
287 Number n = (Number)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
288 int r = n.intValue();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
289 if( r==n.doubleValue() )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
290 return r;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
291 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
292 else if( obj instanceof String ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
293 String s = (String)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
294 try {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
295 return Integer.valueOf(s);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
296 } catch(NumberFormatException e) {}
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
297 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
298 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
299 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
300 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
301
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
302 private static final ArgConverter ARG_SHORT = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
303 public Object convert(Object obj) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
304 if( obj instanceof Short )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
305 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
306 if( obj instanceof Number ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
307 Number n = (Number)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
308 short r = n.shortValue();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
309 if( r==n.doubleValue() )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
310 return r;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
311 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
312 else if( obj instanceof String ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
313 String s = (String)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
314 try {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
315 return Short.valueOf(s);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
316 } catch(NumberFormatException e) {}
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
317 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
318 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
319 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
320 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
321
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
322 private static final ArgConverter ARG_BYTE = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
323 public Object convert(Object obj) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
324 if( obj instanceof Byte )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
325 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
326 if( obj instanceof Number ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
327 Number n = (Number)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
328 byte r = n.byteValue();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
329 if( r==n.doubleValue() )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
330 return r;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
331 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
332 else if( obj instanceof String ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
333 String s = (String)obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
334 try {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
335 return Byte.valueOf(s);
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
336 } catch(NumberFormatException e) {}
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
337 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
338 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
339 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
340 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
341
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
342 private static final ArgConverter ARG_TABLE = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
343 public Object convert(Object obj) {
86
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 69
diff changeset
344 if( obj == null )
6db8f286fa6c _ENV is per module, not global
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 69
diff changeset
345 return null;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
346 if( obj instanceof List ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
347 @SuppressWarnings("unchecked")
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
348 List<Object> list = (List<Object>)obj;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
349 return new LuanTable(list);
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
350 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
351 if( obj instanceof Map ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
352 @SuppressWarnings("unchecked")
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
353 Map<Object,Object> map = (Map<Object,Object>)obj;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
354 return new LuanTable(map);
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
355 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
356 if( obj instanceof Set ) {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
357 @SuppressWarnings("unchecked")
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
358 Set<Object> set = (Set<Object>)obj;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
359 return new LuanTable(set);
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
360 }
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
361 Class cls = obj.getClass();
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
362 if( cls.isArray() && !cls.getComponentType().isPrimitive() ) {
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
363 Object[] a = (Object[])obj;
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
364 return new LuanTable(Arrays.asList(a));
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
365 }
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
366 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
367 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
368 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
369
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
370 private static final ArgConverter ARG_MAP = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
371 public Object convert(Object obj) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
372 if( obj instanceof LuanTable ) {
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
373 LuanTable t = (LuanTable)obj;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
374 return t.asMap();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
375 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
376 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
377 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
378 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
379
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
380 private static final ArgConverter ARG_LIST = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
381 public Object convert(Object obj) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
382 if( obj instanceof LuanTable ) {
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
383 LuanTable t = (LuanTable)obj;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
384 if( t.isList() )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
385 return t.asList();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
386 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
387 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
388 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
389 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
390
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
391 private static final ArgConverter ARG_SET = new ArgConverter() {
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
392 public Object convert(Object obj) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
393 if( obj instanceof LuanTable ) {
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
394 LuanTable t = (LuanTable)obj;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
395 if( t.isSet() )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
396 return t.asSet();
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
397 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
398 return obj;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
399 }
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
400 };
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
401
57
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
402 private static class ArgArray implements ArgConverter {
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
403 private final Object[] a;
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
404
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
405 ArgArray(Class cls) {
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
406 a = (Object[])Array.newInstance(cls.getComponentType(),0);
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
407 }
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
408
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
409 public Object convert(Object obj) {
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
410 if( obj instanceof LuanTable ) {
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
411 LuanTable t = (LuanTable)obj;
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
412 if( t.isList() ) {
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
413 try {
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
414 return t.asList().toArray(a);
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
415 } catch(ArrayStoreException e) {}
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
416 }
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
417 }
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
418 return obj;
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
419 }
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
420 }
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
421
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
422 private static boolean takesLuaState(JavaMethod m) {
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
423 Class<?>[] paramTypes = m.getParameterTypes();
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
424 return paramTypes.length > 0 && paramTypes[0].equals(LuanState.class);
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
425 }
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
426
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
427 private static ArgConverter[] getArgConverters(boolean takesLuaState,JavaMethod m) {
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
428 final boolean isVarArgs = m.isVarArgs();
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
429 Class<?>[] paramTypes = m.getParameterTypes();
21
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
430 if( takesLuaState ) {
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
431 Class<?>[] t = new Class<?>[paramTypes.length-1];
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
432 System.arraycopy(paramTypes,1,t,0,t.length);
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
433 paramTypes = t;
c93d8c781853 add functions
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 12
diff changeset
434 }
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
435 ArgConverter[] a = new ArgConverter[paramTypes.length];
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
436 for( int i=0; i<a.length; i++ ) {
4
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
437 Class<?> paramType = paramTypes[i];
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
438 if( isVarArgs && i == a.length-1 )
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
439 paramType = paramType.getComponentType();
24fd6381caca add to interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 2
diff changeset
440 a[i] = getArgConverter(paramType);
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
441 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
442 return a;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
443 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
444
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
445 private static ArgConverter getArgConverter(Class<?> cls) {
95
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
446 if( cls == Boolean.TYPE )
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
447 return ARG_BOOLEAN;
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
448 if( cls.equals(Boolean.class) )
9aa7d5f20333 in LuanJavaFunction, fix primitive type checking and add boolean conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 88
diff changeset
449 return ARG_BOOLEAN_OBJ;
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
450 if( cls == Double.TYPE || cls.equals(Double.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
451 return ARG_DOUBLE;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
452 if( cls == Float.TYPE || cls.equals(Float.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
453 return ARG_FLOAT;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
454 if( cls == Long.TYPE || cls.equals(Long.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
455 return ARG_LONG;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
456 if( cls == Integer.TYPE || cls.equals(Integer.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
457 return ARG_INTEGER;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
458 if( cls == Short.TYPE || cls.equals(Short.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
459 return ARG_SHORT;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
460 if( cls == Byte.TYPE || cls.equals(Byte.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
461 return ARG_BYTE;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 47
diff changeset
462 if( cls.equals(LuanTable.class) )
47
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
463 return ARG_TABLE;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
464 if( cls.equals(Map.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
465 return ARG_MAP;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
466 if( cls.equals(List.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
467 return ARG_LIST;
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
468 if( cls.equals(Set.class) )
659c7139e903 better conversion
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
469 return ARG_SET;
57
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
470 if( cls.isArray() && !cls.getComponentType().isPrimitive() )
19ca9c4c6c7f auto-convert table to java array
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
471 return new ArgArray(cls);
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
472 return ARG_SAME;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
473 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
474
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
475
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
476
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
477 private static abstract class JavaMethod {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
478 abstract boolean isVarArgs();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
479 abstract Class<?>[] getParameterTypes();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
480 abstract Object invoke(Object obj,Object... args)
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
481 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
482 abstract Class<?> getReturnType();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
483
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
484 static JavaMethod of(final Method m) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
485 return new JavaMethod() {
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
486 @Override boolean isVarArgs() {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
487 return m.isVarArgs();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
488 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
489 @Override Class<?>[] getParameterTypes() {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
490 return m.getParameterTypes();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
491 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
492 @Override Object invoke(Object obj,Object... args)
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
493 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
494 {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
495 return m.invoke(obj,args);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
496 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
497 @Override Class<?> getReturnType() {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
498 return m.getReturnType();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
499 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
500 @Override public String toString() {
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
501 return m.toString();
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
502 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
503 };
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
504 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
505
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
506 static JavaMethod of(final Constructor c) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
507 return new JavaMethod() {
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
508 @Override boolean isVarArgs() {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
509 return c.isVarArgs();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
510 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
511 @Override Class<?>[] getParameterTypes() {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
512 return c.getParameterTypes();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
513 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
514 @Override Object invoke(Object obj,Object... args)
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
515 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
516 {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
517 return c.newInstance(args);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
518 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
519 @Override Class<?> getReturnType() {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
520 return c.getDeclaringClass();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
521 }
43
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
522 @Override public String toString() {
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
523 return c.toString();
80b67b1a653c implement string lib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 40
diff changeset
524 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
525 };
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
526 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
527
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
528 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 21
diff changeset
529
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
530 }