| 243 | |
| 244 | |
| 245 | static jobject JNICALL native_invoke_callback(JNIEnv* env, jclass clazz, |
| 246 | jlong callbackPtr, jstring methodName, jstring returnType, jobjectArray args) |
| 247 | { |
| 248 | CallbackRegistry* reg = (CallbackRegistry*)(uintptr_t)callbackPtr; |
| 249 | if (!reg || !reg->L) return NULL; |
| 250 | |
| 251 | const char* name = (*env)->GetStringUTFChars(env, methodName, NULL); |
| 252 | if (!name) return NULL; |
| 253 | |
| 254 | int lua_ref = LUA_NOREF; |
| 255 | for (int i = 0; i < reg->method_count; i++) { |
| 256 | if (strcmp(reg->methods[i].method_name, name) == 0) { |
| 257 | lua_ref = reg->methods[i].lua_ref; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (lua_ref == LUA_NOREF) { |
| 263 | (*env)->ReleaseStringUTFChars(env, methodName, name); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | lua_State* L = reg->L; |
| 268 | lua_rawgeti(L, LUA_REGISTRYINDEX, lua_ref); |
| 269 | |
| 270 | int arg_count = 0; |
| 271 | if (args) { |
| 272 | arg_count = (*env)->GetArrayLength(env, args); |
| 273 | for (int i = 0; i < arg_count; i++) { |
| 274 | jobject arg = (*env)->GetObjectArrayElement(env, args, i); |
| 275 | if (!arg) { |
| 276 | lua_pushnil(L); |
| 277 | } else { |
| 278 | jclass string_class = (*env)->FindClass(env, "java/lang/String"); |
| 279 | if ((*env)->IsInstanceOf(env, arg, string_class)) { |
| 280 | const char* str = (*env)->GetStringUTFChars(env, (jstring)arg, NULL); |
| 281 | lua_pushstring(L, str); |
| 282 | (*env)->ReleaseStringUTFChars(env, (jstring)arg, str); |
| 283 | } else { |
| 284 | lua_pushinteger(L, (lua_Integer)(uintptr_t)arg); |
| 285 | } |
| 286 | (*env)->DeleteLocalRef(env, arg); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | jobject result = NULL; |
| 292 | if (lua_pcall(L, arg_count, 1, 0) != LUA_OK) { |
| 293 | LOGE("registerClass callback error [%s]: %s", name, lua_tostring(L, -1)); |
| 294 | lua_pop(L, 1); |
| 295 | } else { |
| 296 | const char* ret_type = (*env)->GetStringUTFChars(env, returnType, NULL); |
| 297 | |
| 298 | if (ret_type && strcmp(ret_type, "boolean") == 0) { |
| 299 | jclass bool_class = (*env)->FindClass(env, "java/lang/Boolean"); |
| 300 | jmethodID value_of = (*env)->GetStaticMethodID(env, bool_class, "valueOf", "(Z)Ljava/lang/Boolean;"); |
| 301 | jboolean val = lua_toboolean(L, -1); |
| 302 | result = (*env)->CallStaticObjectMethod(env, bool_class, value_of, val); |
nothing calls this directly
no outgoing calls
no test coverage detected