comparison src/luan/LuanJavaFunction.java @ 111:2428ecfed375

change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection git-svn-id: https://luan-java.googlecode.com/svn/trunk@112 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 20:40:05 +0000
parents 3a0ff21f0c96
children 8c706d6eb5dc
comparison
equal deleted inserted replaced
110:7afa6df829f3 111:2428ecfed375
47 47
48 public Class<?>[] getParameterTypes() { 48 public Class<?>[] getParameterTypes() {
49 return method.getParameterTypes(); 49 return method.getParameterTypes();
50 } 50 }
51 51
52 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { 52 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
53 args = fixArgs(luan,args); 53 args = fixArgs(luan,args);
54 try { 54 try {
55 return doCall(luan,args); 55 return doCall(luan,args);
56 } catch(IllegalArgumentException e) { 56 } catch(IllegalArgumentException e) {
57 checkArgs(luan,args); 57 checkArgs(luan,args);
58 throw e; 58 throw e;
59 } 59 }
60 } 60 }
61 61
62 public Object[] rawCall(LuanState luan,Object[] args) throws LuanException { 62 public Object rawCall(LuanState luan,Object[] args) throws LuanException {
63 args = fixArgs(luan,args); 63 args = fixArgs(luan,args);
64 return doCall(luan,args); 64 return doCall(luan,args);
65 } 65 }
66 66
67 private Object[] doCall(LuanState luan,Object[] args) throws LuanException { 67 private Object doCall(LuanState luan,Object[] args) throws LuanException {
68 Object rtn; 68 Object rtn;
69 try { 69 try {
70 rtn = method.invoke(obj,args); 70 rtn = method.invoke(obj,args);
71 } catch(IllegalAccessException e) { 71 } catch(IllegalAccessException e) {
72 throw new RuntimeException(e); 72 throw new RuntimeException(e);
156 return rtn; 156 return rtn;
157 } 157 }
158 158
159 159
160 private interface RtnConverter { 160 private interface RtnConverter {
161 public Object[] convert(Object obj); 161 public Object convert(Object obj);
162 } 162 }
163 163
164 private static final RtnConverter RTN_EMPTY = new RtnConverter() { 164 private static final RtnConverter RTN_EMPTY = new RtnConverter() {
165 public Object[] convert(Object obj) { 165 @Override public Object[] convert(Object obj) {
166 return EMPTY; 166 return EMPTY;
167 } 167 }
168 }; 168 };
169 169
170 private static final RtnConverter RTN_ARRAY = new RtnConverter() { 170 private static final RtnConverter RTN_SAME = new RtnConverter() {
171 public Object[] convert(Object obj) { 171 @Override public Object convert(Object obj) {
172 return obj;
173 }
174 };
175
176 private static final RtnConverter RTN_NUMBER_ARRAY = new RtnConverter() {
177 @Override public Object convert(Object obj) {
172 if( obj == null ) 178 if( obj == null )
173 return NULL_RTN; 179 return null;
174 return (Object[])obj;
175 }
176 };
177
178 private static final RtnConverter RTN_ONE = new RtnConverter() {
179 public Object[] convert(Object obj) {
180 return new Object[]{obj};
181 }
182 };
183
184 private static final Object[] NULL_RTN = new Object[1];
185
186 private static final RtnConverter RTN_NUMBER_ARRAY = new RtnConverter() {
187 public Object[] convert(Object obj) {
188 if( obj == null )
189 return NULL_RTN;
190 Object[] rtn = new Object[Array.getLength(obj)]; 180 Object[] rtn = new Object[Array.getLength(obj)];
191 for( int i=0; i<rtn.length; i++ ) { 181 for( int i=0; i<rtn.length; i++ ) {
192 rtn[i] = Array.get(obj,i); 182 rtn[i] = Array.get(obj,i);
193 } 183 }
194 return rtn; 184 return rtn;
197 187
198 private static RtnConverter getRtnConverter(JavaMethod m) { 188 private static RtnConverter getRtnConverter(JavaMethod m) {
199 Class<?> rtnType = m.getReturnType(); 189 Class<?> rtnType = m.getReturnType();
200 if( rtnType == Void.TYPE ) 190 if( rtnType == Void.TYPE )
201 return RTN_EMPTY; 191 return RTN_EMPTY;
202 if( rtnType.isArray() ) { 192 if( rtnType.isArray() && isNumber(rtnType.getComponentType()) )
203 rtnType = rtnType.getComponentType(); 193 return RTN_NUMBER_ARRAY;
204 if( isNumber(rtnType) ) 194 return RTN_SAME;
205 return RTN_NUMBER_ARRAY;
206 return RTN_ARRAY;
207 }
208 return RTN_ONE;
209 } 195 }
210 196
211 private static boolean isNumber(Class<?> rtnType) { 197 private static boolean isNumber(Class<?> rtnType) {
212 return rtnType == Byte.TYPE 198 return rtnType == Byte.TYPE
213 || rtnType == Short.TYPE 199 || rtnType == Short.TYPE