@@ -384,66 +384,58 @@ Java_com_quickjs_android_QuickJS__1getKeys(JNIEnv *env, jclass clazz, jlong cont
384384 }
385385 return  stringArray;
386386}
387- extern  " C" 
388- JNIEXPORT jobject JNICALL
389- Java_com_quickjs_android_QuickJS__1executeFunction2 (JNIEnv *env, jclass clazz, jlong context_ptr,
390-  jint expected_type, jobject object_handle,
391-  jobject functionHandle,
392-  jobject parameters_handle) {
387+ 
388+ JSValue executeFunction (JNIEnv *env, jlong context_ptr, jobject object_handle, JSValue func_obj,
389+  jobject parameters_handle) {
393390 auto  *ctx = reinterpret_cast <JSContext *>(context_ptr);
394391 JSValue this_obj = TO_JS_VALUE (env, object_handle);
395-  JSValue func_obj =  TO_JS_VALUE (env, functionHandle); 
392+ 
396393 JSValue *argv = nullptr ;
397394 int  argc = 0 ;
398395 if  (parameters_handle != nullptr ) {
399396 JSValue argArray = TO_JS_VALUE (env, parameters_handle);
400397 argc = JS_VALUE_GET_INT (JS_GetPropertyStr (ctx, argArray, " length"  ));
401398 argv = new  JSValue[argc];
402399 for  (int  i = 0 ; i < argc; ++i) {
403-  argv[i] = JS_GetPropertyUint32 (ctx, argArray, i);
400+  argv[i] = JS_DupValue (ctx,  JS_GetPropertyUint32 (ctx, argArray, i) );
404401 }
405402 }
406-  JSValue result = JS_Call (ctx, func_obj, this_obj, argc, argv);
407-  delete  argv;
408-  return  To_JObject (env, context_ptr, expected_type, result);
409- }
410- 
411- JSValue executeJSFunction (JNIEnv *env,
412-  jlong context_ptr, jobject object_handle,
413-  jstring name, jobject parameters_handle) {
414-  const  char  *name_ = env->GetStringUTFChars (name, nullptr );
415-  auto  *ctx = reinterpret_cast <JSContext *>(context_ptr);
416-  JSValue this_obj = TO_JS_VALUE (env, object_handle);
417-  JSValue func_obj = JS_GetPropertyStr (ctx, this_obj, name_);
418-  JSValue *argv = nullptr ;
419-  int  argc = 0 ;
420-  if  (parameters_handle != nullptr ) {
421-  JSValue argArray = TO_JS_VALUE (env, parameters_handle);
422-  argc = JS_VALUE_GET_INT (JS_GetPropertyStr (ctx, argArray, " length"  ));
423-  argv = new  JSValue[argc];
403+  JSValue result = JS_Call (ctx, func_obj, JS_DupValue (ctx, this_obj), argc, argv);
404+  if  (argv != nullptr ) {
424405 for  (int  i = 0 ; i < argc; ++i) {
425-  argv[i] =  JS_GetPropertyUint32 (ctx, argArray, i );
406+ //  JS_FreeValue (ctx, argv[i] );
426407 }
427408 }
428-  JSValue result = JS_Call (ctx, func_obj, this_obj, argc, argv);
429-  for  (int  i = 0 ; i < argc; ++i) {
430-  JS_FreeValue (ctx, argv[i]);
431-  }
432-  delete  argv;
409+  JS_FreeValue (ctx, func_obj);
433410 JS_FreeValue (ctx, func_obj);
434411 return  result;
435412}
436413
414+ 
415+ extern  " C" 
416+ JNIEXPORT jobject JNICALL
417+ Java_com_quickjs_android_QuickJS__1executeFunction2 (JNIEnv *env, jclass clazz, jlong context_ptr,
418+  jint expected_type, jobject object_handle,
419+  jobject functionHandle,
420+  jobject parameters_handle) {
421+  JSValue func_obj = TO_JS_VALUE (env, functionHandle);
422+  JSValue value = executeFunction (env, context_ptr, object_handle, func_obj, parameters_handle);
423+  jobject result = To_JObject (env, context_ptr, expected_type, value);
424+  return  result;
425+ }
426+ 
427+ 
437428extern  " C" 
438429JNIEXPORT jobject JNICALL
439430Java_com_quickjs_android_QuickJS__1executeFunction (JNIEnv *env, jclass clazz, jlong context_ptr,
440431 jint expected_type, jobject object_handle,
441432 jstring name, jobject parameters_handle) {
442433 auto  *ctx = reinterpret_cast <JSContext *>(context_ptr);
443-  JSValue result = executeJSFunction (env, context_ptr, object_handle, name, parameters_handle);
444-  jobject jResult = To_JObject (env, context_ptr, expected_type, result);
445-  JS_FreeValue (ctx, result);
446-  return  jResult;
434+  JSValue this_obj = TO_JS_VALUE (env, object_handle);
435+  JSValue func_obj = JS_GetPropertyStr (ctx, this_obj, env->GetStringUTFChars (name, nullptr ));
436+  JSValue value = executeFunction (env, context_ptr, object_handle, func_obj, parameters_handle);
437+  jobject result = To_JObject (env, context_ptr, expected_type, value);
438+  return  result;
447439}
448440
449441JSValue
@@ -515,8 +507,8 @@ Java_com_quickjs_android_QuickJS__1initNewJSFunction(JNIEnv *env,
515507 JSCFunctionData *functionData = void_method ? callJavaVoidCallback : callJavaCallback;
516508 JSValue func_data = JS_NewArray (ctx);
517509 JSValue func = JS_NewCFunctionData (ctx, functionData, 1 , 0 , 1 , &func_data);
518-  JS_SetPropertyUint32 (ctx, func_data, 0 , func);
519- //  JS_FreeValue(ctx, func_data);
510+  JS_SetPropertyUint32 (ctx, func_data, 0 , JS_DupValue (ctx,  func) );
511+  JS_FreeValue (ctx, func_data);
520512 return  TO_JAVA_OBJECT (env, ctx, func);
521513}
522514extern  " C" 
0 commit comments