unix/modjni: Add array() top-level function to create Java array.
Takes element primitive type encoded as a char per standard JNI encoding, and array size. TODO: Support object arrays.
This commit is contained in:
parent
b9672bcbe8
commit
ee324c501e
@ -654,6 +654,46 @@ STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
|
||||
|
||||
STATIC mp_obj_t mod_jni_array(mp_obj_t type_in, mp_obj_t size_in) {
|
||||
const char *type = mp_obj_str_get_str(type_in);
|
||||
mp_int_t size = mp_obj_get_int(size_in);
|
||||
if (!env) {
|
||||
create_jvm();
|
||||
}
|
||||
|
||||
jobject res = NULL;
|
||||
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;
|
||||
}
|
||||
|
||||
return new_jobject(res);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mod_jni_array_obj, mod_jni_array);
|
||||
|
||||
|
||||
STATIC mp_obj_t mod_jni_env() {
|
||||
return mp_obj_new_int((mp_int_t)env);
|
||||
}
|
||||
@ -662,6 +702,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
|
||||
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 },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mod_jni_array_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_env), (mp_obj_t)&mod_jni_env_obj },
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user