diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-09-17 16:14:02 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-09-17 16:14:02 +0300 |
commit | ee324c501eee738bedf917123abae4eb613268f8 (patch) | |
tree | af26b328efc4a1a560fdfbc9bdb09c709766dc3a | |
parent | b9672bcbe8dd29b61326af8eb026df4d10a8f0ce (diff) | |
download | micropython-ee324c501eee738bedf917123abae4eb613268f8.tar.gz micropython-ee324c501eee738bedf917123abae4eb613268f8.zip |
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.
-rw-r--r-- | unix/modjni.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/unix/modjni.c b/unix/modjni.c index 789b3139aa..5d1de17f5d 100644 --- a/unix/modjni.c +++ b/unix/modjni.c @@ -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 }, }; |