modjni.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Paul Sokolovsky
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <assert.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <dlfcn.h>
  30. #include <ctype.h>
  31. #include "py/runtime.h"
  32. #include "py/binary.h"
  33. #include <jni.h>
  34. #define JJ(call, ...) (*env)->call(env, __VA_ARGS__)
  35. #define JJ1(call) (*env)->call(env)
  36. #define MATCH(s, static) (!strncmp(s, static, sizeof(static) - 1))
  37. static JavaVM *jvm;
  38. static JNIEnv *env;
  39. static jclass Class_class;
  40. static jclass String_class;
  41. static jmethodID Class_getName_mid;
  42. static jmethodID Class_getField_mid;
  43. static jmethodID Class_getMethods_mid;
  44. static jmethodID Class_getConstructors_mid;
  45. static jmethodID Method_getName_mid;
  46. static jmethodID Object_toString_mid;
  47. static jclass List_class;
  48. static jmethodID List_get_mid;
  49. static jmethodID List_set_mid;
  50. static jmethodID List_size_mid;
  51. static jclass IndexException_class;
  52. STATIC const mp_obj_type_t jobject_type;
  53. STATIC const mp_obj_type_t jmethod_type;
  54. STATIC mp_obj_t new_jobject(jobject jo);
  55. STATIC mp_obj_t new_jclass(jclass jc);
  56. STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, size_t n_args, const mp_obj_t *args);
  57. STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out);
  58. typedef struct _mp_obj_jclass_t {
  59. mp_obj_base_t base;
  60. jclass cls;
  61. } mp_obj_jclass_t;
  62. typedef struct _mp_obj_jobject_t {
  63. mp_obj_base_t base;
  64. jobject obj;
  65. } mp_obj_jobject_t;
  66. typedef struct _mp_obj_jmethod_t {
  67. mp_obj_base_t base;
  68. jobject obj;
  69. jmethodID meth;
  70. qstr name;
  71. bool is_static;
  72. } mp_obj_jmethod_t;
  73. // Utility functions
  74. STATIC bool is_object_type(const char *jtypesig) {
  75. while (*jtypesig != ' ' && *jtypesig) {
  76. if (*jtypesig == '.') {
  77. return true;
  78. }
  79. jtypesig++;
  80. }
  81. return false;
  82. }
  83. STATIC void check_exception(void) {
  84. jobject exc = JJ1(ExceptionOccurred);
  85. if (exc) {
  86. //JJ1(ExceptionDescribe);
  87. mp_obj_t py_e = new_jobject(exc);
  88. JJ1(ExceptionClear);
  89. if (JJ(IsInstanceOf, exc, IndexException_class)) {
  90. nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, py_e));
  91. }
  92. nlr_raise(mp_obj_new_exception_arg1(&mp_type_Exception, py_e));
  93. }
  94. }
  95. STATIC void print_jobject(const mp_print_t *print, jobject obj) {
  96. jobject str_o = JJ(CallObjectMethod, obj, Object_toString_mid);
  97. const char *str = JJ(GetStringUTFChars, str_o, NULL);
  98. mp_printf(print, str);
  99. JJ(ReleaseStringUTFChars, str_o, str);
  100. }
  101. // jclass
  102. STATIC void jclass_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  103. mp_obj_jclass_t *self = self_in;
  104. if (kind == PRINT_REPR) {
  105. mp_printf(print, "<jclass @%p \"", self->cls);
  106. }
  107. print_jobject(print, self->cls);
  108. if (kind == PRINT_REPR) {
  109. mp_printf(print, "\">");
  110. }
  111. }
  112. STATIC void jclass_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
  113. if (dest[0] == MP_OBJ_NULL) {
  114. // load attribute
  115. mp_obj_jclass_t *self = self_in;
  116. const char *attr = qstr_str(attr_in);
  117. jstring field_name = JJ(NewStringUTF, attr);
  118. jobject field = JJ(CallObjectMethod, self->cls, Class_getField_mid, field_name);
  119. if (!JJ1(ExceptionCheck)) {
  120. jfieldID field_id = JJ(FromReflectedField, field);
  121. jobject obj = JJ(GetStaticObjectField, self->cls, field_id);
  122. dest[0] = new_jobject(obj);
  123. return;
  124. }
  125. //JJ1(ExceptionDescribe);
  126. JJ1(ExceptionClear);
  127. mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
  128. o->base.type = &jmethod_type;
  129. o->name = attr_in;
  130. o->meth = NULL;
  131. o->obj = self->cls;
  132. o->is_static = true;
  133. dest[0] = o;
  134. }
  135. }
  136. STATIC mp_obj_t jclass_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  137. if (n_kw != 0) {
  138. mp_raise_TypeError("kwargs not supported");
  139. }
  140. mp_obj_jclass_t *self = self_in;
  141. jarray methods = JJ(CallObjectMethod, self->cls, Class_getConstructors_mid);
  142. return call_method(self->cls, NULL, methods, true, n_args, args);
  143. }
  144. STATIC const mp_rom_map_elem_t jclass_locals_dict_table[] = {
  145. // { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&ffivar_get_obj) },
  146. // { MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&ffivar_set_obj) },
  147. };
  148. STATIC MP_DEFINE_CONST_DICT(jclass_locals_dict, jclass_locals_dict_table);
  149. STATIC const mp_obj_type_t jclass_type = {
  150. { &mp_type_type },
  151. .name = MP_QSTR_jclass,
  152. .print = jclass_print,
  153. .attr = jclass_attr,
  154. .call = jclass_call,
  155. .locals_dict = (mp_obj_dict_t*)&jclass_locals_dict,
  156. };
  157. STATIC mp_obj_t new_jclass(jclass jc) {
  158. mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
  159. o->base.type = &jclass_type;
  160. o->cls = jc;
  161. return o;
  162. }
  163. // jobject
  164. STATIC void jobject_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  165. mp_obj_jobject_t *self = self_in;
  166. if (kind == PRINT_REPR) {
  167. mp_printf(print, "<jobject @%p \"", self->obj);
  168. }
  169. print_jobject(print, self->obj);
  170. if (kind == PRINT_REPR) {
  171. mp_printf(print, "\">");
  172. }
  173. }
  174. STATIC void jobject_attr(mp_obj_t self_in, qstr attr_in, mp_obj_t *dest) {
  175. if (dest[0] == MP_OBJ_NULL) {
  176. // load attribute
  177. mp_obj_jobject_t *self = self_in;
  178. const char *attr = qstr_str(attr_in);
  179. jclass obj_class = JJ(GetObjectClass, self->obj);
  180. jstring field_name = JJ(NewStringUTF, attr);
  181. jobject field = JJ(CallObjectMethod, obj_class, Class_getField_mid, field_name);
  182. JJ(DeleteLocalRef, field_name);
  183. JJ(DeleteLocalRef, obj_class);
  184. if (!JJ1(ExceptionCheck)) {
  185. jfieldID field_id = JJ(FromReflectedField, field);
  186. JJ(DeleteLocalRef, field);
  187. jobject obj = JJ(GetObjectField, self->obj, field_id);
  188. dest[0] = new_jobject(obj);
  189. return;
  190. }
  191. //JJ1(ExceptionDescribe);
  192. JJ1(ExceptionClear);
  193. mp_obj_jmethod_t *o = m_new_obj(mp_obj_jmethod_t);
  194. o->base.type = &jmethod_type;
  195. o->name = attr_in;
  196. o->meth = NULL;
  197. o->obj = self->obj;
  198. o->is_static = false;
  199. dest[0] = o;
  200. }
  201. }
  202. STATIC void get_jclass_name(jobject obj, char *buf) {
  203. jclass obj_class = JJ(GetObjectClass, obj);
  204. jstring name = JJ(CallObjectMethod, obj_class, Class_getName_mid);
  205. jint len = JJ(GetStringLength, name);
  206. JJ(GetStringUTFRegion, name, 0, len, buf);
  207. check_exception();
  208. }
  209. STATIC mp_obj_t jobject_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
  210. mp_obj_jobject_t *self = self_in;
  211. mp_uint_t idx = mp_obj_get_int(index);
  212. char class_name[64];
  213. get_jclass_name(self->obj, class_name);
  214. //printf("class: %s\n", class_name);
  215. if (class_name[0] == '[') {
  216. if (class_name[1] == 'L' || class_name[1] == '[') {
  217. if (value == MP_OBJ_NULL) {
  218. // delete
  219. assert(0);
  220. } else if (value == MP_OBJ_SENTINEL) {
  221. // load
  222. jobject el = JJ(GetObjectArrayElement, self->obj, idx);
  223. return new_jobject(el);
  224. } else {
  225. // store
  226. jvalue jval;
  227. const char *t = class_name + 1;
  228. py2jvalue(&t, value, &jval);
  229. JJ(SetObjectArrayElement, self->obj, idx, jval.l);
  230. return mp_const_none;
  231. }
  232. }
  233. mp_raise_NotImplementedError(NULL);
  234. }
  235. if (!JJ(IsInstanceOf, self->obj, List_class)) {
  236. return MP_OBJ_NULL;
  237. }
  238. if (value == MP_OBJ_NULL) {
  239. // delete
  240. assert(0);
  241. } else if (value == MP_OBJ_SENTINEL) {
  242. // load
  243. jobject el = JJ(CallObjectMethod, self->obj, List_get_mid, idx);
  244. check_exception();
  245. return new_jobject(el);
  246. } else {
  247. // store
  248. assert(0);
  249. }
  250. return MP_OBJ_NULL;
  251. }
  252. STATIC mp_obj_t jobject_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
  253. mp_obj_jobject_t *self = self_in;
  254. switch (op) {
  255. case MP_UNARY_OP_BOOL:
  256. case MP_UNARY_OP_LEN: {
  257. jint len = JJ(CallIntMethod, self->obj, List_size_mid);
  258. if (op == MP_UNARY_OP_BOOL) {
  259. return mp_obj_new_bool(len != 0);
  260. }
  261. return MP_OBJ_NEW_SMALL_INT(len);
  262. }
  263. default:
  264. return MP_OBJ_NULL; // op not supported
  265. }
  266. }
  267. // TODO: subscr_load_adaptor & subscr_getiter convenience functions
  268. // should be moved to common location for reuse.
  269. STATIC mp_obj_t subscr_load_adaptor(mp_obj_t self_in, mp_obj_t index_in) {
  270. return mp_obj_subscr(self_in, index_in, MP_OBJ_SENTINEL);
  271. }
  272. MP_DEFINE_CONST_FUN_OBJ_2(subscr_load_adaptor_obj, subscr_load_adaptor);
  273. // .getiter special method which returns iterator which works in terms
  274. // of object subscription.
  275. STATIC mp_obj_t subscr_getiter(mp_obj_t self_in) {
  276. mp_obj_t dest[2] = {(mp_obj_t)&subscr_load_adaptor_obj, self_in};
  277. return mp_obj_new_getitem_iter(dest);
  278. }
  279. STATIC const mp_obj_type_t jobject_type = {
  280. { &mp_type_type },
  281. .name = MP_QSTR_jobject,
  282. .print = jobject_print,
  283. .unary_op = jobject_unary_op,
  284. .attr = jobject_attr,
  285. .subscr = jobject_subscr,
  286. .getiter = subscr_getiter,
  287. // .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict,
  288. };
  289. STATIC mp_obj_t new_jobject(jobject jo) {
  290. if (jo == NULL) {
  291. return mp_const_none;
  292. } else if (JJ(IsInstanceOf, jo, String_class)) {
  293. const char *s = JJ(GetStringUTFChars, jo, NULL);
  294. mp_obj_t ret = mp_obj_new_str(s, strlen(s));
  295. JJ(ReleaseStringUTFChars, jo, s);
  296. return ret;
  297. } else if (JJ(IsInstanceOf, jo, Class_class)) {
  298. return new_jclass(jo);
  299. } else {
  300. mp_obj_jobject_t *o = m_new_obj(mp_obj_jobject_t);
  301. o->base.type = &jobject_type;
  302. o->obj = jo;
  303. return o;
  304. }
  305. }
  306. // jmethod
  307. STATIC void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  308. (void)kind;
  309. mp_obj_jmethod_t *self = self_in;
  310. // Variable value printed as cast to int
  311. mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
  312. }
  313. #define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))
  314. #define CHECK_TYPE(java_type_name) \
  315. if (strncmp(arg_type, java_type_name, sizeof(java_type_name) - 1) != 0) { \
  316. return false; \
  317. } \
  318. arg_type += sizeof(java_type_name) - 1;
  319. STATIC const char *strprev(const char *s, char c) {
  320. while (*s != c) {
  321. s--;
  322. }
  323. return s;
  324. }
  325. STATIC bool py2jvalue(const char **jtypesig, mp_obj_t arg, jvalue *out) {
  326. const char *arg_type = *jtypesig;
  327. mp_obj_type_t *type = mp_obj_get_type(arg);
  328. if (type == &mp_type_str) {
  329. if (IMATCH(arg_type, "java.lang.String") || IMATCH(arg_type, "java.lang.Object")) {
  330. out->l = JJ(NewStringUTF, mp_obj_str_get_str(arg));
  331. } else {
  332. return false;
  333. }
  334. } else if (type == &mp_type_int) {
  335. if (IMATCH(arg_type, "int") || IMATCH(arg_type, "long")) {
  336. // TODO: Java long is 64-bit actually
  337. out->j = mp_obj_get_int(arg);
  338. } else {
  339. return false;
  340. }
  341. } else if (type == &jobject_type) {
  342. bool is_object = false;
  343. const char *expected_type = arg_type;
  344. while (1) {
  345. if (isalpha(*arg_type)) {
  346. } else if (*arg_type == '.') {
  347. is_object = true;
  348. } else {
  349. break;
  350. }
  351. arg_type++;
  352. }
  353. if (!is_object) {
  354. return false;
  355. }
  356. mp_obj_jobject_t *jo = arg;
  357. if (!MATCH(expected_type, "java.lang.Object")) {
  358. char class_name[64];
  359. get_jclass_name(jo->obj, class_name);
  360. //printf("Arg class: %s\n", class_name);
  361. if (strcmp(class_name, expected_type) != 0) {
  362. return false;
  363. }
  364. }
  365. out->l = jo->obj;
  366. } else if (type == &mp_type_bool) {
  367. if (IMATCH(arg_type, "boolean")) {
  368. out->z = arg == mp_const_true;
  369. } else {
  370. return false;
  371. }
  372. } else if (arg == mp_const_none) {
  373. //printf("TODO: Check java arg type!!\n");
  374. while (isalpha(*arg_type) || *arg_type == '.') {
  375. arg_type++;
  376. }
  377. out->l = NULL;
  378. } else {
  379. mp_raise_TypeError("arg type not supported");
  380. }
  381. *jtypesig = arg_type;
  382. return true;
  383. }
  384. #if 0
  385. // jvalue is known to be union of jobject and friends. And yet from C's
  386. // perspective, it's aggregate object which may require passing via stack
  387. // instead of registers. Work that around by passing jobject and typecasting
  388. // it.
  389. STATIC mp_obj_t jvalue2py(const char *jtypesig, jobject arg) {
  390. if (arg == NULL || MATCH(jtypesig, "void")) {
  391. return mp_const_none;
  392. } else if (MATCH(jtypesig, "boolean")) {
  393. return mp_obj_new_bool((bool)arg);
  394. } else if (MATCH(jtypesig, "int")) {
  395. return mp_obj_new_int((mp_int_t)arg);
  396. } else if (is_object_type(jtypesig)) {
  397. // Non-primitive, object type
  398. return new_jobject(arg);
  399. }
  400. printf("Unknown return type: %s\n", jtypesig);
  401. return MP_OBJ_NULL;
  402. }
  403. #endif
  404. STATIC mp_obj_t call_method(jobject obj, const char *name, jarray methods, bool is_constr, size_t n_args, const mp_obj_t *args) {
  405. jvalue jargs[n_args];
  406. // printf("methods=%p\n", methods);
  407. jsize num_methods = JJ(GetArrayLength, methods);
  408. for (int i = 0; i < num_methods; i++) {
  409. jobject meth = JJ(GetObjectArrayElement, methods, i);
  410. jobject name_o = JJ(CallObjectMethod, meth, Object_toString_mid);
  411. const char *decl = JJ(GetStringUTFChars, name_o, NULL);
  412. const char *arg_types = strchr(decl, '(') + 1;
  413. //const char *arg_types_end = strchr(arg_types, ')');
  414. // printf("method[%d]=%p %s\n", i, meth, decl);
  415. const char *meth_name = NULL;
  416. const char *ret_type = NULL;
  417. if (!is_constr) {
  418. meth_name = strprev(arg_types, '.') + 1;
  419. ret_type = strprev(meth_name, ' ') - 1;
  420. ret_type = strprev(ret_type, ' ') + 1;
  421. int name_len = strlen(name);
  422. if (strncmp(name, meth_name, name_len/*arg_types - meth_name - 1*/) || meth_name[name_len] != '('/*(*/) {
  423. goto next_method;
  424. }
  425. }
  426. // printf("method[%d]=%p %s\n", i, meth, decl);
  427. // printf("!!!%s\n", arg_types);
  428. // printf("name=%p meth_name=%s\n", name, meth_name);
  429. bool found = true;
  430. for (int i = 0; i < n_args && *arg_types != ')'; i++) {
  431. if (!py2jvalue(&arg_types, args[i], &jargs[i])) {
  432. goto next_method;
  433. }
  434. if (*arg_types == ',') {
  435. arg_types++;
  436. }
  437. }
  438. if (*arg_types != ')') {
  439. goto next_method;
  440. }
  441. if (found) {
  442. // printf("found!\n");
  443. jmethodID method_id = JJ(FromReflectedMethod, meth);
  444. jobject res;
  445. mp_obj_t ret;
  446. if (is_constr) {
  447. JJ(ReleaseStringUTFChars, name_o, decl);
  448. res = JJ(NewObjectA, obj, method_id, jargs);
  449. return new_jobject(res);
  450. } else {
  451. if (MATCH(ret_type, "void")) {
  452. JJ(CallVoidMethodA, obj, method_id, jargs);
  453. check_exception();
  454. ret = mp_const_none;
  455. } else if (MATCH(ret_type, "int")) {
  456. jint res = JJ(CallIntMethodA, obj, method_id, jargs);
  457. check_exception();
  458. ret = mp_obj_new_int(res);
  459. } else if (MATCH(ret_type, "boolean")) {
  460. jboolean res = JJ(CallBooleanMethodA, obj, method_id, jargs);
  461. check_exception();
  462. ret = mp_obj_new_bool(res);
  463. } else if (is_object_type(ret_type)) {
  464. res = JJ(CallObjectMethodA, obj, method_id, jargs);
  465. check_exception();
  466. ret = new_jobject(res);
  467. } else {
  468. JJ(ReleaseStringUTFChars, name_o, decl);
  469. mp_raise_TypeError("cannot handle return type");
  470. }
  471. JJ(ReleaseStringUTFChars, name_o, decl);
  472. JJ(DeleteLocalRef, name_o);
  473. JJ(DeleteLocalRef, meth);
  474. return ret;
  475. }
  476. }
  477. next_method:
  478. JJ(ReleaseStringUTFChars, name_o, decl);
  479. JJ(DeleteLocalRef, name_o);
  480. JJ(DeleteLocalRef, meth);
  481. }
  482. mp_raise_TypeError("method not found");
  483. }
  484. STATIC mp_obj_t jmethod_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  485. if (n_kw != 0) {
  486. mp_raise_TypeError("kwargs not supported");
  487. }
  488. mp_obj_jmethod_t *self = self_in;
  489. const char *name = qstr_str(self->name);
  490. // jstring meth_name = JJ(NewStringUTF, name);
  491. jclass obj_class = self->obj;
  492. if (!self->is_static) {
  493. obj_class = JJ(GetObjectClass, self->obj);
  494. }
  495. jarray methods = JJ(CallObjectMethod, obj_class, Class_getMethods_mid);
  496. return call_method(self->obj, name, methods, false, n_args, args);
  497. }
  498. STATIC const mp_obj_type_t jmethod_type = {
  499. { &mp_type_type },
  500. .name = MP_QSTR_jmethod,
  501. .print = jmethod_print,
  502. .call = jmethod_call,
  503. // .attr = jobject_attr,
  504. // .locals_dict = (mp_obj_dict_t*)&jobject_locals_dict,
  505. };
  506. #ifdef __ANDROID__
  507. #define LIBJVM_SO "libdvm.so"
  508. #else
  509. #define LIBJVM_SO "libjvm.so"
  510. #endif
  511. STATIC void create_jvm() {
  512. JavaVMInitArgs args;
  513. JavaVMOption options;
  514. options.optionString = "-Djava.class.path=.";
  515. args.version = JNI_VERSION_1_6;
  516. args.nOptions = 1;
  517. args.options = &options;
  518. args.ignoreUnrecognized = 0;
  519. if (env) {
  520. return;
  521. }
  522. void *libjvm = dlopen(LIBJVM_SO, RTLD_NOW | RTLD_GLOBAL);
  523. if (!libjvm) {
  524. mp_raise_msg(&mp_type_OSError, "unable to load libjvm.so, use LD_LIBRARY_PATH");
  525. }
  526. int (*_JNI_CreateJavaVM)(void*, void**, void*) = dlsym(libjvm, "JNI_CreateJavaVM");
  527. int st = _JNI_CreateJavaVM(&jvm, (void**)&env, &args);
  528. if (st < 0 || !env) {
  529. mp_raise_msg(&mp_type_OSError, "unable to create JVM");
  530. }
  531. Class_class = JJ(FindClass, "java/lang/Class");
  532. jclass method_class = JJ(FindClass, "java/lang/reflect/Method");
  533. String_class = JJ(FindClass, "java/lang/String");
  534. jclass Object_class = JJ(FindClass, "java/lang/Object");
  535. Object_toString_mid = JJ(GetMethodID, Object_class, "toString",
  536. "()Ljava/lang/String;");
  537. Class_getName_mid = (*env)->GetMethodID(env, Class_class, "getName",
  538. "()Ljava/lang/String;");
  539. Class_getField_mid = (*env)->GetMethodID(env, Class_class, "getField",
  540. "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
  541. Class_getMethods_mid = (*env)->GetMethodID(env, Class_class, "getMethods",
  542. "()[Ljava/lang/reflect/Method;");
  543. Class_getConstructors_mid = (*env)->GetMethodID(env, Class_class, "getConstructors",
  544. "()[Ljava/lang/reflect/Constructor;");
  545. Method_getName_mid = (*env)->GetMethodID(env, method_class, "getName",
  546. "()Ljava/lang/String;");
  547. List_class = JJ(FindClass, "java/util/List");
  548. List_get_mid = JJ(GetMethodID, List_class, "get",
  549. "(I)Ljava/lang/Object;");
  550. List_set_mid = JJ(GetMethodID, List_class, "set",
  551. "(ILjava/lang/Object;)Ljava/lang/Object;");
  552. List_size_mid = JJ(GetMethodID, List_class, "size",
  553. "()I");
  554. IndexException_class = JJ(FindClass, "java/lang/IndexOutOfBoundsException");
  555. }
  556. STATIC mp_obj_t mod_jni_cls(mp_obj_t cls_name_in) {
  557. const char *cls_name = mp_obj_str_get_str(cls_name_in);
  558. if (!env) {
  559. create_jvm();
  560. }
  561. jclass cls = JJ(FindClass, cls_name);
  562. mp_obj_jclass_t *o = m_new_obj(mp_obj_jclass_t);
  563. o->base.type = &jclass_type;
  564. o->cls = cls;
  565. return o;
  566. }
  567. MP_DEFINE_CONST_FUN_OBJ_1(mod_jni_cls_obj, mod_jni_cls);
  568. STATIC mp_obj_t mod_jni_array(mp_obj_t type_in, mp_obj_t size_in) {
  569. if (!env) {
  570. create_jvm();
  571. }
  572. mp_int_t size = mp_obj_get_int(size_in);
  573. jobject res = NULL;
  574. if (MP_OBJ_IS_TYPE(type_in, &jclass_type)) {
  575. mp_obj_jclass_t *jcls = type_in;
  576. res = JJ(NewObjectArray, size, jcls->cls, NULL);
  577. } else if (MP_OBJ_IS_STR(type_in)) {
  578. const char *type = mp_obj_str_get_str(type_in);
  579. switch (*type) {
  580. case 'Z':
  581. res = JJ(NewBooleanArray, size);
  582. break;
  583. case 'B':
  584. res = JJ(NewByteArray, size);
  585. break;
  586. case 'C':
  587. res = JJ(NewCharArray, size);
  588. break;
  589. case 'S':
  590. res = JJ(NewShortArray, size);
  591. break;
  592. case 'I':
  593. res = JJ(NewIntArray, size);
  594. break;
  595. case 'J':
  596. res = JJ(NewLongArray, size);
  597. break;
  598. case 'F':
  599. res = JJ(NewFloatArray, size);
  600. break;
  601. case 'D':
  602. res = JJ(NewDoubleArray, size);
  603. break;
  604. }
  605. }
  606. return new_jobject(res);
  607. }
  608. MP_DEFINE_CONST_FUN_OBJ_2(mod_jni_array_obj, mod_jni_array);
  609. STATIC mp_obj_t mod_jni_env() {
  610. return mp_obj_new_int((mp_int_t)env);
  611. }
  612. MP_DEFINE_CONST_FUN_OBJ_0(mod_jni_env_obj, mod_jni_env);
  613. STATIC const mp_rom_map_elem_t mp_module_jni_globals_table[] = {
  614. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_jni) },
  615. { MP_ROM_QSTR(MP_QSTR_cls), MP_ROM_PTR(&mod_jni_cls_obj) },
  616. { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mod_jni_array_obj) },
  617. { MP_ROM_QSTR(MP_QSTR_env), MP_ROM_PTR(&mod_jni_env_obj) },
  618. };
  619. STATIC MP_DEFINE_CONST_DICT(mp_module_jni_globals, mp_module_jni_globals_table);
  620. const mp_obj_module_t mp_module_jni = {
  621. .base = { &mp_type_module },
  622. .globals = (mp_obj_dict_t*)&mp_module_jni_globals,
  623. };