| 565 | #define JAVA_INSTANCE_MT "JavaInstance" |
| 566 | |
| 567 | static int lua_java_register_class(lua_State* L) { |
| 568 | luaL_checktype(L, 1, LUA_TTABLE); |
| 569 | |
| 570 | JNIEnv* env = get_jni_env(); |
| 571 | if (!env) { |
| 572 | lua_pushnil(L); |
| 573 | lua_pushstring(L, "Failed to get JNIEnv"); |
| 574 | return 2; |
| 575 | } |
| 576 | |
| 577 | if (init_bridge_dex(env) != 0) { |
| 578 | lua_pushnil(L); |
| 579 | lua_pushstring(L, "Failed to load bridge DEX"); |
| 580 | return 2; |
| 581 | } |
| 582 | |
| 583 | lua_getfield(L, 1, "implements"); |
| 584 | if (!lua_istable(L, -1)) { |
| 585 | lua_pushnil(L); |
| 586 | lua_pushstring(L, "implements must be a table"); |
| 587 | return 2; |
| 588 | } |
| 589 | |
| 590 | int iface_count = luaL_len(L, -1); |
| 591 | jclass iface_classes[16]; |
| 592 | for (int i = 0; i < iface_count && i < 16; i++) { |
| 593 | lua_rawgeti(L, -1, i + 1); |
| 594 | const char* iface_name = lua_tostring(L, -1); |
| 595 | iface_classes[i] = find_class(env, iface_name); |
| 596 | if (!iface_classes[i]) { |
| 597 | lua_pushnil(L); |
| 598 | lua_pushfstring(L, "Interface not found: %s", iface_name); |
| 599 | return 2; |
| 600 | } |
| 601 | lua_pop(L, 1); |
| 602 | } |
| 603 | lua_pop(L, 1); |
| 604 | |
| 605 | CallbackRegistry* reg = (CallbackRegistry*)malloc(sizeof(CallbackRegistry)); |
| 606 | memset(reg, 0, sizeof(CallbackRegistry)); |
| 607 | reg->L = L; |
| 608 | |
| 609 | lua_getfield(L, 1, "methods"); |
| 610 | if (lua_istable(L, -1)) { |
| 611 | lua_pushnil(L); |
| 612 | while (lua_next(L, -2) != 0) { |
| 613 | if (lua_isstring(L, -2) && lua_isfunction(L, -1)) { |
| 614 | const char* mname = lua_tostring(L, -2); |
| 615 | int ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 616 | strncpy(reg->methods[reg->method_count].method_name, mname, 63); |
| 617 | reg->methods[reg->method_count].lua_ref = ref; |
| 618 | reg->method_count++; |
| 619 | } else { |
| 620 | lua_pop(L, 1); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | lua_pop(L, 1); |
nothing calls this directly
no test coverage detected