blob: 808930fd33a4c9ee39f53760d830c961ad0049f0 [file] [log] [blame]
Paul Sokolovskye79c6b62015-09-11 17:57:47 +03001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2015 Paul Sokolovsky
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <assert.h>
28#include <string.h>
29#include <errno.h>
30#include <dlfcn.h>
Paul Sokolovsky0eba1622015-09-30 00:54:20 -070031#include <ctype.h>
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030032
33#include "py/nlr.h"
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -070034#include "py/runtime0.h"
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030035#include "py/runtime.h"
36#include "py/binary.h"
37
38#include <jni.h>
39
40#define JJ(call, ...) (*env)->call(env, __VA_ARGS__)
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030041#define JJ1(call) (*env)->call(env)
Paul Sokolovsky02041bf2015-10-09 00:25:26 +030042#define MATCH(s, static) (!strncmp(s, static, sizeof(static) - 1))
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030043
44static JavaVM *jvm;
45static JNIEnv *env;
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +030046static jclass Class_class;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030047static jclass String_class;
48static jmethodID Class_getField_mid;
49static jmethodID Class_getMethods_mid;
50static jmethodID Class_getConstructors_mid;
51static jmethodID Method_getName_mid;
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -070052static jmethodID Object_toString_mid;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030053
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -070054static jclass List_class;
55static jmethodID List_get_mid;
56static jmethodID List_set_mid;
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -070057static jmethodID List_size_mid;
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -070058
Paul Sokolovsky9e0a3d42015-10-02 00:21:07 -070059static jclass IndexException_class;
60
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030061STATIC const mp_obj_type_t jobject_type;
62STATIC const mp_obj_type_t jmethod_type;
63
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030064STATIC mp_obj_t new_jobject(jobject jo);
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +030065STATIC mp_obj_t new_jclass(jclass jc);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030066STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, mp_uint_t n_args, const mp_obj_t *args);
67
68typedef struct _mp_obj_jclass_t {
69 mp_obj_base_t base;
70 jclass cls;
71} mp_obj_jclass_t;
72
73typedef struct _mp_obj_jobject_t {
74 mp_obj_base_t base;
75 jobject obj;
76} mp_obj_jobject_t;
77
78typedef struct _mp_obj_jmethod_t {
79 mp_obj_base_t base;
80 jobject obj;
81 jmethodID meth;
82 qstr name;
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030083 bool is_static;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030084} mp_obj_jmethod_t;
85
Paul Sokolovskye632b1f2015-09-23 07:10:55 -070086// Utility functions
87
88STATIC bool is_object_type(const char *jtypesig) {
89 while (*jtypesig != ' ' && *jtypesig) {
90 if (*jtypesig == '.') {
91 return true;
92 }
93 jtypesig++;
94 }
95 return false;
96}
97
Paul Sokolovskyc4489a02015-10-01 01:19:33 -070098STATIC void check_exception(void) {
99 jobject exc = JJ1(ExceptionOccurred);
100 if (exc) {
101 //JJ1(ExceptionDescribe);
102 mp_obj_t py_e = new_jobject(exc);
103 JJ1(ExceptionClear);
Paul Sokolovsky9e0a3d42015-10-02 00:21:07 -0700104 if (JJ(IsInstanceOf, exc, IndexException_class)) {
105 nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, py_e));
106 }
Paul Sokolovskyc4489a02015-10-01 01:19:33 -0700107 nlr_raise(mp_obj_new_exception_arg1(&mp_type_Exception, py_e));
108 }
109}
110
Paul Sokolovsky1ea4b772015-10-04 01:56:40 +0300111STATIC void print_jobject(const mp_print_t *print, jobject obj) {
112 jobject str_o = JJ(CallObjectMethod, obj, Object_toString_mid);
113 const char *str = JJ(GetStringUTFChars, str_o, NULL);
114 mp_printf(print, str);
115 JJ(ReleaseStringUTFChars, str_o, str);
116}
117
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300118// jclass
119
120STATIC void jclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300121 mp_obj_jclass_t *self = self_in;
Paul Sokolovsky1ea4b772015-10-04 01:56:40 +0300122 if (kind == PRINT_REPR) {
123 mp_printf(print, "<jclass @%p \"", self->cls);
124 }
125 print_jobject(print, self->cls);
126 if (kind == PRINT_REPR) {
127 mp_printf(print, "\">");
128 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300129}
130
131STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
132 if (dest[0] == MP_OBJ_NULL) {
133 // load attribute
134 mp_obj_jclass_t *self = self_in;
135 const char *attr = qstr_str(attr_in);
136
137 jstring field_name = JJ(NewStringUTF, attr);
138 jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name);
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300139 if (!JJ1(ExceptionCheck)) {
140 jfieldID field_id = JJ(FromReflectedField, field);
141 jobject obj = JJ(GetStaticObjectField, self->cls, field_id);
142 dest[0] = new_jobject(obj);
143 return;
144 }
145 //JJ1(ExceptionDescribe);
146 JJ1(ExceptionClear);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300147
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300148 mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
149 o->base.type = &jmethod_type;
150 o->name = attr_in;
151 o->meth = NULL;
152 o->obj = self->cls;
153 o->is_static = true;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300154 dest[0] = o;
155 }
156}
157
158STATIC mp_obj_t jclass_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
159 if (n_kw != 0) {
160 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported"));
161 }
162 mp_obj_jclass_t *self = self_in;
163
164 jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid);
165
166 return call_method(self->cls, NULL, methods, true, n_args, args);
167}
168
169STATIC const mp_map_elem_t jclass_locals_dict_table[] = {
170// { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&ffivar_get_obj },
171// { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&ffivar_set_obj },
172};
173
174STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table);
175
176STATIC const mp_obj_type_t jclass_type = {
177 { &mp_type_type },
178 .name = MP_QSTR_jclass,
179 .print = jclass_print,
180 .attr = jclass_attr,
181 .call = jclass_call,
182 .locals_dict = (mp_obj_t)&jclass_locals_dict,
183};
184
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300185STATIC mp_obj_t new_jclass(jclass jc) {
186 mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
187 o->base.type = &jclass_type;
188 o->cls = jc;
189 return o;
190}
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300191
192// jobject
193
194STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300195 mp_obj_jobject_t *self = self_in;
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700196 if (kind == PRINT_REPR) {
197 mp_printf(print, "<jobject @%p \"", self->obj);
198 }
Paul Sokolovsky1ea4b772015-10-04 01:56:40 +0300199 print_jobject(print, self->obj);
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700200 if (kind == PRINT_REPR) {
201 mp_printf(print, "\">");
202 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300203}
204
205STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
206 if (dest[0] == MP_OBJ_NULL) {
207 // load attribute
208 mp_obj_jobject_t *self = self_in;
209
Paul Sokolovsky216b6a42015-10-08 16:55:59 +0300210 const char *attr = qstr_str(attr_in);
211 jclass obj_class = JJ(GetObjectClass, self->obj);
212 jstring field_name = JJ(NewStringUTF, attr);
213 jobject field = JJ(CallObjectMethod, obj_class, Class_getField_mid, field_name);
214 JJ(DeleteLocalRef, field_name);
215 JJ(DeleteLocalRef, obj_class);
216 if (!JJ1(ExceptionCheck)) {
217 jfieldID field_id = JJ(FromReflectedField, field);
218 JJ(DeleteLocalRef, field);
219 jobject obj = JJ(GetObjectField, self->obj, field_id);
220 dest[0] = new_jobject(obj);
221 return;
222 }
223 //JJ1(ExceptionDescribe);
224 JJ1(ExceptionClear);
225
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300226 mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
227 o->base.type = &jmethod_type;
228 o->name = attr_in;
229 o->meth = NULL;
230 o->obj = self->obj;
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300231 o->is_static = false;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300232 dest[0] = o;
233 }
234}
235
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700236STATIC mp_obj_t jobject_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
237 mp_obj_jobject_t *self = self_in;
238 if (!JJ(IsInstanceOf, self->obj, List_class)) {
239 return MP_OBJ_NULL;
240 }
241
242 mp_uint_t idx = mp_obj_get_int(index);
243
244 if (value == MP_OBJ_NULL) {
245 // delete
246 assert(0);
247 } else if (value == MP_OBJ_SENTINEL) {
248 // load
249 jobject el = JJ(CallObjectMethod, self->obj, List_get_mid, idx);
Paul Sokolovskyfd387992015-10-07 07:39:41 +0300250 check_exception();
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700251 return new_jobject(el);
252 } else {
253 // store
254 assert(0);
255 }
256
257
258return MP_OBJ_NULL;
259}
260
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700261STATIC mp_obj_t jobject_unary_op(mp_uint_t op, mp_obj_t self_in) {
262 mp_obj_jobject_t *self = self_in;
263 switch (op) {
264 case MP_UNARY_OP_BOOL:
265 case MP_UNARY_OP_LEN: {
266 jint len = JJ(CallIntMethod, self->obj, List_size_mid);
267 if (op == MP_UNARY_OP_BOOL) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300268 return mp_obj_new_bool(len != 0);
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700269 }
270 return MP_OBJ_NEW_SMALL_INT(len);
271 }
272 default:
273 return MP_OBJ_NULL; // op not supported
274 }
275}
276
Paul Sokolovskyfe29cc12015-10-14 00:35:28 +0300277// TODO: subscr_load_adaptor & subscr_getiter convenience functions
278// should be moved to common location for reuse.
279STATIC mp_obj_t subscr_load_adaptor(mp_obj_t self_in, mp_obj_t index_in) {
280 return mp_obj_subscr(self_in, index_in, MP_OBJ_SENTINEL);
281}
282MP_DEFINE_CONST_FUN_OBJ_2(subscr_load_adaptor_obj, subscr_load_adaptor);
283
284// .getiter special method which returns iterator which works in terms
285// of object subscription.
286STATIC mp_obj_t subscr_getiter(mp_obj_t self_in) {
287 mp_obj_t dest[2] = {(mp_obj_t)&subscr_load_adaptor_obj, self_in};
288 return mp_obj_new_getitem_iter(dest);
289}
290
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300291STATIC const mp_obj_type_t jobject_type = {
292 { &mp_type_type },
293 .name = MP_QSTR_jobject,
294 .print = jobject_print,
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700295 .unary_op = jobject_unary_op,
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300296 .attr = jobject_attr,
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700297 .subscr = jobject_subscr,
Paul Sokolovskyfe29cc12015-10-14 00:35:28 +0300298 .getiter = subscr_getiter,
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300299// .locals_dict = (mp_obj_t)&jobject_locals_dict,
300};
301
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300302STATIC mp_obj_t new_jobject(jobject jo) {
Paul Sokolovsky7e18d3b2015-09-24 15:29:22 -0700303 if (jo == NULL) {
304 return mp_const_none;
305 } else if (JJ(IsInstanceOf, jo, String_class)) {
Paul Sokolovskyed22e9b2015-09-22 17:01:01 -0700306 const char *s = JJ(GetStringUTFChars, jo, NULL);
307 mp_obj_t ret = mp_obj_new_str(s, strlen(s), false);
308 JJ(ReleaseStringUTFChars, jo, s);
309 return ret;
310 } else if (JJ(IsInstanceOf, jo, Class_class)) {
311 return new_jclass(jo);
312 } else {
313 mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t);
314 o->base.type = &jobject_type;
315 o->obj = jo;
316 return o;
317 }
318
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300319}
320
321
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300322// jmethod
323
324STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
325 (void)kind;
326 mp_obj_jmethod_t *self = self_in;
327 // Variable value printed as cast to int
328 mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
329}
330
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300331#define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300332
333#define CHECK_TYPE(java_type_name) \
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300334 if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
335 return false; \
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300336 } \
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300337 arg_type += sizeof(java_type_name) - 1;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300338
339STATIC const char *strprev(const char *s, char c) {
340 while (*s != c) {
341 s--;
342 }
343 return s;
344}
345
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300346STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
347 const char *arg_type = *jtypesig;
348 mp_obj_type_t *type = mp_obj_get_type(arg);
349
350 if (type == &mp_type_str) {
351 if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) {
352 out->l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
353 } else {
354 return false;
355 }
356 } else if (type == &mp_type_int) {
Paul Sokolovsky011c7f52015-09-18 13:19:50 +0300357 if (IMATCH(arg_type, "int") || IMATCH(arg_type, "long")) {
358 // TODO: Java long is 64-bit actually
359 out->j = mp_obj_get_int(arg);
360 } else {
361 return false;
362 }
Paul Sokolovsky1e9d8e12015-09-19 01:04:41 +0300363 } else if (type == &jobject_type) {
364 printf("TODO: Check java arg type!!\n");
Paul Sokolovsky0eba1622015-09-30 00:54:20 -0700365 while (isalpha(*arg_type) || *arg_type == '.') {
366 arg_type++;
367 }
Paul Sokolovsky1e9d8e12015-09-19 01:04:41 +0300368 mp_obj_jobject_t *jo = arg;
369 out->l = jo->obj;
Paul Sokolovsky7381b7a2015-10-10 01:19:28 +0300370 } else if (type == &mp_type_bool) {
371 if (IMATCH(arg_type, "boolean")) {
372 out->z = arg == mp_const_true;
373 } else {
374 return false;
375 }
376 } else if (arg == mp_const_none) {
377 //printf("TODO: Check java arg type!!\n");
378 while (isalpha(*arg_type) || *arg_type == '.') {
379 arg_type++;
380 }
381 out->l = NULL;
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300382 } else {
383 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported"));
384 }
385
386 *jtypesig = arg_type;
387 return true;
388}
389
Paul Sokolovsky02041bf2015-10-09 00:25:26 +0300390#if 0
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300391// jvalue is known to be union of jobject and friends. And yet from C's
392// perspective, it's aggregate object which may require passing via stack
393// instead of registers. Work that around by passing jobject and typecasting
394// it.
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300395STATIC mp_obj_t jvalue2py(const char *jtypesig, jobject arg) {
Paul Sokolovsky51673322015-09-14 00:15:08 +0300396 if (arg == NULL || MATCH(jtypesig, "void")) {
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300397 return mp_const_none;
Paul Sokolovsky6196aa42015-09-20 00:36:26 +0300398 } else if (MATCH(jtypesig, "boolean")) {
399 return mp_obj_new_bool((bool)arg);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300400 } else if (MATCH(jtypesig, "int")) {
401 return mp_obj_new_int((mp_int_t)arg);
Paul Sokolovskye632b1f2015-09-23 07:10:55 -0700402 } else if (is_object_type(jtypesig)) {
403 // Non-primitive, object type
404 return new_jobject(arg);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300405 }
406
Paul Sokolovskye632b1f2015-09-23 07:10:55 -0700407 printf("Unknown return type: %s\n", jtypesig);
Paul Sokolovskyb230a862015-09-15 14:07:12 +0300408
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300409 return MP_OBJ_NULL;
410}
Paul Sokolovsky02041bf2015-10-09 00:25:26 +0300411#endif
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300412
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300413STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, mp_uint_t n_args, const mp_obj_t *args) {
414 jvalue jargs[n_args];
415// printf("methods=%p\n", methods);
416 jsize num_methods = JJ(GetArrayLength, methods);
417 for (int i = 0; i < num_methods; i++) {
418 jobject meth = JJ(GetObjectArrayElement, methods, i);
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700419 jobject name_o = JJ(CallObjectMethod, meth, Object_toString_mid);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300420 const char *decl = JJ(GetStringUTFChars, name_o, NULL);
421 const char *arg_types = strchr(decl, '(') + 1;
422 //const char *arg_types_end = strchr(arg_types, ')');
423// printf("method[%d]=%p %s\n", i, meth, decl);
424
425 const char *meth_name = NULL;
426 const char *ret_type = NULL;
427 if (!is_constr) {
428 meth_name = strprev(arg_types, '.') + 1;
429 ret_type = strprev(meth_name, ' ') - 1;
430 ret_type = strprev(ret_type, ' ') + 1;
431
432 int name_len = strlen(name);
433 if (strncmp(name, meth_name, name_len/*arg_types - meth_name - 1*/) || meth_name[name_len] != '('/*(*/) {
Paul Sokolovsky81d64ab2015-09-26 08:50:37 -0700434 goto next_method;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300435 }
436 }
437// printf("method[%d]=%p %s\n", i, meth, decl);
438// printf("!!!%s\n", arg_types);
439// printf("name=%p meth_name=%s\n", name, meth_name);
440
441 bool found = true;
Paul Sokolovsky0eba1622015-09-30 00:54:20 -0700442 for (int i = 0; i < n_args && *arg_types != ')'; i++) {
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300443 if (!py2jvalue(&arg_types, args[i], &jargs[i])) {
444 goto next_method;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300445 }
446
447 if (*arg_types == ',') {
448 arg_types++;
449 }
450 }
451
Paul Sokolovsky0eba1622015-09-30 00:54:20 -0700452 if (*arg_types != ')') {
453 goto next_method;
454 }
455
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300456 if (found) {
457// printf("found!\n");
458 jmethodID method_id = JJ(FromReflectedMethod, meth);
459 jobject res;
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700460 mp_obj_t ret;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300461 if (is_constr) {
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300462 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700463 res = JJ(NewObjectA, obj, method_id, jargs);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300464 return new_jobject(res);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300465 } else {
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700466 if (MATCH(ret_type, "void")) {
467 JJ(CallVoidMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300468 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700469 ret = mp_const_none;
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700470 } else if (MATCH(ret_type, "int")) {
471 jint res = JJ(CallIntMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300472 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700473 ret = mp_obj_new_int(res);
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700474 } else if (MATCH(ret_type, "boolean")) {
475 jboolean res = JJ(CallBooleanMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300476 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700477 ret = mp_obj_new_bool(res);
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700478 } else if (is_object_type(ret_type)) {
479 res = JJ(CallObjectMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300480 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700481 ret = new_jobject(res);
482 } else {
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700483 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700484 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "cannot handle return type"));
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300485 }
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700486
487 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky77020282015-09-28 08:36:43 -0700488 JJ(DeleteLocalRef, name_o);
489 JJ(DeleteLocalRef, meth);
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700490 return ret;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300491 }
492 }
493
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300494next_method:
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300495 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky77020282015-09-28 08:36:43 -0700496 JJ(DeleteLocalRef, name_o);
497 JJ(DeleteLocalRef, meth);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300498 }
499
500 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "method not found"));
501}
502
503
504STATIC mp_obj_t jmethod_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
505 if (n_kw != 0) {
506 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported"));
507 }
508 mp_obj_jmethod_t *self = self_in;
509
510 const char *name = qstr_str(self->name);
511// jstring meth_name = JJ(NewStringUTF, name);
512
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300513 jclass obj_class = self->obj;
514 if (!self->is_static) {
515 obj_class = JJ(GetObjectClass, self->obj);
516 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300517 jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid);
518
519 return call_method(self->obj, name, methods, false, n_args, args);
520}
521
522STATIC const mp_obj_type_t jmethod_type = {
523 { &mp_type_type },
524 .name = MP_QSTR_jmethod,
525 .print = jmethod_print,
526 .call = jmethod_call,
527// .attr = jobject_attr,
528// .locals_dict = (mp_obj_t)&jobject_locals_dict,
529};
530
531#ifdef __ANDROID__
532#define LIBJVM_SO "libdvm.so"
533#else
534#define LIBJVM_SO "libjvm.so"
535#endif
536
537STATIC void create_jvm() {
538 JavaVMInitArgs args;
539 JavaVMOption options;
540 options.optionString = "-Djava.class.path=.";
541 args.version = JNI_VERSION_1_6;
542 args.nOptions = 1;
543 args.options = &options;
544 args.ignoreUnrecognized = 0;
545
546 if (env) {
547 return;
548 }
549
550 void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL);
551 if (!libjvm) {
552 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH"));
553 }
554 int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM");
555
556 int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args);
557 if (st < 0 || !env) {
558 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to create JVM"));
559 }
560
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300561 Class_class = JJ(FindClass, "java/lang/Class");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300562 jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
563 String_class = JJ(FindClass, "java/lang/String");
564
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700565 jclass Object_class = JJ(FindClass, "java/lang/Object");
566 Object_toString_mid = JJ(GetMethodID, Object_class, "toString",
567 "()Ljava/lang/String;");
568
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300569 Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300570 "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300571 Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300572 "()[Ljava/lang/reflect/Method;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300573 Class_getConstructors_mid = (*env)->GetMethodID(env, Class_class, "getConstructors",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300574 "()[Ljava/lang/reflect/Constructor;");
575 Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
576 "()Ljava/lang/String;");
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700577
578 List_class = JJ(FindClass, "java/util/List");
579 List_get_mid = JJ(GetMethodID, List_class, "get",
580 "(I)Ljava/lang/Object;");
581 List_set_mid = JJ(GetMethodID, List_class, "set",
582 "(ILjava/lang/Object;)Ljava/lang/Object;");
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700583 List_size_mid = JJ(GetMethodID, List_class, "size",
584 "()I");
Paul Sokolovsky9e0a3d42015-10-02 00:21:07 -0700585 IndexException_class = JJ(FindClass, "java/lang/IndexOutOfBoundsException");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300586}
587
588STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) {
589 const char *cls_name = mp_obj_str_get_str(cls_name_in);
590 if (!env) {
591 create_jvm();
592 }
593 jclass cls = JJ(FindClass, cls_name);
594
595 mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
596 o->base.type = &jclass_type;
597 o->cls = cls;
598 return o;
599}
600MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
601
Paul Sokolovskycb6cf5e2015-09-16 01:09:39 +0300602STATIC mp_obj_t mod_jni_env() {
603 return mp_obj_new_int((mp_int_t)env);
604}
605MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
606
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300607STATIC const mp_map_elem_t mp_module_jni_globals_table[] = {
608 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_jni) },
609 { MP_OBJ_NEW_QSTR(MP_QSTR_cls), (mp_obj_t)&mod_jni_cls_obj },
Paul Sokolovskycb6cf5e2015-09-16 01:09:39 +0300610 { MP_OBJ_NEW_QSTR(MP_QSTR_env), (mp_obj_t)&mod_jni_env_obj },
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300611};
612
613STATIC MP_DEFINE_CONST_DICT(mp_module_jni_globals, mp_module_jni_globals_table);
614
615const mp_obj_module_t mp_module_jni = {
616 .base = { &mp_type_module },
617 .name = MP_QSTR_jni,
618 .globals = (mp_obj_dict_t*)&mp_module_jni_globals,
619};