2015-09-11 10:57:47 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-09-11 10:57:47 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-10-08 08:38:18 -04:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
|
|
|
|
|
|
|
#if MICROPY_PY_JNI
|
|
|
|
|
2015-09-11 10:57:47 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
2015-09-30 03:54:20 -04:00
|
|
|
#include <ctype.h>
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
#define JJ(call, ...) (*env)->call(env, __VA_ARGS__)
|
2015-09-13 17:12:27 -04:00
|
|
|
#define JJ1(call) (*env)->call(env)
|
2015-10-08 17:25:26 -04:00
|
|
|
#define MATCH(s, static) (!strncmp(s, static, sizeof(static) - 1))
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
static JavaVM *jvm;
|
|
|
|
static JNIEnv *env;
|
2015-09-17 06:31:16 -04:00
|
|
|
static jclass Class_class;
|
2015-09-11 10:57:47 -04:00
|
|
|
static jclass String_class;
|
2015-11-11 09:43:27 -05:00
|
|
|
static jmethodID Class_getName_mid;
|
2015-09-11 10:57:47 -04:00
|
|
|
static jmethodID Class_getField_mid;
|
|
|
|
static jmethodID Class_getMethods_mid;
|
|
|
|
static jmethodID Class_getConstructors_mid;
|
|
|
|
static jmethodID Method_getName_mid;
|
2015-10-03 11:43:41 -04:00
|
|
|
static jmethodID Object_toString_mid;
|
2015-09-11 10:57:47 -04:00
|
|
|
|
2015-09-21 15:02:22 -04:00
|
|
|
static jclass List_class;
|
|
|
|
static jmethodID List_get_mid;
|
|
|
|
static jmethodID List_set_mid;
|
2015-09-29 13:05:30 -04:00
|
|
|
static jmethodID List_size_mid;
|
2015-09-21 15:02:22 -04:00
|
|
|
|
2015-10-02 03:21:07 -04:00
|
|
|
static jclass IndexException_class;
|
|
|
|
|
2015-09-11 10:57:47 -04:00
|
|
|
STATIC const mp_obj_type_t jobject_type;
|
|
|
|
STATIC const mp_obj_type_t jmethod_type;
|
|
|
|
|
2015-09-13 17:12:27 -04:00
|
|
|
STATIC mp_obj_t new_jobject(jobject jo);
|
2015-09-17 06:31:16 -04:00
|
|
|
STATIC mp_obj_t new_jclass(jclass jc);
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, size_t n_args, const mp_obj_t *args);
|
2016-09-15 17:59:48 -04:00
|
|
|
STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
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;
|
2015-09-13 17:12:27 -04:00
|
|
|
bool is_static;
|
2015-09-11 10:57:47 -04:00
|
|
|
} mp_obj_jmethod_t;
|
|
|
|
|
2015-09-23 10:10:55 -04:00
|
|
|
// Utility functions
|
|
|
|
|
|
|
|
STATIC bool is_object_type(const char *jtypesig) {
|
|
|
|
while (*jtypesig != ' ' && *jtypesig) {
|
|
|
|
if (*jtypesig == '.') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
jtypesig++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-01 04:19:33 -04:00
|
|
|
STATIC void check_exception(void) {
|
|
|
|
jobject exc = JJ1(ExceptionOccurred);
|
|
|
|
if (exc) {
|
2020-04-16 03:13:57 -04:00
|
|
|
// JJ1(ExceptionDescribe);
|
2015-10-01 04:19:33 -04:00
|
|
|
mp_obj_t py_e = new_jobject(exc);
|
|
|
|
JJ1(ExceptionClear);
|
2015-10-02 03:21:07 -04:00
|
|
|
if (JJ(IsInstanceOf, exc, IndexException_class)) {
|
2021-07-14 08:56:52 -04:00
|
|
|
mp_raise_type_arg(&mp_type_IndexError, py_e);
|
2015-10-02 03:21:07 -04:00
|
|
|
}
|
2021-07-14 08:56:52 -04:00
|
|
|
mp_raise_type_arg(&mp_type_Exception, py_e);
|
2015-10-01 04:19:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 18:56:40 -04:00
|
|
|
STATIC void print_jobject(const mp_print_t *print, jobject obj) {
|
|
|
|
jobject str_o = JJ(CallObjectMethod, obj, Object_toString_mid);
|
|
|
|
const char *str = JJ(GetStringUTFChars, str_o, NULL);
|
|
|
|
mp_printf(print, str);
|
|
|
|
JJ(ReleaseStringUTFChars, str_o, str);
|
|
|
|
}
|
|
|
|
|
2015-09-11 10:57:47 -04:00
|
|
|
// jclass
|
|
|
|
|
|
|
|
STATIC void jclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jclass_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-10-03 18:56:40 -04:00
|
|
|
if (kind == PRINT_REPR) {
|
|
|
|
mp_printf(print, "<jclass @%p \"", self->cls);
|
|
|
|
}
|
|
|
|
print_jobject(print, self->cls);
|
|
|
|
if (kind == PRINT_REPR) {
|
|
|
|
mp_printf(print, "\">");
|
|
|
|
}
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
|
|
|
|
if (dest[0] == MP_OBJ_NULL) {
|
|
|
|
// load attribute
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jclass_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-09-11 10:57:47 -04:00
|
|
|
const char *attr = qstr_str(attr_in);
|
|
|
|
|
|
|
|
jstring field_name = JJ(NewStringUTF, attr);
|
|
|
|
jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name);
|
2015-09-13 17:12:27 -04:00
|
|
|
if (!JJ1(ExceptionCheck)) {
|
|
|
|
jfieldID field_id = JJ(FromReflectedField, field);
|
|
|
|
jobject obj = JJ(GetStaticObjectField, self->cls, field_id);
|
|
|
|
dest[0] = new_jobject(obj);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-16 03:13:57 -04:00
|
|
|
// JJ1(ExceptionDescribe);
|
2015-09-13 17:12:27 -04:00
|
|
|
JJ1(ExceptionClear);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_jmethod_t *o = mp_obj_malloc(mp_obj_jmethod_t, &jmethod_type);
|
2015-09-13 17:12:27 -04:00
|
|
|
o->name = attr_in;
|
|
|
|
o->meth = NULL;
|
|
|
|
o->obj = self->cls;
|
|
|
|
o->is_static = true;
|
2018-09-20 02:01:13 -04:00
|
|
|
dest[0] = MP_OBJ_FROM_PTR(o);
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t jclass_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-09-11 10:57:47 -04:00
|
|
|
if (n_kw != 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("kwargs not supported"));
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jclass_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid);
|
|
|
|
|
|
|
|
return call_method(self->cls, NULL, methods, true, n_args, args);
|
|
|
|
}
|
|
|
|
|
2017-07-30 03:03:14 -04:00
|
|
|
STATIC const mp_rom_map_elem_t jclass_locals_dict_table[] = {
|
|
|
|
// { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&ffivar_get_obj) },
|
|
|
|
// { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&ffivar_set_obj) },
|
2015-09-11 10:57:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
jclass_type,
|
|
|
|
MP_QSTR_jclass,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
print, jclass_print,
|
|
|
|
attr, jclass_attr,
|
|
|
|
call, jclass_call,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &jclass_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
2015-09-17 06:31:16 -04:00
|
|
|
STATIC mp_obj_t new_jclass(jclass jc) {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_jclass_t *o = mp_obj_malloc(mp_obj_jclass_t, &jclass_type);
|
2015-09-17 06:31:16 -04:00
|
|
|
o->cls = jc;
|
2018-09-20 02:01:13 -04:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2015-09-17 06:31:16 -04:00
|
|
|
}
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
// jobject
|
|
|
|
|
|
|
|
STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jobject_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-10-03 11:43:41 -04:00
|
|
|
if (kind == PRINT_REPR) {
|
|
|
|
mp_printf(print, "<jobject @%p \"", self->obj);
|
|
|
|
}
|
2015-10-03 18:56:40 -04:00
|
|
|
print_jobject(print, self->obj);
|
2015-10-03 11:43:41 -04:00
|
|
|
if (kind == PRINT_REPR) {
|
|
|
|
mp_printf(print, "\">");
|
|
|
|
}
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
|
|
|
|
if (dest[0] == MP_OBJ_NULL) {
|
|
|
|
// load attribute
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jobject_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
2015-10-08 09:55:59 -04:00
|
|
|
const char *attr = qstr_str(attr_in);
|
|
|
|
jclass obj_class = JJ(GetObjectClass, self->obj);
|
|
|
|
jstring field_name = JJ(NewStringUTF, attr);
|
|
|
|
jobject field = JJ(CallObjectMethod, obj_class, Class_getField_mid, field_name);
|
|
|
|
JJ(DeleteLocalRef, field_name);
|
|
|
|
JJ(DeleteLocalRef, obj_class);
|
|
|
|
if (!JJ1(ExceptionCheck)) {
|
|
|
|
jfieldID field_id = JJ(FromReflectedField, field);
|
|
|
|
JJ(DeleteLocalRef, field);
|
|
|
|
jobject obj = JJ(GetObjectField, self->obj, field_id);
|
|
|
|
dest[0] = new_jobject(obj);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-16 03:13:57 -04:00
|
|
|
// JJ1(ExceptionDescribe);
|
2015-10-08 09:55:59 -04:00
|
|
|
JJ1(ExceptionClear);
|
|
|
|
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_jmethod_t *o = mp_obj_malloc(mp_obj_jmethod_t, &jmethod_type);
|
2015-09-11 10:57:47 -04:00
|
|
|
o->name = attr_in;
|
|
|
|
o->meth = NULL;
|
|
|
|
o->obj = self->obj;
|
2015-09-13 17:12:27 -04:00
|
|
|
o->is_static = false;
|
2018-09-20 02:01:13 -04:00
|
|
|
dest[0] = MP_OBJ_FROM_PTR(o);
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 09:43:27 -05:00
|
|
|
STATIC void get_jclass_name(jobject obj, char *buf) {
|
|
|
|
jclass obj_class = JJ(GetObjectClass, obj);
|
|
|
|
jstring name = JJ(CallObjectMethod, obj_class, Class_getName_mid);
|
|
|
|
jint len = JJ(GetStringLength, name);
|
|
|
|
JJ(GetStringUTFRegion, name, 0, len, buf);
|
|
|
|
check_exception();
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:02:22 -04:00
|
|
|
STATIC mp_obj_t jobject_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jobject_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-09-15 17:59:48 -04:00
|
|
|
mp_uint_t idx = mp_obj_get_int(index);
|
|
|
|
char class_name[64];
|
|
|
|
get_jclass_name(self->obj, class_name);
|
2020-04-16 03:13:57 -04:00
|
|
|
// printf("class: %s\n", class_name);
|
2016-09-15 17:59:48 -04:00
|
|
|
|
|
|
|
if (class_name[0] == '[') {
|
|
|
|
if (class_name[1] == 'L' || class_name[1] == '[') {
|
|
|
|
if (value == MP_OBJ_NULL) {
|
|
|
|
// delete
|
|
|
|
assert(0);
|
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
|
|
|
// load
|
|
|
|
jobject el = JJ(GetObjectArrayElement, self->obj, idx);
|
|
|
|
return new_jobject(el);
|
|
|
|
} else {
|
|
|
|
// store
|
|
|
|
jvalue jval;
|
|
|
|
const char *t = class_name + 1;
|
|
|
|
py2jvalue(&t, value, &jval);
|
|
|
|
JJ(SetObjectArrayElement, self->obj, idx, jval.l);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 07:39:36 -04:00
|
|
|
mp_raise_NotImplementedError(NULL);
|
2016-09-15 17:59:48 -04:00
|
|
|
}
|
|
|
|
|
2015-09-21 15:02:22 -04:00
|
|
|
if (!JJ(IsInstanceOf, self->obj, List_class)) {
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (value == MP_OBJ_NULL) {
|
|
|
|
// delete
|
|
|
|
assert(0);
|
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
|
|
|
// load
|
|
|
|
jobject el = JJ(CallObjectMethod, self->obj, List_get_mid, idx);
|
2015-10-07 00:39:41 -04:00
|
|
|
check_exception();
|
2015-09-21 15:02:22 -04:00
|
|
|
return new_jobject(el);
|
|
|
|
} else {
|
|
|
|
// store
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
return MP_OBJ_NULL;
|
2015-09-21 15:02:22 -04:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
STATIC mp_obj_t jobject_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jobject_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-09-29 13:05:30 -04:00
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
case MP_UNARY_OP_LEN: {
|
|
|
|
jint len = JJ(CallIntMethod, self->obj, List_size_mid);
|
|
|
|
if (op == MP_UNARY_OP_BOOL) {
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(len != 0);
|
2015-09-29 13:05:30 -04:00
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(len);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 17:35:28 -04:00
|
|
|
// TODO: subscr_load_adaptor & subscr_getiter convenience functions
|
|
|
|
// should be moved to common location for reuse.
|
|
|
|
STATIC mp_obj_t subscr_load_adaptor(mp_obj_t self_in, mp_obj_t index_in) {
|
|
|
|
return mp_obj_subscr(self_in, index_in, MP_OBJ_SENTINEL);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(subscr_load_adaptor_obj, subscr_load_adaptor);
|
|
|
|
|
|
|
|
// .getiter special method which returns iterator which works in terms
|
|
|
|
// of object subscription.
|
2018-09-08 18:26:06 -04:00
|
|
|
STATIC mp_obj_t subscr_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_t dest[2] = {MP_OBJ_FROM_PTR(&subscr_load_adaptor_obj), self_in};
|
2018-09-08 18:26:06 -04:00
|
|
|
return mp_obj_new_getitem_iter(dest, iter_buf);
|
2015-10-13 17:35:28 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
jobject_type,
|
|
|
|
MP_QSTR_jobject,
|
2022-09-16 09:57:38 -04:00
|
|
|
MP_TYPE_FLAG_ITER_IS_GETITER,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, jobject_print,
|
|
|
|
unary_op, jobject_unary_op,
|
|
|
|
attr, jobject_attr,
|
|
|
|
subscr, jobject_subscr,
|
2022-09-16 09:57:38 -04:00
|
|
|
iter, subscr_getiter,
|
2022-06-24 02:27:46 -04:00
|
|
|
// .locals_dict = &jobject_locals_dict,
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
2015-09-12 18:25:04 -04:00
|
|
|
STATIC mp_obj_t new_jobject(jobject jo) {
|
2015-09-24 18:29:22 -04:00
|
|
|
if (jo == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
} else if (JJ(IsInstanceOf, jo, String_class)) {
|
2015-09-22 20:01:01 -04:00
|
|
|
const char *s = JJ(GetStringUTFChars, jo, NULL);
|
2017-11-15 21:17:51 -05:00
|
|
|
mp_obj_t ret = mp_obj_new_str(s, strlen(s));
|
2015-09-22 20:01:01 -04:00
|
|
|
JJ(ReleaseStringUTFChars, jo, s);
|
|
|
|
return ret;
|
|
|
|
} else if (JJ(IsInstanceOf, jo, Class_class)) {
|
|
|
|
return new_jclass(jo);
|
|
|
|
} else {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_jobject_t *o = mp_obj_malloc(mp_obj_jobject_t, &jobject_type);
|
2015-09-22 20:01:01 -04:00
|
|
|
o->obj = jo;
|
2018-09-20 02:01:13 -04:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2015-09-22 20:01:01 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 18:25:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-11 10:57:47 -04:00
|
|
|
// jmethod
|
|
|
|
|
|
|
|
STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
(void)kind;
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jmethod_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-09-11 10:57:47 -04:00
|
|
|
// Variable value printed as cast to int
|
|
|
|
mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:19:18 -04:00
|
|
|
#define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
#define CHECK_TYPE(java_type_name) \
|
2020-02-26 23:36:53 -05:00
|
|
|
if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
arg_type += sizeof(java_type_name) - 1;
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
STATIC const char *strprev(const char *s, char c) {
|
|
|
|
while (*s != c) {
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:19:18 -04:00
|
|
|
STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
|
|
|
|
const char *arg_type = *jtypesig;
|
2021-01-04 10:22:08 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(arg);
|
2015-09-11 17:19:18 -04:00
|
|
|
|
|
|
|
if (type == &mp_type_str) {
|
|
|
|
if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) {
|
|
|
|
out->l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (type == &mp_type_int) {
|
2015-09-18 06:19:50 -04:00
|
|
|
if (IMATCH(arg_type, "int") || IMATCH(arg_type, "long")) {
|
|
|
|
// TODO: Java long is 64-bit actually
|
|
|
|
out->j = mp_obj_get_int(arg);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-18 18:04:41 -04:00
|
|
|
} else if (type == &jobject_type) {
|
2015-10-21 18:34:03 -04:00
|
|
|
bool is_object = false;
|
2015-10-22 17:33:03 -04:00
|
|
|
const char *expected_type = arg_type;
|
2015-10-21 18:34:03 -04:00
|
|
|
while (1) {
|
|
|
|
if (isalpha(*arg_type)) {
|
|
|
|
} else if (*arg_type == '.') {
|
|
|
|
is_object = true;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2015-09-30 03:54:20 -04:00
|
|
|
arg_type++;
|
|
|
|
}
|
2015-10-21 18:34:03 -04:00
|
|
|
if (!is_object) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jobject_t *jo = MP_OBJ_TO_PTR(arg);
|
2015-10-22 17:33:03 -04:00
|
|
|
if (!MATCH(expected_type, "java.lang.Object")) {
|
|
|
|
char class_name[64];
|
|
|
|
get_jclass_name(jo->obj, class_name);
|
2020-04-16 03:13:57 -04:00
|
|
|
// printf("Arg class: %s\n", class_name);
|
2015-10-22 17:33:03 -04:00
|
|
|
if (strcmp(class_name, expected_type) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 18:04:41 -04:00
|
|
|
out->l = jo->obj;
|
2015-10-09 18:19:28 -04:00
|
|
|
} else if (type == &mp_type_bool) {
|
|
|
|
if (IMATCH(arg_type, "boolean")) {
|
|
|
|
out->z = arg == mp_const_true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (arg == mp_const_none) {
|
2020-04-16 03:13:57 -04:00
|
|
|
// printf("TODO: Check java arg type!!\n");
|
2015-10-09 18:19:28 -04:00
|
|
|
while (isalpha(*arg_type) || *arg_type == '.') {
|
|
|
|
arg_type++;
|
|
|
|
}
|
|
|
|
out->l = NULL;
|
2015-09-11 17:19:18 -04:00
|
|
|
} else {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("arg type not supported"));
|
2015-09-11 17:19:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*jtypesig = arg_type;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-08 17:25:26 -04:00
|
|
|
#if 0
|
2015-09-12 18:25:04 -04:00
|
|
|
// jvalue is known to be union of jobject and friends. And yet from C's
|
|
|
|
// perspective, it's aggregate object which may require passing via stack
|
|
|
|
// instead of registers. Work that around by passing jobject and typecasting
|
|
|
|
// it.
|
|
|
|
STATIC mp_obj_t jvalue2py(const char *jtypesig, jobject arg) {
|
2015-09-13 17:15:08 -04:00
|
|
|
if (arg == NULL || MATCH(jtypesig, "void")) {
|
2015-09-12 18:25:04 -04:00
|
|
|
return mp_const_none;
|
2015-09-19 17:36:26 -04:00
|
|
|
} else if (MATCH(jtypesig, "boolean")) {
|
|
|
|
return mp_obj_new_bool((bool)arg);
|
2015-09-12 18:25:04 -04:00
|
|
|
} else if (MATCH(jtypesig, "int")) {
|
|
|
|
return mp_obj_new_int((mp_int_t)arg);
|
2015-09-23 10:10:55 -04:00
|
|
|
} else if (is_object_type(jtypesig)) {
|
|
|
|
// Non-primitive, object type
|
|
|
|
return new_jobject(arg);
|
2015-09-12 18:25:04 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 10:10:55 -04:00
|
|
|
printf("Unknown return type: %s\n", jtypesig);
|
2015-09-15 07:07:12 -04:00
|
|
|
|
2015-09-12 18:25:04 -04:00
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
2015-10-08 17:25:26 -04:00
|
|
|
#endif
|
2015-09-12 18:25:04 -04:00
|
|
|
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, size_t n_args, const mp_obj_t *args) {
|
2015-09-11 10:57:47 -04:00
|
|
|
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);
|
2015-10-03 11:43:41 -04:00
|
|
|
jobject name_o = JJ(CallObjectMethod, meth, Object_toString_mid);
|
2015-09-11 10:57:47 -04:00
|
|
|
const char *decl = JJ(GetStringUTFChars, name_o, NULL);
|
|
|
|
const char *arg_types = strchr(decl, '(') + 1;
|
2020-04-16 03:13:57 -04:00
|
|
|
// const char *arg_types_end = strchr(arg_types, ')');
|
2015-09-11 10:57:47 -04:00
|
|
|
// 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);
|
2020-02-26 23:36:53 -05:00
|
|
|
if (strncmp(name, meth_name, name_len /*arg_types - meth_name - 1*/) || meth_name[name_len] != '(' /*(*/) {
|
2015-09-26 11:50:37 -04:00
|
|
|
goto next_method;
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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;
|
2018-09-20 02:01:13 -04:00
|
|
|
for (size_t j = 0; j < n_args && *arg_types != ')'; j++) {
|
|
|
|
if (!py2jvalue(&arg_types, args[j], &jargs[j])) {
|
2015-09-11 17:19:18 -04:00
|
|
|
goto next_method;
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*arg_types == ',') {
|
|
|
|
arg_types++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:54:20 -04:00
|
|
|
if (*arg_types != ')') {
|
|
|
|
goto next_method;
|
|
|
|
}
|
|
|
|
|
2015-09-11 10:57:47 -04:00
|
|
|
if (found) {
|
|
|
|
// printf("found!\n");
|
|
|
|
jmethodID method_id = JJ(FromReflectedMethod, meth);
|
|
|
|
if (is_constr) {
|
2015-09-12 18:25:04 -04:00
|
|
|
JJ(ReleaseStringUTFChars, name_o, decl);
|
2018-09-20 02:01:13 -04:00
|
|
|
jobject res = JJ(NewObjectA, obj, method_id, jargs);
|
2015-09-12 18:25:04 -04:00
|
|
|
return new_jobject(res);
|
2015-09-11 10:57:47 -04:00
|
|
|
} else {
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_t ret;
|
2015-09-25 20:11:24 -04:00
|
|
|
if (MATCH(ret_type, "void")) {
|
|
|
|
JJ(CallVoidMethodA, obj, method_id, jargs);
|
2015-10-13 17:24:36 -04:00
|
|
|
check_exception();
|
2015-09-27 05:30:28 -04:00
|
|
|
ret = mp_const_none;
|
2015-09-25 20:11:24 -04:00
|
|
|
} else if (MATCH(ret_type, "int")) {
|
|
|
|
jint res = JJ(CallIntMethodA, obj, method_id, jargs);
|
2015-10-13 17:24:36 -04:00
|
|
|
check_exception();
|
2015-09-27 05:30:28 -04:00
|
|
|
ret = mp_obj_new_int(res);
|
2015-09-25 20:11:24 -04:00
|
|
|
} else if (MATCH(ret_type, "boolean")) {
|
|
|
|
jboolean res = JJ(CallBooleanMethodA, obj, method_id, jargs);
|
2015-10-13 17:24:36 -04:00
|
|
|
check_exception();
|
2015-09-27 05:30:28 -04:00
|
|
|
ret = mp_obj_new_bool(res);
|
2015-09-25 20:11:24 -04:00
|
|
|
} else if (is_object_type(ret_type)) {
|
2018-09-20 02:01:13 -04:00
|
|
|
jobject res = JJ(CallObjectMethodA, obj, method_id, jargs);
|
2015-10-13 17:24:36 -04:00
|
|
|
check_exception();
|
2015-09-27 05:30:28 -04:00
|
|
|
ret = new_jobject(res);
|
|
|
|
} else {
|
2015-09-25 20:11:24 -04:00
|
|
|
JJ(ReleaseStringUTFChars, name_o, decl);
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("can't handle return type"));
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
2015-09-27 05:30:28 -04:00
|
|
|
|
|
|
|
JJ(ReleaseStringUTFChars, name_o, decl);
|
2015-09-28 11:36:43 -04:00
|
|
|
JJ(DeleteLocalRef, name_o);
|
|
|
|
JJ(DeleteLocalRef, meth);
|
2015-09-27 05:30:28 -04:00
|
|
|
return ret;
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
next_method:
|
2015-09-11 10:57:47 -04:00
|
|
|
JJ(ReleaseStringUTFChars, name_o, decl);
|
2015-09-28 11:36:43 -04:00
|
|
|
JJ(DeleteLocalRef, name_o);
|
|
|
|
JJ(DeleteLocalRef, meth);
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("method not found"));
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t jmethod_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-09-11 10:57:47 -04:00
|
|
|
if (n_kw != 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("kwargs not supported"));
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jmethod_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
const char *name = qstr_str(self->name);
|
|
|
|
// jstring meth_name = JJ(NewStringUTF, name);
|
|
|
|
|
2015-09-13 17:12:27 -04:00
|
|
|
jclass obj_class = self->obj;
|
|
|
|
if (!self->is_static) {
|
|
|
|
obj_class = JJ(GetObjectClass, self->obj);
|
|
|
|
}
|
2015-09-11 10:57:47 -04:00
|
|
|
jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid);
|
|
|
|
|
|
|
|
return call_method(self->obj, name, methods, false, n_args, args);
|
|
|
|
}
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
jmethod_type,
|
|
|
|
MP_QSTR_jmethod,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
print, jmethod_print,
|
|
|
|
call, jmethod_call,
|
|
|
|
// .attr = jobject_attr,
|
2022-06-24 02:27:46 -04:00
|
|
|
// .locals_dict = &jobject_locals_dict,
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2015-09-11 10:57:47 -04:00
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
#define LIBJVM_SO "libdvm.so"
|
|
|
|
#else
|
|
|
|
#define LIBJVM_SO "libjvm.so"
|
|
|
|
#endif
|
|
|
|
|
2018-09-20 02:01:13 -04:00
|
|
|
STATIC void create_jvm(void) {
|
2015-09-11 10:57:47 -04:00
|
|
|
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) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("unable to load libjvm.so, use LD_LIBRARY_PATH"));
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
2020-02-26 23:36:53 -05:00
|
|
|
int (*_JNI_CreateJavaVM)(void *, void **, void *) = dlsym(libjvm, "JNI_CreateJavaVM");
|
2015-09-11 10:57:47 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
int st = _JNI_CreateJavaVM(&jvm, (void **)&env, &args);
|
2015-09-11 10:57:47 -04:00
|
|
|
if (st < 0 || !env) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("unable to create JVM"));
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
2015-09-17 06:31:16 -04:00
|
|
|
Class_class = JJ(FindClass, "java/lang/Class");
|
2015-09-11 10:57:47 -04:00
|
|
|
jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
|
|
|
|
String_class = JJ(FindClass, "java/lang/String");
|
|
|
|
|
2015-10-03 11:43:41 -04:00
|
|
|
jclass Object_class = JJ(FindClass, "java/lang/Object");
|
|
|
|
Object_toString_mid = JJ(GetMethodID, Object_class, "toString",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("()Ljava/lang/String;"));
|
2015-10-03 11:43:41 -04:00
|
|
|
|
2015-11-11 09:43:27 -05:00
|
|
|
Class_getName_mid = (*env)->GetMethodID(env, Class_class, "getName",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("()Ljava/lang/String;"));
|
2015-09-17 06:31:16 -04:00
|
|
|
Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("(Ljava/lang/String;)Ljava/lang/reflect/Field;"));
|
2015-09-17 06:31:16 -04:00
|
|
|
Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("()[Ljava/lang/reflect/Method;"));
|
2015-09-17 06:31:16 -04:00
|
|
|
Class_getConstructors_mid = (*env)->GetMethodID(env, Class_class, "getConstructors",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("()[Ljava/lang/reflect/Constructor;"));
|
2015-09-11 10:57:47 -04:00
|
|
|
Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("()Ljava/lang/String;"));
|
2015-09-21 15:02:22 -04:00
|
|
|
|
|
|
|
List_class = JJ(FindClass, "java/util/List");
|
|
|
|
List_get_mid = JJ(GetMethodID, List_class, "get",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("(I)Ljava/lang/Object;"));
|
2015-09-21 15:02:22 -04:00
|
|
|
List_set_mid = JJ(GetMethodID, List_class, "set",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("(ILjava/lang/Object;)Ljava/lang/Object;"));
|
2015-09-29 13:05:30 -04:00
|
|
|
List_size_mid = JJ(GetMethodID, List_class, "size",
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("()I"));
|
2015-10-02 03:21:07 -04:00
|
|
|
IndexException_class = JJ(FindClass, "java/lang/IndexOutOfBoundsException");
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_jclass_t *o = mp_obj_malloc(mp_obj_jclass_t, &jclass_type);
|
2015-09-11 10:57:47 -04:00
|
|
|
o->cls = cls;
|
2018-09-20 02:01:13 -04:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2015-09-11 10:57:47 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
|
|
|
|
|
2016-09-17 09:14:02 -04:00
|
|
|
STATIC mp_obj_t mod_jni_array(mp_obj_t type_in, mp_obj_t size_in) {
|
|
|
|
if (!env) {
|
|
|
|
create_jvm();
|
|
|
|
}
|
2016-09-18 06:37:40 -04:00
|
|
|
mp_int_t size = mp_obj_get_int(size_in);
|
2016-09-17 09:14:02 -04:00
|
|
|
jobject res = NULL;
|
2016-09-18 06:37:40 -04:00
|
|
|
|
2019-01-30 06:05:48 -05:00
|
|
|
if (mp_obj_is_type(type_in, &jclass_type)) {
|
2016-09-18 06:37:40 -04:00
|
|
|
|
2018-09-20 02:01:13 -04:00
|
|
|
mp_obj_jclass_t *jcls = MP_OBJ_TO_PTR(type_in);
|
2016-09-18 06:37:40 -04:00
|
|
|
res = JJ(NewObjectArray, size, jcls->cls, NULL);
|
|
|
|
|
2019-01-30 06:05:48 -05:00
|
|
|
} else if (mp_obj_is_str(type_in)) {
|
2016-09-18 06:37:40 -04:00
|
|
|
const char *type = mp_obj_str_get_str(type_in);
|
|
|
|
switch (*type) {
|
|
|
|
case 'Z':
|
|
|
|
res = JJ(NewBooleanArray, size);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
res = JJ(NewByteArray, size);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
res = JJ(NewCharArray, size);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
res = JJ(NewShortArray, size);
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
res = JJ(NewIntArray, size);
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
res = JJ(NewLongArray, size);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
res = JJ(NewFloatArray, size);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
res = JJ(NewDoubleArray, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-17 09:14:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_jobject(res);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mod_jni_array_obj, mod_jni_array);
|
|
|
|
|
|
|
|
|
2018-09-20 02:01:13 -04:00
|
|
|
STATIC mp_obj_t mod_jni_env(void) {
|
|
|
|
return mp_obj_new_int((mp_int_t)(uintptr_t)env);
|
2015-09-15 18:09:39 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
|
|
|
|
|
2017-07-30 03:03:14 -04:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_jni_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_jni) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cls), MP_ROM_PTR(&mod_jni_cls_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mod_jni_array_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_env), MP_ROM_PTR(&mod_jni_env_obj) },
|
2015-09-11 10:57:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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 },
|
2020-02-26 23:36:53 -05:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_jni_globals,
|
2015-09-11 10:57:47 -04:00
|
|
|
};
|
2022-04-20 07:24:01 -04:00
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_jni, mp_module_jni);
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_JNI
|