Skip to content

Commit 4e7bde8

Browse files
committed
unix/modjni: Factor out py2jvalue() function.
1 parent 9d6128a commit 4e7bde8

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

unix/modjni.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,13 @@ STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
163163
mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
164164
}
165165

166-
#define MATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
166+
#define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
167167

168168
#define CHECK_TYPE(java_type_name) \
169-
if (strncmp(arg_types, java_type_name, sizeof(java_type_name) - 1) != 0) { \
170-
found = false; \
171-
break; \
169+
if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
170+
return false; \
172171
} \
173-
arg_types += sizeof(java_type_name) - 1;
172+
arg_type += sizeof(java_type_name) - 1;
174173

175174
STATIC const char *strprev(const char *s, char c) {
176175
while (*s != c) {
@@ -179,6 +178,27 @@ STATIC const char *strprev(const char *s, char c) {
179178
return s;
180179
}
181180

181+
STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
182+
const char *arg_type = *jtypesig;
183+
mp_obj_type_t *type = mp_obj_get_type(arg);
184+
185+
if (type == &mp_type_str) {
186+
if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) {
187+
out->l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
188+
} else {
189+
return false;
190+
}
191+
} else if (type == &mp_type_int) {
192+
CHECK_TYPE("long");
193+
out->j = mp_obj_get_int(arg);
194+
} else {
195+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported"));
196+
}
197+
198+
*jtypesig = arg_type;
199+
return true;
200+
}
201+
182202
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) {
183203
jvalue jargs[n_args];
184204
// printf("methods=%p\n", methods);
@@ -209,20 +229,8 @@ STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool
209229

210230
bool found = true;
211231
for (int i = 0; i < n_args; i++) {
212-
mp_obj_t arg = args[i];
213-
mp_obj_type_t *type = mp_obj_get_type(arg);
214-
if (type == &mp_type_str) {
215-
// CHECK_TYPE("java.lang.String");
216-
if (MATCH(arg_types, "java.lang.String") || MATCH(arg_types, "java.lang.Object")) {
217-
jargs[i].l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
218-
} else {
219-
found = false;
220-
}
221-
} else if (type == &mp_type_int) {
222-
CHECK_TYPE("long");
223-
jargs[i].j = mp_obj_get_int(arg);
224-
} else {
225-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "arg type not supported"));
232+
if (!py2jvalue(&arg_types, args[i], &jargs[i])) {
233+
goto next_method;
226234
}
227235

228236
if (*arg_types == ',') {
@@ -269,6 +277,7 @@ ret_string:;
269277
}
270278
}
271279

280+
next_method:
272281
JJ(ReleaseStringUTFChars, name_o, decl);
273282
}
274283

0 commit comments

Comments
 (0)