blob: 001fa097ab23ab183d9a5a22e00e2732f9a8ae39 [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>
31
32#include "py/nlr.h"
33#include "py/runtime.h"
34#include "py/binary.h"
35
36#include <jni.h>
37
38#define JJ(call, ...) (*env)->call(env, __VA_ARGS__)
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030039#define JJ1(call) (*env)->call(env)
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030040
41static JavaVM *jvm;
42static JNIEnv *env;
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +030043static jclass Class_class;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030044static jclass String_class;
45static jmethodID Class_getField_mid;
46static jmethodID Class_getMethods_mid;
47static jmethodID Class_getConstructors_mid;
48static jmethodID Method_getName_mid;
49static jmethodID Method_toString_mid;
50
51STATIC const mp_obj_type_t jobject_type;
52STATIC const mp_obj_type_t jmethod_type;
53
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030054STATIC mp_obj_t new_jobject(jobject jo);
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +030055STATIC mp_obj_t new_jclass(jclass jc);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030056STATIC 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);
57
58typedef struct _mp_obj_jclass_t {
59 mp_obj_base_t base;
60 jclass cls;
61} mp_obj_jclass_t;
62
63typedef struct _mp_obj_jobject_t {
64 mp_obj_base_t base;
65 jobject obj;
66} mp_obj_jobject_t;
67
68typedef struct _mp_obj_jmethod_t {
69 mp_obj_base_t base;
70 jobject obj;
71 jmethodID meth;
72 qstr name;
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030073 bool is_static;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +030074} mp_obj_jmethod_t;
75
76// jclass
77
78STATIC void jclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
79 (void)kind;
80 mp_obj_jclass_t *self = self_in;
81 // Variable value printed as cast to int
82 mp_printf(print, "<jclass @%p>", self->cls);
83}
84
85STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
86 if (dest[0] == MP_OBJ_NULL) {
87 // load attribute
88 mp_obj_jclass_t *self = self_in;
89 const char *attr = qstr_str(attr_in);
90
91 jstring field_name = JJ(NewStringUTF, attr);
92 jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name);
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +030093 if (!JJ1(ExceptionCheck)) {
94 jfieldID field_id = JJ(FromReflectedField, field);
95 jobject obj = JJ(GetStaticObjectField, self->cls, field_id);
96 dest[0] = new_jobject(obj);
97 return;
98 }
99 //JJ1(ExceptionDescribe);
100 JJ1(ExceptionClear);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300101
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300102 mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
103 o->base.type = &jmethod_type;
104 o->name = attr_in;
105 o->meth = NULL;
106 o->obj = self->cls;
107 o->is_static = true;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300108 dest[0] = o;
109 }
110}
111
112STATIC 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) {
113 if (n_kw != 0) {
114 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported"));
115 }
116 mp_obj_jclass_t *self = self_in;
117
118 jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid);
119
120 return call_method(self->cls, NULL, methods, true, n_args, args);
121}
122
123STATIC const mp_map_elem_t jclass_locals_dict_table[] = {
124// { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&ffivar_get_obj },
125// { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&ffivar_set_obj },
126};
127
128STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table);
129
130STATIC const mp_obj_type_t jclass_type = {
131 { &mp_type_type },
132 .name = MP_QSTR_jclass,
133 .print = jclass_print,
134 .attr = jclass_attr,
135 .call = jclass_call,
136 .locals_dict = (mp_obj_t)&jclass_locals_dict,
137};
138
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300139STATIC mp_obj_t new_jclass(jclass jc) {
140 mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
141 o->base.type = &jclass_type;
142 o->cls = jc;
143 return o;
144}
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300145
146// jobject
147
148STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
149 (void)kind;
150 mp_obj_jobject_t *self = self_in;
151 // Variable value printed as cast to int
152 mp_printf(print, "<jobject @%p>", self->obj);
153}
154
155STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
156 if (dest[0] == MP_OBJ_NULL) {
157 // load attribute
158 mp_obj_jobject_t *self = self_in;
159
160 mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
161 o->base.type = &jmethod_type;
162 o->name = attr_in;
163 o->meth = NULL;
164 o->obj = self->obj;
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300165 o->is_static = false;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300166 dest[0] = o;
167 }
168}
169
170STATIC const mp_obj_type_t jobject_type = {
171 { &mp_type_type },
172 .name = MP_QSTR_jobject,
173 .print = jobject_print,
174 .attr = jobject_attr,
175// .locals_dict = (mp_obj_t)&jobject_locals_dict,
176};
177
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300178STATIC mp_obj_t new_jobject(jobject jo) {
179 mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t);
180 o->base.type = &jobject_type;
181 o->obj = jo;
182 return o;
183}
184
185
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300186// jmethod
187
188STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
189 (void)kind;
190 mp_obj_jmethod_t *self = self_in;
191 // Variable value printed as cast to int
192 mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
193}
194
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300195#define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300196
197#define CHECK_TYPE(java_type_name) \
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300198 if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
199 return false; \
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300200 } \
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300201 arg_type += sizeof(java_type_name) - 1;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300202
203STATIC const char *strprev(const char *s, char c) {
204 while (*s != c) {
205 s--;
206 }
207 return s;
208}
209
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300210STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
211 const char *arg_type = *jtypesig;
212 mp_obj_type_t *type = mp_obj_get_type(arg);
213
214 if (type == &mp_type_str) {
215 if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) {
216 out->l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
217 } else {
218 return false;
219 }
220 } else if (type == &mp_type_int) {
221 CHECK_TYPE("long");
222 out->j = mp_obj_get_int(arg);
223 } else {
224 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported"));
225 }
226
227 *jtypesig = arg_type;
228 return true;
229}
230
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300231// jvalue is known to be union of jobject and friends. And yet from C's
232// perspective, it's aggregate object which may require passing via stack
233// instead of registers. Work that around by passing jobject and typecasting
234// it.
235#define MATCH(s, static) (!strncmp(s, static, sizeof(static) - 1))
236STATIC mp_obj_t jvalue2py(const char *jtypesig, jobject arg) {
237 mp_obj_t ret;
Paul Sokolovsky51673322015-09-14 00:15:08 +0300238 if (arg == NULL || MATCH(jtypesig, "void")) {
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300239 return mp_const_none;
240 } else if (MATCH(jtypesig, "int")) {
241 return mp_obj_new_int((mp_int_t)arg);
242 } else if (MATCH(jtypesig, "java.lang.String")) {
243ret_string:;
244 const char *s = JJ(GetStringUTFChars, arg, NULL);
245 ret = mp_obj_new_str(s, strlen(s), false);
246 JJ(ReleaseStringUTFChars, arg, s);
247 return ret;
Paul Sokolovskyb230a862015-09-15 14:07:12 +0300248 } else {
249 while (*jtypesig != ' ' && *jtypesig) {
250 if (*jtypesig == '.') {
251 // Non-primitive, object type
252 if (JJ(IsInstanceOf, arg, String_class)) {
253 goto ret_string;
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300254 } else if (JJ(IsInstanceOf, arg, Class_class)) {
255 return new_jclass(arg);
Paul Sokolovskyb230a862015-09-15 14:07:12 +0300256 } else {
257 return new_jobject(arg);
258 }
259 }
260 jtypesig++;
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300261 }
262 }
263
Paul Sokolovskyb230a862015-09-15 14:07:12 +0300264 printf("Unknown return type: %s\n", jtypesig);
265
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300266 return MP_OBJ_NULL;
267}
268
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300269STATIC 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) {
270 jvalue jargs[n_args];
271// printf("methods=%p\n", methods);
272 jsize num_methods = JJ(GetArrayLength, methods);
273 for (int i = 0; i < num_methods; i++) {
274 jobject meth = JJ(GetObjectArrayElement, methods, i);
275 jobject name_o = JJ(CallObjectMethod, meth, Method_toString_mid);
276 const char *decl = JJ(GetStringUTFChars, name_o, NULL);
277 const char *arg_types = strchr(decl, '(') + 1;
278 //const char *arg_types_end = strchr(arg_types, ')');
279// printf("method[%d]=%p %s\n", i, meth, decl);
280
281 const char *meth_name = NULL;
282 const char *ret_type = NULL;
283 if (!is_constr) {
284 meth_name = strprev(arg_types, '.') + 1;
285 ret_type = strprev(meth_name, ' ') - 1;
286 ret_type = strprev(ret_type, ' ') + 1;
287
288 int name_len = strlen(name);
289 if (strncmp(name, meth_name, name_len/*arg_types - meth_name - 1*/) || meth_name[name_len] != '('/*(*/) {
290 continue;
291 }
292 }
293// printf("method[%d]=%p %s\n", i, meth, decl);
294// printf("!!!%s\n", arg_types);
295// printf("name=%p meth_name=%s\n", name, meth_name);
296
297 bool found = true;
298 for (int i = 0; i < n_args; i++) {
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300299 if (!py2jvalue(&arg_types, args[i], &jargs[i])) {
300 goto next_method;
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300301 }
302
303 if (*arg_types == ',') {
304 arg_types++;
305 }
306 }
307
308 if (found) {
309// printf("found!\n");
310 jmethodID method_id = JJ(FromReflectedMethod, meth);
311 jobject res;
312 if (is_constr) {
313 res = JJ(NewObjectA, obj, method_id, jargs);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300314 JJ(ReleaseStringUTFChars, name_o, decl);
315 return new_jobject(res);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300316 } else {
317 res = JJ(CallObjectMethodA, obj, method_id, jargs);
Paul Sokolovsky26a9b4d2015-09-13 01:25:04 +0300318 mp_obj_t ret = jvalue2py(ret_type, res);
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300319 JJ(ReleaseStringUTFChars, name_o, decl);
320 if (ret != MP_OBJ_NULL) {
321 return ret;
322 }
323 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "cannot handle return type"));
324 }
325 }
326
Paul Sokolovsky4e7bde82015-09-12 00:19:18 +0300327next_method:
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300328 JJ(ReleaseStringUTFChars, name_o, decl);
329 }
330
331 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "method not found"));
332}
333
334
335STATIC 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) {
336 if (n_kw != 0) {
337 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported"));
338 }
339 mp_obj_jmethod_t *self = self_in;
340
341 const char *name = qstr_str(self->name);
342// jstring meth_name = JJ(NewStringUTF, name);
343
Paul Sokolovsky7a4b10c2015-09-14 00:12:27 +0300344 jclass obj_class = self->obj;
345 if (!self->is_static) {
346 obj_class = JJ(GetObjectClass, self->obj);
347 }
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300348 jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid);
349
350 return call_method(self->obj, name, methods, false, n_args, args);
351}
352
353STATIC const mp_obj_type_t jmethod_type = {
354 { &mp_type_type },
355 .name = MP_QSTR_jmethod,
356 .print = jmethod_print,
357 .call = jmethod_call,
358// .attr = jobject_attr,
359// .locals_dict = (mp_obj_t)&jobject_locals_dict,
360};
361
362#ifdef __ANDROID__
363#define LIBJVM_SO "libdvm.so"
364#else
365#define LIBJVM_SO "libjvm.so"
366#endif
367
368STATIC void create_jvm() {
369 JavaVMInitArgs args;
370 JavaVMOption options;
371 options.optionString = "-Djava.class.path=.";
372 args.version = JNI_VERSION_1_6;
373 args.nOptions = 1;
374 args.options = &options;
375 args.ignoreUnrecognized = 0;
376
377 if (env) {
378 return;
379 }
380
381 void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL);
382 if (!libjvm) {
383 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH"));
384 }
385 int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM");
386
387 int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args);
388 if (st < 0 || !env) {
389 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to create JVM"));
390 }
391
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300392 Class_class = JJ(FindClass, "java/lang/Class");
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300393 jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
394 String_class = JJ(FindClass, "java/lang/String");
395
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300396 Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300397 "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300398 Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300399 "()[Ljava/lang/reflect/Method;");
Paul Sokolovsky1cb5de22015-09-17 13:31:16 +0300400 Class_getConstructors_mid = (*env)->GetMethodID(env, Class_class, "getConstructors",
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300401 "()[Ljava/lang/reflect/Constructor;");
402 Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
403 "()Ljava/lang/String;");
404 Method_toString_mid = (*env)->GetMethodID(env, method_class, "toString",
405 "()Ljava/lang/String;");
406}
407
408STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) {
409 const char *cls_name = mp_obj_str_get_str(cls_name_in);
410 if (!env) {
411 create_jvm();
412 }
413 jclass cls = JJ(FindClass, cls_name);
414
415 mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
416 o->base.type = &jclass_type;
417 o->cls = cls;
418 return o;
419}
420MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
421
Paul Sokolovskycb6cf5e2015-09-16 01:09:39 +0300422STATIC mp_obj_t mod_jni_env() {
423 return mp_obj_new_int((mp_int_t)env);
424}
425MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
426
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300427STATIC const mp_map_elem_t mp_module_jni_globals_table[] = {
428 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_jni) },
429 { MP_OBJ_NEW_QSTR(MP_QSTR_cls), (mp_obj_t)&mod_jni_cls_obj },
Paul Sokolovskycb6cf5e2015-09-16 01:09:39 +0300430 { MP_OBJ_NEW_QSTR(MP_QSTR_env), (mp_obj_t)&mod_jni_env_obj },
Paul Sokolovskye79c6b62015-09-11 17:57:47 +0300431};
432
433STATIC MP_DEFINE_CONST_DICT(mp_module_jni_globals, mp_module_jni_globals_table);
434
435const mp_obj_module_t mp_module_jni = {
436 .base = { &mp_type_module },
437 .name = MP_QSTR_jni,
438 .globals = (mp_obj_dict_t*)&mp_module_jni_globals,
439};