blob: df9cd9d67a2dc59080566d94d5275a6218d97632 [file] [log] [blame]
Paul Sokolovskye79c6b62015-09-11 17:57:47 +03001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Paul Sokolovskye79c6b62015-09-11 17:57:47 +03003 *
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;
Paul Sokolovsky3c7e1b82015-11-11 16:43:27 +020048static jmethodID Class_getName_mid;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030049static jmethodID Class_getField_mid;
50static jmethodID Class_getMethods_mid;
51static jmethodID Class_getConstructors_mid;
52static jmethodID Method_getName_mid;
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -070053static jmethodID Object_toString_mid;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030054
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -070055static jclass List_class;
56static jmethodID List_get_mid;
57static jmethodID List_set_mid;
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -070058static jmethodID List_size_mid;
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -070059
Paul Sokolovsky9e0a3d42015-10-02 00:21:07 -070060static jclass IndexException_class;
61
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030062STATIC const mp_obj_type_t jobject_type;
63STATIC const mp_obj_type_t jmethod_type;
64
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030065STATIC mp_obj_t new_jobject(jobject jo);
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +030066STATIC mp_obj_t new_jclass(jclass jc);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030067STATIC 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);
Paul Sokolovsky3fea1f02016-09-16 00:59:48 +030068STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030069
70typedef struct _mp_obj_jclass_t {
71 mp_obj_base_t base;
72 jclass cls;
73} mp_obj_jclass_t;
74
75typedef struct _mp_obj_jobject_t {
76 mp_obj_base_t base;
77 jobject obj;
78} mp_obj_jobject_t;
79
80typedef struct _mp_obj_jmethod_t {
81 mp_obj_base_t base;
82 jobject obj;
83 jmethodID meth;
84 qstr name;
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030085 bool is_static;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030086} mp_obj_jmethod_t;
87
Paul Sokolovskye632b1f2015-09-23 07:10:55 -070088// Utility functions
89
90STATIC bool is_object_type(const char *jtypesig) {
91 while (*jtypesig != ' ' && *jtypesig) {
92 if (*jtypesig == '.') {
93 return true;
94 }
95 jtypesig++;
96 }
97 return false;
98}
99
Paul Sokolovskyc4489a02015-10-01 01:19:33 -0700100STATIC void check_exception(void) {
101 jobject exc = JJ1(ExceptionOccurred);
102 if (exc) {
103 //JJ1(ExceptionDescribe);
104 mp_obj_t py_e = new_jobject(exc);
105 JJ1(ExceptionClear);
Paul Sokolovsky9e0a3d42015-10-02 00:21:07 -0700106 if (JJ(IsInstanceOf, exc, IndexException_class)) {
107 nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, py_e));
108 }
Paul Sokolovskyc4489a02015-10-01 01:19:33 -0700109 nlr_raise(mp_obj_new_exception_arg1(&mp_type_Exception, py_e));
110 }
111}
112
Paul Sokolovsky1ea4b772015-10-04 01:56:40 +0300113STATIC void print_jobject(const mp_print_t *print, jobject obj) {
114 jobject str_o = JJ(CallObjectMethod, obj, Object_toString_mid);
115 const char *str = JJ(GetStringUTFChars, str_o, NULL);
116 mp_printf(print, str);
117 JJ(ReleaseStringUTFChars, str_o, str);
118}
119
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300120// jclass
121
122STATIC 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 +0300123 mp_obj_jclass_t *self = self_in;
Paul Sokolovsky1ea4b772015-10-04 01:56:40 +0300124 if (kind == PRINT_REPR) {
125 mp_printf(print, "<jclass @%p \"", self->cls);
126 }
127 print_jobject(print, self->cls);
128 if (kind == PRINT_REPR) {
129 mp_printf(print, "\">");
130 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300131}
132
133STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
134 if (dest[0] == MP_OBJ_NULL) {
135 // load attribute
136 mp_obj_jclass_t *self = self_in;
137 const char *attr = qstr_str(attr_in);
138
139 jstring field_name = JJ(NewStringUTF, attr);
140 jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name);
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300141 if (!JJ1(ExceptionCheck)) {
142 jfieldID field_id = JJ(FromReflectedField, field);
143 jobject obj = JJ(GetStaticObjectField, self->cls, field_id);
144 dest[0] = new_jobject(obj);
145 return;
146 }
147 //JJ1(ExceptionDescribe);
148 JJ1(ExceptionClear);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300149
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300150 mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
151 o->base.type = &jmethod_type;
152 o->name = attr_in;
153 o->meth = NULL;
154 o->obj = self->cls;
155 o->is_static = true;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300156 dest[0] = o;
157 }
158}
159
Paul Sokolovskyeac22e22017-01-04 16:10:42 +0300160STATIC mp_obj_t jclass_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300161 if (n_kw != 0) {
Damien George48d867b2017-06-15 11:54:41 +1000162 mp_raise_TypeError("kwargs not supported");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300163 }
164 mp_obj_jclass_t *self = self_in;
165
166 jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid);
167
168 return call_method(self->cls, NULL, methods, true, n_args, args);
169}
170
Paul Sokolovskye2801222017-07-30 10:03:14 +0300171STATIC const mp_rom_map_elem_t jclass_locals_dict_table[] = {
172// { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&ffivar_get_obj) },
173// { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&ffivar_set_obj) },
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300174};
175
176STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table);
177
178STATIC const mp_obj_type_t jclass_type = {
179 { &mp_type_type },
180 .name = MP_QSTR_jclass,
181 .print = jclass_print,
182 .attr = jclass_attr,
183 .call = jclass_call,
Paul Sokolovskye2801222017-07-30 10:03:14 +0300184 .locals_dict = (mp_obj_dict_t*)&jclass_locals_dict,
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300185};
186
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300187STATIC mp_obj_t new_jclass(jclass jc) {
188 mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
189 o->base.type = &jclass_type;
190 o->cls = jc;
191 return o;
192}
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300193
194// jobject
195
196STATIC 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 +0300197 mp_obj_jobject_t *self = self_in;
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700198 if (kind == PRINT_REPR) {
199 mp_printf(print, "<jobject @%p \"", self->obj);
200 }
Paul Sokolovsky1ea4b772015-10-04 01:56:40 +0300201 print_jobject(print, self->obj);
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700202 if (kind == PRINT_REPR) {
203 mp_printf(print, "\">");
204 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300205}
206
207STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
208 if (dest[0] == MP_OBJ_NULL) {
209 // load attribute
210 mp_obj_jobject_t *self = self_in;
211
Paul Sokolovsky216b6a42015-10-08 16:55:59 +0300212 const char *attr = qstr_str(attr_in);
213 jclass obj_class = JJ(GetObjectClass, self->obj);
214 jstring field_name = JJ(NewStringUTF, attr);
215 jobject field = JJ(CallObjectMethod, obj_class, Class_getField_mid, field_name);
216 JJ(DeleteLocalRef, field_name);
217 JJ(DeleteLocalRef, obj_class);
218 if (!JJ1(ExceptionCheck)) {
219 jfieldID field_id = JJ(FromReflectedField, field);
220 JJ(DeleteLocalRef, field);
221 jobject obj = JJ(GetObjectField, self->obj, field_id);
222 dest[0] = new_jobject(obj);
223 return;
224 }
225 //JJ1(ExceptionDescribe);
226 JJ1(ExceptionClear);
227
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300228 mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
229 o->base.type = &jmethod_type;
230 o->name = attr_in;
231 o->meth = NULL;
232 o->obj = self->obj;
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300233 o->is_static = false;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300234 dest[0] = o;
235 }
236}
237
Paul Sokolovsky3c7e1b82015-11-11 16:43:27 +0200238STATIC void get_jclass_name(jobject obj, char *buf) {
239 jclass obj_class = JJ(GetObjectClass, obj);
240 jstring name = JJ(CallObjectMethod, obj_class, Class_getName_mid);
241 jint len = JJ(GetStringLength, name);
242 JJ(GetStringUTFRegion, name, 0, len, buf);
243 check_exception();
244}
245
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700246STATIC mp_obj_t jobject_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
247 mp_obj_jobject_t *self = self_in;
Paul Sokolovsky3fea1f02016-09-16 00:59:48 +0300248 mp_uint_t idx = mp_obj_get_int(index);
249 char class_name[64];
250 get_jclass_name(self->obj, class_name);
251 //printf("class: %s\n", class_name);
252
253 if (class_name[0] == '[') {
254 if (class_name[1] == 'L' || class_name[1] == '[') {
255 if (value == MP_OBJ_NULL) {
256 // delete
257 assert(0);
258 } else if (value == MP_OBJ_SENTINEL) {
259 // load
260 jobject el = JJ(GetObjectArrayElement, self->obj, idx);
261 return new_jobject(el);
262 } else {
263 // store
264 jvalue jval;
265 const char *t = class_name + 1;
266 py2jvalue(&t, value, &jval);
267 JJ(SetObjectArrayElement, self->obj, idx, jval.l);
268 return mp_const_none;
269 }
270 }
Javier Candeira35a1fea2017-08-09 14:40:45 +1000271 mp_raise_NotImplementedError("");
Paul Sokolovsky3fea1f02016-09-16 00:59:48 +0300272 }
273
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700274 if (!JJ(IsInstanceOf, self->obj, List_class)) {
275 return MP_OBJ_NULL;
276 }
277
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700278
279 if (value == MP_OBJ_NULL) {
280 // delete
281 assert(0);
282 } else if (value == MP_OBJ_SENTINEL) {
283 // load
284 jobject el = JJ(CallObjectMethod, self->obj, List_get_mid, idx);
Paul Sokolovskyfd387992015-10-07 07:39:41 +0300285 check_exception();
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700286 return new_jobject(el);
287 } else {
288 // store
289 assert(0);
290 }
291
292
293return MP_OBJ_NULL;
294}
295
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700296STATIC mp_obj_t jobject_unary_op(mp_uint_t op, mp_obj_t self_in) {
297 mp_obj_jobject_t *self = self_in;
298 switch (op) {
299 case MP_UNARY_OP_BOOL:
300 case MP_UNARY_OP_LEN: {
301 jint len = JJ(CallIntMethod, self->obj, List_size_mid);
302 if (op == MP_UNARY_OP_BOOL) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300303 return mp_obj_new_bool(len != 0);
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700304 }
305 return MP_OBJ_NEW_SMALL_INT(len);
306 }
307 default:
308 return MP_OBJ_NULL; // op not supported
309 }
310}
311
Paul Sokolovskyfe29cc12015-10-14 00:35:28 +0300312// TODO: subscr_load_adaptor & subscr_getiter convenience functions
313// should be moved to common location for reuse.
314STATIC mp_obj_t subscr_load_adaptor(mp_obj_t self_in, mp_obj_t index_in) {
315 return mp_obj_subscr(self_in, index_in, MP_OBJ_SENTINEL);
316}
317MP_DEFINE_CONST_FUN_OBJ_2(subscr_load_adaptor_obj, subscr_load_adaptor);
318
319// .getiter special method which returns iterator which works in terms
320// of object subscription.
321STATIC mp_obj_t subscr_getiter(mp_obj_t self_in) {
322 mp_obj_t dest[2] = {(mp_obj_t)&subscr_load_adaptor_obj, self_in};
323 return mp_obj_new_getitem_iter(dest);
324}
325
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300326STATIC const mp_obj_type_t jobject_type = {
327 { &mp_type_type },
328 .name = MP_QSTR_jobject,
329 .print = jobject_print,
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700330 .unary_op = jobject_unary_op,
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300331 .attr = jobject_attr,
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700332 .subscr = jobject_subscr,
Paul Sokolovskyfe29cc12015-10-14 00:35:28 +0300333 .getiter = subscr_getiter,
Paul Sokolovskye2801222017-07-30 10:03:14 +0300334// .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict,
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300335};
336
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300337STATIC mp_obj_t new_jobject(jobject jo) {
Paul Sokolovsky7e18d3b2015-09-24 15:29:22 -0700338 if (jo == NULL) {
339 return mp_const_none;
340 } else if (JJ(IsInstanceOf, jo, String_class)) {
Paul Sokolovskyed22e9b2015-09-22 17:01:01 -0700341 const char *s = JJ(GetStringUTFChars, jo, NULL);
342 mp_obj_t ret = mp_obj_new_str(s, strlen(s), false);
343 JJ(ReleaseStringUTFChars, jo, s);
344 return ret;
345 } else if (JJ(IsInstanceOf, jo, Class_class)) {
346 return new_jclass(jo);
347 } else {
348 mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t);
349 o->base.type = &jobject_type;
350 o->obj = jo;
351 return o;
352 }
353
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300354}
355
356
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300357// jmethod
358
359STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
360 (void)kind;
361 mp_obj_jmethod_t *self = self_in;
362 // Variable value printed as cast to int
363 mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
364}
365
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300366#define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300367
368#define CHECK_TYPE(java_type_name) \
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300369 if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
370 return false; \
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300371 } \
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300372 arg_type += sizeof(java_type_name) - 1;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300373
374STATIC const char *strprev(const char *s, char c) {
375 while (*s != c) {
376 s--;
377 }
378 return s;
379}
380
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300381STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
382 const char *arg_type = *jtypesig;
383 mp_obj_type_t *type = mp_obj_get_type(arg);
384
385 if (type == &mp_type_str) {
386 if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) {
387 out->l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
388 } else {
389 return false;
390 }
391 } else if (type == &mp_type_int) {
Paul Sokolovsky011c7f52015-09-18 13:19:50 +0300392 if (IMATCH(arg_type, "int") || IMATCH(arg_type, "long")) {
393 // TODO: Java long is 64-bit actually
394 out->j = mp_obj_get_int(arg);
395 } else {
396 return false;
397 }
Paul Sokolovsky1e9d8e12015-09-19 01:04:41 +0300398 } else if (type == &jobject_type) {
Paul Sokolovsky9ebd4da2015-10-22 01:34:03 +0300399 bool is_object = false;
Paul Sokolovsky91f21682015-10-23 00:33:03 +0300400 const char *expected_type = arg_type;
Paul Sokolovsky9ebd4da2015-10-22 01:34:03 +0300401 while (1) {
402 if (isalpha(*arg_type)) {
403 } else if (*arg_type == '.') {
404 is_object = true;
405 } else {
406 break;
407 }
Paul Sokolovsky0eba1622015-09-30 00:54:20 -0700408 arg_type++;
409 }
Paul Sokolovsky9ebd4da2015-10-22 01:34:03 +0300410 if (!is_object) {
411 return false;
412 }
Paul Sokolovsky1e9d8e12015-09-19 01:04:41 +0300413 mp_obj_jobject_t *jo = arg;
Paul Sokolovsky91f21682015-10-23 00:33:03 +0300414 if (!MATCH(expected_type, "java.lang.Object")) {
415 char class_name[64];
416 get_jclass_name(jo->obj, class_name);
417 //printf("Arg class: %s\n", class_name);
418 if (strcmp(class_name, expected_type) != 0) {
419 return false;
420 }
421 }
Paul Sokolovsky1e9d8e12015-09-19 01:04:41 +0300422 out->l = jo->obj;
Paul Sokolovsky7381b7a2015-10-10 01:19:28 +0300423 } else if (type == &mp_type_bool) {
424 if (IMATCH(arg_type, "boolean")) {
425 out->z = arg == mp_const_true;
426 } else {
427 return false;
428 }
429 } else if (arg == mp_const_none) {
430 //printf("TODO: Check java arg type!!\n");
431 while (isalpha(*arg_type) || *arg_type == '.') {
432 arg_type++;
433 }
434 out->l = NULL;
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300435 } else {
Damien George48d867b2017-06-15 11:54:41 +1000436 mp_raise_TypeError("arg type not supported");
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300437 }
438
439 *jtypesig = arg_type;
440 return true;
441}
442
Paul Sokolovsky02041bf2015-10-09 00:25:26 +0300443#if 0
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300444// jvalue is known to be union of jobject and friends. And yet from C's
445// perspective, it's aggregate object which may require passing via stack
446// instead of registers. Work that around by passing jobject and typecasting
447// it.
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300448STATIC mp_obj_t jvalue2py(const char *jtypesig, jobject arg) {
Paul Sokolovsky51673322015-09-14 00:15:08 +0300449 if (arg == NULL || MATCH(jtypesig, "void")) {
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300450 return mp_const_none;
Paul Sokolovsky6196aa42015-09-20 00:36:26 +0300451 } else if (MATCH(jtypesig, "boolean")) {
452 return mp_obj_new_bool((bool)arg);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300453 } else if (MATCH(jtypesig, "int")) {
454 return mp_obj_new_int((mp_int_t)arg);
Paul Sokolovskye632b1f2015-09-23 07:10:55 -0700455 } else if (is_object_type(jtypesig)) {
456 // Non-primitive, object type
457 return new_jobject(arg);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300458 }
459
Paul Sokolovskye632b1f2015-09-23 07:10:55 -0700460 printf("Unknown return type: %s\n", jtypesig);
Paul Sokolovskyb230a862015-09-15 14:07:12 +0300461
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300462 return MP_OBJ_NULL;
463}
Paul Sokolovsky02041bf2015-10-09 00:25:26 +0300464#endif
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300465
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300466STATIC 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) {
467 jvalue jargs[n_args];
468// printf("methods=%p\n", methods);
469 jsize num_methods = JJ(GetArrayLength, methods);
470 for (int i = 0; i < num_methods; i++) {
471 jobject meth = JJ(GetObjectArrayElement, methods, i);
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700472 jobject name_o = JJ(CallObjectMethod, meth, Object_toString_mid);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300473 const char *decl = JJ(GetStringUTFChars, name_o, NULL);
474 const char *arg_types = strchr(decl, '(') + 1;
475 //const char *arg_types_end = strchr(arg_types, ')');
476// printf("method[%d]=%p %s\n", i, meth, decl);
477
478 const char *meth_name = NULL;
479 const char *ret_type = NULL;
480 if (!is_constr) {
481 meth_name = strprev(arg_types, '.') + 1;
482 ret_type = strprev(meth_name, ' ') - 1;
483 ret_type = strprev(ret_type, ' ') + 1;
484
485 int name_len = strlen(name);
486 if (strncmp(name, meth_name, name_len/*arg_types - meth_name - 1*/) || meth_name[name_len] != '('/*(*/) {
Paul Sokolovsky81d64ab2015-09-26 08:50:37 -0700487 goto next_method;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300488 }
489 }
490// printf("method[%d]=%p %s\n", i, meth, decl);
491// printf("!!!%s\n", arg_types);
492// printf("name=%p meth_name=%s\n", name, meth_name);
493
494 bool found = true;
Paul Sokolovsky0eba1622015-09-30 00:54:20 -0700495 for (int i = 0; i < n_args && *arg_types != ')'; i++) {
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300496 if (!py2jvalue(&arg_types, args[i], &jargs[i])) {
497 goto next_method;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300498 }
499
500 if (*arg_types == ',') {
501 arg_types++;
502 }
503 }
504
Paul Sokolovsky0eba1622015-09-30 00:54:20 -0700505 if (*arg_types != ')') {
506 goto next_method;
507 }
508
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300509 if (found) {
510// printf("found!\n");
511 jmethodID method_id = JJ(FromReflectedMethod, meth);
512 jobject res;
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700513 mp_obj_t ret;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300514 if (is_constr) {
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300515 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700516 res = JJ(NewObjectA, obj, method_id, jargs);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300517 return new_jobject(res);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300518 } else {
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700519 if (MATCH(ret_type, "void")) {
520 JJ(CallVoidMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300521 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700522 ret = mp_const_none;
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700523 } else if (MATCH(ret_type, "int")) {
524 jint res = JJ(CallIntMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300525 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700526 ret = mp_obj_new_int(res);
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700527 } else if (MATCH(ret_type, "boolean")) {
528 jboolean res = JJ(CallBooleanMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300529 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700530 ret = mp_obj_new_bool(res);
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700531 } else if (is_object_type(ret_type)) {
532 res = JJ(CallObjectMethodA, obj, method_id, jargs);
Paul Sokolovsky41eb7052015-10-14 00:24:36 +0300533 check_exception();
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700534 ret = new_jobject(res);
535 } else {
Paul Sokolovskyc0a79cc2015-09-25 17:11:24 -0700536 JJ(ReleaseStringUTFChars, name_o, decl);
Damien George48d867b2017-06-15 11:54:41 +1000537 mp_raise_TypeError("cannot handle return type");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300538 }
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700539
540 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky77020282015-09-28 08:36:43 -0700541 JJ(DeleteLocalRef, name_o);
542 JJ(DeleteLocalRef, meth);
Paul Sokolovsky0d28a3e2015-09-27 02:30:28 -0700543 return ret;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300544 }
545 }
546
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300547next_method:
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300548 JJ(ReleaseStringUTFChars, name_o, decl);
Paul Sokolovsky77020282015-09-28 08:36:43 -0700549 JJ(DeleteLocalRef, name_o);
550 JJ(DeleteLocalRef, meth);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300551 }
552
Damien George48d867b2017-06-15 11:54:41 +1000553 mp_raise_TypeError("method not found");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300554}
555
556
Paul Sokolovskyeac22e22017-01-04 16:10:42 +0300557STATIC mp_obj_t jmethod_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300558 if (n_kw != 0) {
Damien George48d867b2017-06-15 11:54:41 +1000559 mp_raise_TypeError("kwargs not supported");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300560 }
561 mp_obj_jmethod_t *self = self_in;
562
563 const char *name = qstr_str(self->name);
564// jstring meth_name = JJ(NewStringUTF, name);
565
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300566 jclass obj_class = self->obj;
567 if (!self->is_static) {
568 obj_class = JJ(GetObjectClass, self->obj);
569 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300570 jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid);
571
572 return call_method(self->obj, name, methods, false, n_args, args);
573}
574
575STATIC const mp_obj_type_t jmethod_type = {
576 { &mp_type_type },
577 .name = MP_QSTR_jmethod,
578 .print = jmethod_print,
579 .call = jmethod_call,
580// .attr = jobject_attr,
Paul Sokolovskye2801222017-07-30 10:03:14 +0300581// .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict,
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300582};
583
584#ifdef __ANDROID__
585#define LIBJVM_SO "libdvm.so"
586#else
587#define LIBJVM_SO "libjvm.so"
588#endif
589
590STATIC void create_jvm() {
591 JavaVMInitArgs args;
592 JavaVMOption options;
593 options.optionString = "-Djava.class.path=.";
594 args.version = JNI_VERSION_1_6;
595 args.nOptions = 1;
596 args.options = &options;
597 args.ignoreUnrecognized = 0;
598
599 if (env) {
600 return;
601 }
602
603 void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL);
604 if (!libjvm) {
Damien George48d867b2017-06-15 11:54:41 +1000605 mp_raise_msg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300606 }
607 int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM");
608
609 int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args);
610 if (st < 0 || !env) {
Damien George48d867b2017-06-15 11:54:41 +1000611 mp_raise_msg(&mp_type_OSError, "unable to create JVM");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300612 }
613
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300614 Class_class = JJ(FindClass, "java/lang/Class");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300615 jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
616 String_class = JJ(FindClass, "java/lang/String");
617
Paul Sokolovskyf22be4e2015-10-03 08:43:41 -0700618 jclass Object_class = JJ(FindClass, "java/lang/Object");
619 Object_toString_mid = JJ(GetMethodID, Object_class, "toString",
620 "()Ljava/lang/String;");
621
Paul Sokolovsky3c7e1b82015-11-11 16:43:27 +0200622 Class_getName_mid = (*env)->GetMethodID(env, Class_class, "getName",
623 "()Ljava/lang/String;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300624 Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300625 "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300626 Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300627 "()[Ljava/lang/reflect/Method;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300628 Class_getConstructors_mid = (*env)->GetMethodID(env, Class_class, "getConstructors",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300629 "()[Ljava/lang/reflect/Constructor;");
630 Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
631 "()Ljava/lang/String;");
Paul Sokolovskyb2d880d2015-09-21 12:02:22 -0700632
633 List_class = JJ(FindClass, "java/util/List");
634 List_get_mid = JJ(GetMethodID, List_class, "get",
635 "(I)Ljava/lang/Object;");
636 List_set_mid = JJ(GetMethodID, List_class, "set",
637 "(ILjava/lang/Object;)Ljava/lang/Object;");
Paul Sokolovskyf3ca8622015-09-29 10:05:30 -0700638 List_size_mid = JJ(GetMethodID, List_class, "size",
639 "()I");
Paul Sokolovsky9e0a3d42015-10-02 00:21:07 -0700640 IndexException_class = JJ(FindClass, "java/lang/IndexOutOfBoundsException");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300641}
642
643STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) {
644 const char *cls_name = mp_obj_str_get_str(cls_name_in);
645 if (!env) {
646 create_jvm();
647 }
648 jclass cls = JJ(FindClass, cls_name);
649
650 mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
651 o->base.type = &jclass_type;
652 o->cls = cls;
653 return o;
654}
655MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
656
Paul Sokolovskyee324c52016-09-17 16:14:02 +0300657STATIC mp_obj_t mod_jni_array(mp_obj_t type_in, mp_obj_t size_in) {
Paul Sokolovskyee324c52016-09-17 16:14:02 +0300658 if (!env) {
659 create_jvm();
660 }
Paul Sokolovsky5bf1b4e2016-09-18 13:37:40 +0300661 mp_int_t size = mp_obj_get_int(size_in);
Paul Sokolovskyee324c52016-09-17 16:14:02 +0300662 jobject res = NULL;
Paul Sokolovsky5bf1b4e2016-09-18 13:37:40 +0300663
664 if (MP_OBJ_IS_TYPE(type_in, &jclass_type)) {
665
666 mp_obj_jclass_t *jcls = type_in;
667 res = JJ(NewObjectArray, size, jcls->cls, NULL);
668
669 } else if (MP_OBJ_IS_STR(type_in)) {
670 const char *type = mp_obj_str_get_str(type_in);
671 switch (*type) {
672 case 'Z':
673 res = JJ(NewBooleanArray, size);
674 break;
675 case 'B':
676 res = JJ(NewByteArray, size);
677 break;
678 case 'C':
679 res = JJ(NewCharArray, size);
680 break;
681 case 'S':
682 res = JJ(NewShortArray, size);
683 break;
684 case 'I':
685 res = JJ(NewIntArray, size);
686 break;
687 case 'J':
688 res = JJ(NewLongArray, size);
689 break;
690 case 'F':
691 res = JJ(NewFloatArray, size);
692 break;
693 case 'D':
694 res = JJ(NewDoubleArray, size);
695 break;
696 }
697
Paul Sokolovskyee324c52016-09-17 16:14:02 +0300698 }
699
700 return new_jobject(res);
701}
702MP_DEFINE_CONST_FUN_OBJ_2(mod_jni_array_obj, mod_jni_array);
703
704
Paul Sokolovskycb6cf5e2015-09-16 01:09:39 +0300705STATIC mp_obj_t mod_jni_env() {
706 return mp_obj_new_int((mp_int_t)env);
707}
708MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
709
Paul Sokolovskye2801222017-07-30 10:03:14 +0300710STATIC const mp_rom_map_elem_t mp_module_jni_globals_table[] = {
711 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_jni) },
712 { MP_ROM_QSTR(MP_QSTR_cls), MP_ROM_PTR(&mod_jni_cls_obj) },
713 { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mod_jni_array_obj) },
714 { MP_ROM_QSTR(MP_QSTR_env), MP_ROM_PTR(&mod_jni_env_obj) },
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300715};
716
717STATIC MP_DEFINE_CONST_DICT(mp_module_jni_globals, mp_module_jni_globals_table);
718
719const mp_obj_module_t mp_module_jni = {
720 .base = { &mp_type_module },
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300721 .globals = (mp_obj_dict_t*)&mp_module_jni_globals,
722};