Commit 4e7bde8c authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

unix/modjni: Factor out py2jvalue() function.

parent 9d6128ac
...@@ -163,14 +163,13 @@ STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki ...@@ -163,14 +163,13 @@ STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
mp_printf(print, "<jmethod '%s'>", qstr_str(self->name)); mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
} }
#define MATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1)) #define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
#define CHECK_TYPE(java_type_name) \ #define CHECK_TYPE(java_type_name) \
if (strncmp(arg_types, java_type_name, sizeof(java_type_name) - 1) != 0) { \ if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
found = false; \ return false; \
break; \
} \ } \
arg_types += sizeof(java_type_name) - 1; arg_type += sizeof(java_type_name) - 1;
STATIC const char *strprev(const char *s, char c) { STATIC const char *strprev(const char *s, char c) {
while (*s != c) { while (*s != c) {
...@@ -179,6 +178,27 @@ STATIC const char *strprev(const char *s, char c) { ...@@ -179,6 +178,27 @@ STATIC const char *strprev(const char *s, char c) {
return s; return s;
} }
STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
const char *arg_type = *jtypesig;
mp_obj_type_t *type = mp_obj_get_type(arg);
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) {
CHECK_TYPE("long");
out->j = mp_obj_get_int(arg);
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported"));
}
*jtypesig = arg_type;
return true;
}
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) { 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]; jvalue jargs[n_args];
// printf("methods=%p\n", methods); // printf("methods=%p\n", methods);
...@@ -209,20 +229,8 @@ STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool ...@@ -209,20 +229,8 @@ STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool
bool found = true; bool found = true;
for (int i = 0; i < n_args; i++) { for (int i = 0; i < n_args; i++) {
mp_obj_t arg = args[i]; if (!py2jvalue(&arg_types, args[i], &jargs[i])) {
mp_obj_type_t *type = mp_obj_get_type(arg); goto next_method;
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 == ',') { if (*arg_types == ',') {
...@@ -269,6 +277,7 @@ ret_string:; ...@@ -269,6 +277,7 @@ ret_string:;
} }
} }
next_method:
JJ(ReleaseStringUTFChars, name_o, decl); JJ(ReleaseStringUTFChars, name_o, decl);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment