From e79c6b6312a85a50d113346144d58fa62cb4adc2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 11 Sep 2015 17:57:47 +0300 Subject: [PATCH] unix/modjni: "jni" module to interface to JNI-compliant JavaVM. This includes Android Dalvik VM for example. Example usage: import jni System = jni.cls("java/lang/System") System.out.println("Hello, Java!") --- unix/Makefile | 5 + unix/modjni.c | 374 +++++++++++++++++++++++++++++++++++++++++++ unix/mpconfigport.h | 7 + unix/mpconfigport.mk | 3 + unix/qstrdefsport.h | 8 + 5 files changed, 397 insertions(+) create mode 100644 unix/modjni.c diff --git a/unix/Makefile b/unix/Makefile index edc800b633..ba3ecdaa02 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -101,6 +101,11 @@ LDFLAGS_MOD += $(LIBFFI_LDFLAGS_MOD) SRC_MOD += modffi.c endif +ifeq ($(MICROPY_PY_JNI),1) +# Path for 64-bit OpenJDK, should be adjusted for other JDKs +CFLAGS_MOD += -I/usr/lib/jvm/java-7-openjdk-amd64/include -DMICROPY_PY_JNI=1 +SRC_MOD += modjni.c +endif # source files SRC_C = \ diff --git a/unix/modjni.c b/unix/modjni.c new file mode 100644 index 0000000000..b111e69aca --- /dev/null +++ b/unix/modjni.c @@ -0,0 +1,374 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2015 Paul Sokolovsky + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" + +#include + +#define JJ(call, ...) (*env)->call(env, __VA_ARGS__) + +static JavaVM *jvm; +static JNIEnv *env; +static jclass String_class; +static jmethodID Class_getField_mid; +static jmethodID Class_getMethods_mid; +static jmethodID Class_getConstructors_mid; +static jmethodID Method_getName_mid; +static jmethodID Method_toString_mid; + +STATIC const mp_obj_type_t jobject_type; +STATIC const mp_obj_type_t jmethod_type; + +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); + +typedef struct _mp_obj_jclass_t { + mp_obj_base_t base; + jclass cls; +} mp_obj_jclass_t; + +typedef struct _mp_obj_jobject_t { + mp_obj_base_t base; + jobject obj; +} mp_obj_jobject_t; + +typedef struct _mp_obj_jmethod_t { + mp_obj_base_t base; + jobject obj; + jmethodID meth; + qstr name; +} mp_obj_jmethod_t; + +// jclass + +STATIC void jclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + (void)kind; + mp_obj_jclass_t *self = self_in; + // Variable value printed as cast to int + mp_printf(print, "", self->cls); +} + +STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { + if (dest[0] == MP_OBJ_NULL) { + // load attribute + mp_obj_jclass_t *self = self_in; + const char *attr = qstr_str(attr_in); + + jstring field_name = JJ(NewStringUTF, attr); + jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name); + jfieldID field_id = JJ(FromReflectedField, field); + jobject obj = JJ(GetStaticObjectField, self->cls, field_id); + + mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t); + o->base.type = &jobject_type; + o->obj = obj; + dest[0] = o; + } +} + +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) { + if (n_kw != 0) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported")); + } + mp_obj_jclass_t *self = self_in; + + jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid); + + return call_method(self->cls, NULL, methods, true, n_args, args); +} + +STATIC const mp_map_elem_t jclass_locals_dict_table[] = { +// { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&ffivar_get_obj }, +// { MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&ffivar_set_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table); + +STATIC const mp_obj_type_t jclass_type = { + { &mp_type_type }, + .name = MP_QSTR_jclass, + .print = jclass_print, + .attr = jclass_attr, + .call = jclass_call, + .locals_dict = (mp_obj_t)&jclass_locals_dict, +}; + + +// jobject + +STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + (void)kind; + mp_obj_jobject_t *self = self_in; + // Variable value printed as cast to int + mp_printf(print, "", self->obj); +} + +STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) { + if (dest[0] == MP_OBJ_NULL) { + // load attribute + mp_obj_jobject_t *self = self_in; + + mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t); + o->base.type = &jmethod_type; + o->name = attr_in; + o->meth = NULL; + o->obj = self->obj; + dest[0] = o; + } +} + +STATIC const mp_obj_type_t jobject_type = { + { &mp_type_type }, + .name = MP_QSTR_jobject, + .print = jobject_print, + .attr = jobject_attr, +// .locals_dict = (mp_obj_t)&jobject_locals_dict, +}; + +// jmethod + +STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + (void)kind; + mp_obj_jmethod_t *self = self_in; + // Variable value printed as cast to int + mp_printf(print, "", qstr_str(self->name)); +} + +#define MATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1)) + +#define CHECK_TYPE(java_type_name) \ + if (strncmp(arg_types, java_type_name, sizeof(java_type_name) - 1) != 0) { \ + found = false; \ + break; \ + } \ + arg_types += sizeof(java_type_name) - 1; + +STATIC const char *strprev(const char *s, char c) { + while (*s != c) { + s--; + } + return s; +} + +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) { + jvalue jargs[n_args]; +// printf("methods=%p\n", methods); + jsize num_methods = JJ(GetArrayLength, methods); + for (int i = 0; i < num_methods; i++) { + jobject meth = JJ(GetObjectArrayElement, methods, i); + jobject name_o = JJ(CallObjectMethod, meth, Method_toString_mid); + const char *decl = JJ(GetStringUTFChars, name_o, NULL); + const char *arg_types = strchr(decl, '(') + 1; + //const char *arg_types_end = strchr(arg_types, ')'); +// printf("method[%d]=%p %s\n", i, meth, decl); + + const char *meth_name = NULL; + const char *ret_type = NULL; + if (!is_constr) { + meth_name = strprev(arg_types, '.') + 1; + ret_type = strprev(meth_name, ' ') - 1; + ret_type = strprev(ret_type, ' ') + 1; + + int name_len = strlen(name); + if (strncmp(name, meth_name, name_len/*arg_types - meth_name - 1*/) || meth_name[name_len] != '('/*(*/) { + continue; + } + } +// printf("method[%d]=%p %s\n", i, meth, decl); +// printf("!!!%s\n", arg_types); +// printf("name=%p meth_name=%s\n", name, meth_name); + + bool found = true; + for (int i = 0; i < n_args; i++) { + mp_obj_t arg = args[i]; + mp_obj_type_t *type = mp_obj_get_type(arg); + if (type == &mp_type_str) { +// CHECK_TYPE("java.lang.String"); + if (MATCH(arg_types, "java.lang.String") || MATCH(arg_types, "java.lang.Object")) { + jargs[i].l = JJ(NewStringUTF, mp_obj_str_get_str(arg)); + } else { + found = false; + } + } else if (type == &mp_type_int) { + CHECK_TYPE("long"); + jargs[i].j = mp_obj_get_int(arg); + } else { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported")); + } + + if (*arg_types == ',') { + arg_types++; + } + } + + if (found) { +// printf("found!\n"); + jmethodID method_id = JJ(FromReflectedMethod, meth); + jobject res; + if (is_constr) { + res = JJ(NewObjectA, obj, method_id, jargs); + mp_obj_jobject_t *o; +ret_object: + o = m_new_obj(mp_obj_jobject_t); + o->base.type = &jobject_type; + o->obj = res; + return o; + } else { + res = JJ(CallObjectMethodA, obj, method_id, jargs); + mp_obj_t ret = MP_OBJ_NULL; + if (strncmp(ret_type, "void", 4) == 0) { + ret = mp_const_none; + } else if (strncmp(ret_type, "int", sizeof("int") - 1) == 0) { + ret = mp_obj_new_int((mp_int_t)res); + } else if (strncmp(ret_type, "java.lang.String", sizeof("java.lang.String") - 1) == 0) { +ret_string:; + const char *s = JJ(GetStringUTFChars, res, NULL); + ret = mp_obj_new_str(s, strlen(s), false); + JJ(ReleaseStringUTFChars, res, s); + } else if (strncmp(ret_type, "java.lang.Object", sizeof("java.lang.Object") - 1) == 0) { + if (JJ(IsInstanceOf, res, String_class)) { + goto ret_string; + } else { + goto ret_object; + } + } + JJ(ReleaseStringUTFChars, name_o, decl); + if (ret != MP_OBJ_NULL) { + return ret; + } + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "cannot handle return type")); + } + } + + JJ(ReleaseStringUTFChars, name_o, decl); + } + + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "method not found")); +} + + +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) { + if (n_kw != 0) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "kwargs not supported")); + } + mp_obj_jmethod_t *self = self_in; + + const char *name = qstr_str(self->name); +// jstring meth_name = JJ(NewStringUTF, name); + + jclass obj_class = JJ(GetObjectClass, self->obj); + jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid); + + return call_method(self->obj, name, methods, false, n_args, args); +} + +STATIC const mp_obj_type_t jmethod_type = { + { &mp_type_type }, + .name = MP_QSTR_jmethod, + .print = jmethod_print, + .call = jmethod_call, +// .attr = jobject_attr, +// .locals_dict = (mp_obj_t)&jobject_locals_dict, +}; + +#ifdef __ANDROID__ +#define LIBJVM_SO "libdvm.so" +#else +#define LIBJVM_SO "libjvm.so" +#endif + +STATIC void create_jvm() { + JavaVMInitArgs args; + JavaVMOption options; + options.optionString = "-Djava.class.path=."; + args.version = JNI_VERSION_1_6; + args.nOptions = 1; + args.options = &options; + args.ignoreUnrecognized = 0; + + if (env) { + return; + } + + void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL); + if (!libjvm) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH")); + } + int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM"); + + int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args); + if (st < 0 || !env) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "unable to create JVM")); + } + + jclass class_class = JJ(FindClass, "java/lang/Class"); + jclass method_class = JJ(FindClass, "java/lang/reflect/Method"); + String_class = JJ(FindClass, "java/lang/String"); + + Class_getField_mid = (*env)->GetMethodID(env, class_class, "getField", + "(Ljava/lang/String;)Ljava/lang/reflect/Field;"); + Class_getMethods_mid = (*env)->GetMethodID(env, class_class, "getMethods", + "()[Ljava/lang/reflect/Method;"); + Class_getConstructors_mid = (*env)->GetMethodID(env, class_class, "getConstructors", + "()[Ljava/lang/reflect/Constructor;"); + Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName", + "()Ljava/lang/String;"); + Method_toString_mid = (*env)->GetMethodID(env, method_class, "toString", + "()Ljava/lang/String;"); +} + +STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) { + const char *cls_name = mp_obj_str_get_str(cls_name_in); + if (!env) { + create_jvm(); + } + jclass cls = JJ(FindClass, cls_name); + + mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t); + o->base.type = &jclass_type; + o->cls = cls; + return o; +} +MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls); + +STATIC const mp_map_elem_t mp_module_jni_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_jni) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_cls), (mp_obj_t)&mod_jni_cls_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_jni_globals, mp_module_jni_globals_table); + +const mp_obj_module_t mp_module_jni = { + .base = { &mp_type_module }, + .name = MP_QSTR_jni, + .globals = (mp_obj_dict_t*)&mp_module_jni_globals, +}; diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index ccc3d4af2f..d503aea975 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -123,12 +123,18 @@ extern const struct _mp_obj_module_t mp_module_time; extern const struct _mp_obj_module_t mp_module_termios; extern const struct _mp_obj_module_t mp_module_socket; extern const struct _mp_obj_module_t mp_module_ffi; +extern const struct _mp_obj_module_t mp_module_jni; #if MICROPY_PY_FFI #define MICROPY_PY_FFI_DEF { MP_OBJ_NEW_QSTR(MP_QSTR_ffi), (mp_obj_t)&mp_module_ffi }, #else #define MICROPY_PY_FFI_DEF #endif +#if MICROPY_PY_JNI +#define MICROPY_PY_JNI_DEF { MP_OBJ_NEW_QSTR(MP_QSTR_jni), (mp_obj_t)&mp_module_jni }, +#else +#define MICROPY_PY_JNI_DEF +#endif #if MICROPY_PY_TIME #define MICROPY_PY_TIME_DEF { MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_time }, #else @@ -147,6 +153,7 @@ extern const struct _mp_obj_module_t mp_module_ffi; #define MICROPY_PORT_BUILTIN_MODULES \ MICROPY_PY_FFI_DEF \ + MICROPY_PY_JNI_DEF \ MICROPY_PY_TIME_DEF \ MICROPY_PY_SOCKET_DEF \ { MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \ diff --git a/unix/mpconfigport.mk b/unix/mpconfigport.mk index 84b8e437fa..7832841e4e 100644 --- a/unix/mpconfigport.mk +++ b/unix/mpconfigport.mk @@ -20,3 +20,6 @@ MICROPY_PY_SOCKET = 1 # ffi module requires libffi (libffi-dev Debian package) MICROPY_PY_FFI = 1 + +# jni module requires JVM/JNI +MICROPY_PY_JNI = 0 diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h index 04bfad070d..0ae8f8007b 100644 --- a/unix/qstrdefsport.h +++ b/unix/qstrdefsport.h @@ -102,3 +102,11 @@ Q(B9600) Q(B57600) Q(B115200) #endif + +#if MICROPY_PY_JNI +Q(jni) +Q(cls) +Q(jclass) +Q(jobject) +Q(jmethod) +#endif