From e176b944a58428ae6a466ff0fe68e49d35d64ac5 Mon Sep 17 00:00:00 2001 From: qiuguohua Date: Thu, 19 Dec 2024 14:13:15 +0800 Subject: [PATCH] Fix HarmonyOS Next platform interface for calling arkts using JavaScript --- cocos/native-binding/impl.ts | 2 + cocos/native-binding/index.ts | 11 +++++ .../bindings/jswrapper/jsvm/CommonHeader.h | 40 +++++++++---------- .../bindings/manual/JavaScriptArkTsBridge.cpp | 6 +-- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/cocos/native-binding/impl.ts b/cocos/native-binding/impl.ts index c8f6b639584..71f72d7652f 100644 --- a/cocos/native-binding/impl.ts +++ b/cocos/native-binding/impl.ts @@ -35,6 +35,8 @@ if (NATIVE) { globalJsb.__bridge = new globalThis.JavascriptJavaBridge(); } else if (globalThis.JavaScriptObjCBridge && (sys.os === sys.OS.IOS || sys.os === sys.OS.OSX)) { globalJsb.__bridge = new globalThis.JavaScriptObjCBridge(); + } else if (globalThis.JavaScriptArkTsBridge && (sys.os === sys.OS.OPENHARMONY)) { + globalJsb.__bridge = new globalThis.JavaScriptArkTsBridge(); } else { globalJsb.__bridge = null; } diff --git a/cocos/native-binding/index.ts b/cocos/native-binding/index.ts index ec3b93cfc11..80edf985c68 100644 --- a/cocos/native-binding/index.ts +++ b/cocos/native-binding/index.ts @@ -1268,6 +1268,17 @@ export declare namespace native { * @param parameters : @en the parameters of the Objective-C/Java class to translate @zh 传递至该 Objective-C/Java 方法的参数 */ export function callStaticMethod(className: string, methodName: string, methodSignature: string, ...parameters: any): any; + + /** + * @en call ArkTs static methods. Only valid on HarmonyOS Next platform. + * @zh 调用 ArkTs 静态方法。仅在HarmonyOS Next平台上有效。 + * + * @param isSyn : @en Call synchronous or asynchronous method @zh 调用同步或异步方法 + * @param className : @en the class name of the ArkTs class @zh ArkTs 类的类名 + * @param methodName : @en the method name of the ArkTs class @zh ArkTs 类的方法名 + * @param parame : @en the parameters of the ArkTs class to translate. Use json for multiple parameters @zh 传递至该 ArkTs 方法的参数。使用JSON可以传递多个参数 + */ + export function callStaticMethod(isSyn: boolean, className: string, methodName: string, parame: string): any; } /** diff --git a/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h b/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h index 9f6e5bf0590..a4905147394 100644 --- a/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h +++ b/native/cocos/bindings/jswrapper/jsvm/CommonHeader.h @@ -48,26 +48,26 @@ } while (0) // Returns nullptr if the_call doesn't return JSVM_OK. -#define NODE_API_CALL(status, env, the_call) \ - status = the_call; \ - if (status != JSVM_OK) { \ - bool isPending = false; \ - if (JSVM_OK == OH_JSVM_IsExceptionPending((env), &isPending) && isPending) { \ - JSVM_Value error; \ - if (JSVM_OK == OH_JSVM_GetAndClearLastException((env), &error)) { \ - JSVM_Value stack; \ - OH_JSVM_GetNamedProperty((env), error, "stack", &stack); \ - JSVM_Value message; \ - OH_JSVM_GetNamedProperty((env), error, "message", &message); \ - char stackstr[256]; \ - OH_JSVM_GetValueStringUtf8(env, stack, stackstr, 256, nullptr); \ - SE_LOGE("JSVM error stack: %{public}s", stackstr); \ - char messagestr[256]; \ - OH_JSVM_GetValueStringUtf8(env, message, messagestr, 256, nullptr); \ - SE_LOGE("JSVM error message: %{public}s", messagestr); \ - } \ - } \ - } \ +#define NODE_API_CALL(status, env, the_call) \ + status = the_call; \ + if (status != JSVM_OK) { \ + bool isPending = false; \ + if (JSVM_OK == OH_JSVM_IsExceptionPending((env), &isPending) && isPending) { \ + JSVM_Value error; \ + if (JSVM_OK == OH_JSVM_GetAndClearLastException((env), &error)) { \ + JSVM_Value stack; \ + OH_JSVM_GetNamedProperty((env), error, "stack", &stack); \ + JSVM_Value message; \ + OH_JSVM_GetNamedProperty((env), error, "message", &message); \ + char stackstr[256]; \ + OH_JSVM_GetValueStringUtf8(env, stack, stackstr, 256, nullptr); \ + SE_LOGE("JSVM error stack: %s", stackstr); \ + char messagestr[256]; \ + OH_JSVM_GetValueStringUtf8(env, message, messagestr, 256, nullptr); \ + SE_LOGE("JSVM error message: %s", messagestr); \ + } \ + } \ + } \ NODE_API_CALL_BASE(env, status, nullptr) // Returns empty if the_call doesn't return JSVM_OK. diff --git a/native/cocos/bindings/manual/JavaScriptArkTsBridge.cpp b/native/cocos/bindings/manual/JavaScriptArkTsBridge.cpp index 8d8e253afbd..f2a60eb1630 100644 --- a/native/cocos/bindings/manual/JavaScriptArkTsBridge.cpp +++ b/native/cocos/bindings/manual/JavaScriptArkTsBridge.cpp @@ -207,9 +207,9 @@ static bool JavaScriptArkTsBridge_callStaticMethod(se::State& s) { bool ok = false; bool isSyn; std::string clsPath, methodName, paramStr; - - isSyn = seval_to_type(args[0], ok); - SE_PRECONDITION2(ok, false, "Converting isSyn failed!"); + ok = args[0].isBoolean(); + SE_PRECONDITION2(ok, false, "isSyn must be boolean type"); + isSyn = args[0].toBoolean(); clsPath = seval_to_type(args[1], ok); SE_PRECONDITION2(ok, false, "Converting clsPath failed!");