Skip to content

Commit ee324c5

Browse files
committed
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.
1 parent b9672bc commit ee324c5

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

unix/modjni.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,46 @@ STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) {
654654
}
655655
MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
656656

657+
STATIC mp_obj_t mod_jni_array(mp_obj_t type_in, mp_obj_t size_in) {
658+
const char *type = mp_obj_str_get_str(type_in);
659+
mp_int_t size = mp_obj_get_int(size_in);
660+
if (!env) {
661+
create_jvm();
662+
}
663+
664+
jobject res = NULL;
665+
switch (*type) {
666+
case 'Z':
667+
res = JJ(NewBooleanArray, size);
668+
break;
669+
case 'B':
670+
res = JJ(NewByteArray, size);
671+
break;
672+
case 'C':
673+
res = JJ(NewCharArray, size);
674+
break;
675+
case 'S':
676+
res = JJ(NewShortArray, size);
677+
break;
678+
case 'I':
679+
res = JJ(NewIntArray, size);
680+
break;
681+
case 'J':
682+
res = JJ(NewLongArray, size);
683+
break;
684+
case 'F':
685+
res = JJ(NewFloatArray, size);
686+
break;
687+
case 'D':
688+
res = JJ(NewDoubleArray, size);
689+
break;
690+
}
691+
692+
return new_jobject(res);
693+
}
694+
MP_DEFINE_CONST_FUN_OBJ_2(mod_jni_array_obj, mod_jni_array);
695+
696+
657697
STATIC mp_obj_t mod_jni_env() {
658698
return mp_obj_new_int((mp_int_t)env);
659699
}
@@ -662,6 +702,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
662702
STATIC const mp_map_elem_t mp_module_jni_globals_table[] = {
663703
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_jni) },
664704
{ MP_OBJ_NEW_QSTR(MP_QSTR_cls), (mp_obj_t)&mod_jni_cls_obj },
705+
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mod_jni_array_obj },
665706
{ MP_OBJ_NEW_QSTR(MP_QSTR_env), (mp_obj_t)&mod_jni_env_obj },
666707
};
667708

0 commit comments

Comments
 (0)