|
| 1 | +/* |
| 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__) |
| 39 | + |
| 40 | +static JavaVM *jvm; |
| 41 | +static JNIEnv *env; |
| 42 | +static jclass String_class; |
| 43 | +static jmethodID Class_getField_mid; |
| 44 | +static jmethodID Class_getMethods_mid; |
| 45 | +static jmethodID Class_getConstructors_mid; |
| 46 | +static jmethodID Method_getName_mid; |
| 47 | +static jmethodID Method_toString_mid; |
| 48 | + |
| 49 | +STATIC const mp_obj_type_t jobject_type; |
| 50 | +STATIC const mp_obj_type_t jmethod_type; |
| 51 | + |
| 52 | +STATIC 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); |
| 53 | + |
| 54 | +typedef struct _mp_obj_jclass_t { |
| 55 | + mp_obj_base_t base; |
| 56 | + jclass cls; |
| 57 | +} mp_obj_jclass_t; |
| 58 | + |
| 59 | +typedef struct _mp_obj_jobject_t { |
| 60 | + mp_obj_base_t base; |
| 61 | + jobject obj; |
| 62 | +} mp_obj_jobject_t; |
| 63 | + |
| 64 | +typedef struct _mp_obj_jmethod_t { |
| 65 | + mp_obj_base_t base; |
| 66 | + jobject obj; |
| 67 | + jmethodID meth; |
| 68 | + qstr name; |
| 69 | +} mp_obj_jmethod_t; |
| 70 | + |
| 71 | +// jclass |
| 72 | + |
| 73 | +STATIC void jclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 74 | + (void)kind; |
| 75 | + mp_obj_jclass_t *self = self_in; |
| 76 | + // Variable value printed as cast to int |
| 77 | + mp_printf(print, "<jclass @%p>", self->cls); |
| 78 | +} |
| 79 | + |
| 80 | +STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { |
| 81 | + if (dest[0] == MP_OBJ_NULL) { |
| 82 | + // load attribute |
| 83 | + mp_obj_jclass_t *self = self_in; |
| 84 | + const char *attr = qstr_str(attr_in); |
| 85 | + |
| 86 | + jstring field_name = JJ(NewStringUTF, attr); |
| 87 | + jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name); |
| 88 | + jfieldID field_id = JJ(FromReflectedField, field); |
| 89 | + jobject obj = JJ(GetStaticObjectField, self->cls, field_id); |
| 90 | + |
| 91 | + mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t); |
| 92 | + o->base.type = &jobject_type; |
| 93 | + o->obj = obj; |
| 94 | + dest[0] = o; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +STATIC 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) { |
| 99 | + if (n_kw != 0) { |
| 100 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported")); |
| 101 | + } |
| 102 | + mp_obj_jclass_t *self = self_in; |
| 103 | + |
| 104 | + jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid); |
| 105 | + |
| 106 | + return call_method(self->cls, NULL, methods, true, n_args, args); |
| 107 | +} |
| 108 | + |
| 109 | +STATIC const mp_map_elem_t jclass_locals_dict_table[] = { |
| 110 | +// { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&ffivar_get_obj }, |
| 111 | +// { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&ffivar_set_obj }, |
| 112 | +}; |
| 113 | + |
| 114 | +STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table); |
| 115 | + |
| 116 | +STATIC const mp_obj_type_t jclass_type = { |
| 117 | + { &mp_type_type }, |
| 118 | + .name = MP_QSTR_jclass, |
| 119 | + .print = jclass_print, |
| 120 | + .attr = jclass_attr, |
| 121 | + .call = jclass_call, |
| 122 | + .locals_dict = (mp_obj_t)&jclass_locals_dict, |
| 123 | +}; |
| 124 | + |
| 125 | + |
| 126 | +// jobject |
| 127 | + |
| 128 | +STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 129 | + (void)kind; |
| 130 | + mp_obj_jobject_t *self = self_in; |
| 131 | + // Variable value printed as cast to int |
| 132 | + mp_printf(print, "<jobject @%p>", self->obj); |
| 133 | +} |
| 134 | + |
| 135 | +STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { |
| 136 | + if (dest[0] == MP_OBJ_NULL) { |
| 137 | + // load attribute |
| 138 | + mp_obj_jobject_t *self = self_in; |
| 139 | + |
| 140 | + mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t); |
| 141 | + o->base.type = &jmethod_type; |
| 142 | + o->name = attr_in; |
| 143 | + o->meth = NULL; |
| 144 | + o->obj = self->obj; |
| 145 | + dest[0] = o; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +STATIC const mp_obj_type_t jobject_type = { |
| 150 | + { &mp_type_type }, |
| 151 | + .name = MP_QSTR_jobject, |
| 152 | + .print = jobject_print, |
| 153 | + .attr = jobject_attr, |
| 154 | +// .locals_dict = (mp_obj_t)&jobject_locals_dict, |
| 155 | +}; |
| 156 | + |
| 157 | +// jmethod |
| 158 | + |
| 159 | +STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 160 | + (void)kind; |
| 161 | + mp_obj_jmethod_t *self = self_in; |
| 162 | + // Variable value printed as cast to int |
| 163 | + mp_printf(print, "<jmethod '%s'>", qstr_str(self->name)); |
| 164 | +} |
| 165 | + |
| 166 | +#define MATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1)) |
| 167 | + |
| 168 | +#define CHECK_TYPE(java_type_name) \ |
| 169 | + if (strncmp(arg_types, java_type_name, sizeof(java_type_name) - 1) != 0) { \ |
| 170 | + found = false; \ |
| 171 | + break; \ |
| 172 | + } \ |
| 173 | + arg_types += sizeof(java_type_name) - 1; |
| 174 | + |
| 175 | +STATIC const char *strprev(const char *s, char c) { |
| 176 | + while (*s != c) { |
| 177 | + s--; |
| 178 | + } |
| 179 | + return s; |
| 180 | +} |
| 181 | + |
| 182 | +STATIC 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) { |
| 183 | + jvalue jargs[n_args]; |
| 184 | +// printf("methods=%p\n", methods); |
| 185 | + jsize num_methods = JJ(GetArrayLength, methods); |
| 186 | + for (int i = 0; i < num_methods; i++) { |
| 187 | + jobject meth = JJ(GetObjectArrayElement, methods, i); |
| 188 | + jobject name_o = JJ(CallObjectMethod, meth, Method_toString_mid); |
| 189 | + const char *decl = JJ(GetStringUTFChars, name_o, NULL); |
| 190 | + const char *arg_types = strchr(decl, '(') + 1; |
| 191 | + //const char *arg_types_end = strchr(arg_types, ')'); |
| 192 | +// printf("method[%d]=%p %s\n", i, meth, decl); |
| 193 | + |
| 194 | + const char *meth_name = NULL; |
| 195 | + const char *ret_type = NULL; |
| 196 | + if (!is_constr) { |
| 197 | + meth_name = strprev(arg_types, '.') + 1; |
| 198 | + ret_type = strprev(meth_name, ' ') - 1; |
| 199 | + ret_type = strprev(ret_type, ' ') + 1; |
| 200 | + |
| 201 | + int name_len = strlen(name); |
| 202 | + if (strncmp(name, meth_name, name_len/*arg_types - meth_name - 1*/) || meth_name[name_len] != '('/*(*/) { |
| 203 | + continue; |
| 204 | + } |
| 205 | + } |
| 206 | +// printf("method[%d]=%p %s\n", i, meth, decl); |
| 207 | +// printf("!!!%s\n", arg_types); |
| 208 | +// printf("name=%p meth_name=%s\n", name, meth_name); |
| 209 | + |
| 210 | + bool found = true; |
| 211 | + for (int i = 0; i < n_args; i++) { |
| 212 | + mp_obj_t arg = args[i]; |
| 213 | + mp_obj_type_t *type = mp_obj_get_type(arg); |
| 214 | + if (type == &mp_type_str) { |
| 215 | +// CHECK_TYPE("java.lang.String"); |
| 216 | + if (MATCH(arg_types, "java.lang.String") || MATCH(arg_types, "java.lang.Object")) { |
| 217 | + jargs[i].l = JJ(NewStringUTF, mp_obj_str_get_str(arg)); |
| 218 | + } else { |
| 219 | + found = false; |
| 220 | + } |
| 221 | + } else if (type == &mp_type_int) { |
| 222 | + CHECK_TYPE("long"); |
| 223 | + jargs[i].j = mp_obj_get_int(arg); |
| 224 | + } else { |
| 225 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported")); |
| 226 | + } |
| 227 | + |
| 228 | + if (*arg_types == ',') { |
| 229 | + arg_types++; |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + if (found) { |
| 234 | +// printf("found!\n"); |
| 235 | + jmethodID method_id = JJ(FromReflectedMethod, meth); |
| 236 | + jobject res; |
| 237 | + if (is_constr) { |
| 238 | + res = JJ(NewObjectA, obj, method_id, jargs); |
| 239 | + mp_obj_jobject_t *o; |
| 240 | +ret_object: |
| 241 | + o = m_new_obj(mp_obj_jobject_t); |
| 242 | + o->base.type = &jobject_type; |
| 243 | + o->obj = res; |
| 244 | + return o; |
| 245 | + } else { |
| 246 | + res = JJ(CallObjectMethodA, obj, method_id, jargs); |
| 247 | + mp_obj_t ret = MP_OBJ_NULL; |
| 248 | + if (strncmp(ret_type, "void", 4) == 0) { |
| 249 | + ret = mp_const_none; |
| 250 | + } else if (strncmp(ret_type, "int", sizeof("int") - 1) == 0) { |
| 251 | + ret = mp_obj_new_int((mp_int_t)res); |
| 252 | + } else if (strncmp(ret_type, "java.lang.String", sizeof("java.lang.String") - 1) == 0) { |
| 253 | +ret_string:; |
| 254 | + const char *s = JJ(GetStringUTFChars, res, NULL); |
| 255 | + ret = mp_obj_new_str(s, strlen(s), false); |
| 256 | + JJ(ReleaseStringUTFChars, res, s); |
| 257 | + } else if (strncmp(ret_type, "java.lang.Object", sizeof("java.lang.Object") - 1) == 0) { |
| 258 | + if (JJ(IsInstanceOf, res, String_class)) { |
| 259 | + goto ret_string; |
| 260 | + } else { |
| 261 | + goto ret_object; |
| 262 | + } |
| 263 | + } |
| 264 | + JJ(ReleaseStringUTFChars, name_o, decl); |
| 265 | + if (ret != MP_OBJ_NULL) { |
| 266 | + return ret; |
| 267 | + } |
| 268 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "cannot handle return type")); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + JJ(ReleaseStringUTFChars, name_o, decl); |
| 273 | + } |
| 274 | + |
| 275 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "method not found")); |
| 276 | +} |
| 277 | + |
| 278 | + |
| 279 | +STATIC 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) { |
| 280 | + if (n_kw != 0) { |
| 281 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported")); |
| 282 | + } |
| 283 | + mp_obj_jmethod_t *self = self_in; |
| 284 | + |
| 285 | + const char *name = qstr_str(self->name); |
| 286 | +// jstring meth_name = JJ(NewStringUTF, name); |
| 287 | + |
| 288 | + jclass obj_class = JJ(GetObjectClass, self->obj); |
| 289 | + jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid); |
| 290 | + |
| 291 | + return call_method(self->obj, name, methods, false, n_args, args); |
| 292 | +} |
| 293 | + |
| 294 | +STATIC const mp_obj_type_t jmethod_type = { |
| 295 | + { &mp_type_type }, |
| 296 | + .name = MP_QSTR_jmethod, |
| 297 | + .print = jmethod_print, |
| 298 | + .call = jmethod_call, |
| 299 | +// .attr = jobject_attr, |
| 300 | +// .locals_dict = (mp_obj_t)&jobject_locals_dict, |
| 301 | +}; |
| 302 | + |
| 303 | +#ifdef __ANDROID__ |
| 304 | +#define LIBJVM_SO "libdvm.so" |
| 305 | +#else |
| 306 | +#define LIBJVM_SO "libjvm.so" |
| 307 | +#endif |
| 308 | + |
| 309 | +STATIC void create_jvm() { |
| 310 | + JavaVMInitArgs args; |
| 311 | + JavaVMOption options; |
| 312 | + options.optionString = "-Djava.class.path=."; |
| 313 | + args.version = JNI_VERSION_1_6; |
| 314 | + args.nOptions = 1; |
| 315 | + args.options = &options; |
| 316 | + args.ignoreUnrecognized = 0; |
| 317 | + |
| 318 | + if (env) { |
| 319 | + return; |
| 320 | + } |
| 321 | + |
| 322 | + void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL); |
| 323 | + if (!libjvm) { |
| 324 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH")); |
| 325 | + } |
| 326 | + int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM"); |
| 327 | + |
| 328 | + int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args); |
| 329 | + if (st < 0 || !env) { |
| 330 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to create JVM")); |
| 331 | + } |
| 332 | + |
| 333 | + jclass class_class = JJ(FindClass, "java/lang/Class"); |
| 334 | + jclass method_class = JJ(FindClass, "java/lang/reflect/Method"); |
| 335 | + String_class = JJ(FindClass, "java/lang/String"); |
| 336 | + |
| 337 | + Class_getField_mid = (*env)->GetMethodID(env, class_class, "getField", |
| 338 | + "(Ljava/lang/String;)Ljava/lang/reflect/Field;"); |
| 339 | + Class_getMethods_mid = (*env)->GetMethodID(env, class_class, "getMethods", |
| 340 | + "()[Ljava/lang/reflect/Method;"); |
| 341 | + Class_getConstructors_mid = (*env)->GetMethodID(env, class_class, "getConstructors", |
| 342 | + "()[Ljava/lang/reflect/Constructor;"); |
| 343 | + Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName", |
| 344 | + "()Ljava/lang/String;"); |
| 345 | + Method_toString_mid = (*env)->GetMethodID(env, method_class, "toString", |
| 346 | + "()Ljava/lang/String;"); |
| 347 | +} |
| 348 | + |
| 349 | +STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) { |
| 350 | + const char *cls_name = mp_obj_str_get_str(cls_name_in); |
| 351 | + if (!env) { |
| 352 | + create_jvm(); |
| 353 | + } |
| 354 | + jclass cls = JJ(FindClass, cls_name); |
| 355 | + |
| 356 | + mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t); |
| 357 | + o->base.type = &jclass_type; |
| 358 | + o->cls = cls; |
| 359 | + return o; |
| 360 | +} |
| 361 | +MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls); |
| 362 | + |
| 363 | +STATIC const mp_map_elem_t mp_module_jni_globals_table[] = { |
| 364 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_jni) }, |
| 365 | + { MP_OBJ_NEW_QSTR(MP_QSTR_cls), (mp_obj_t)&mod_jni_cls_obj }, |
| 366 | +}; |
| 367 | + |
| 368 | +STATIC MP_DEFINE_CONST_DICT(mp_module_jni_globals, mp_module_jni_globals_table); |
| 369 | + |
| 370 | +const mp_obj_module_t mp_module_jni = { |
| 371 | + .base = { &mp_type_module }, |
| 372 | + .name = MP_QSTR_jni, |
| 373 | + .globals = (mp_obj_dict_t*)&mp_module_jni_globals, |
| 374 | +}; |
0 commit comments