Skip to content

Commit 40e0769

Browse files
qiuguohuaqiuguohua
andauthored
Fix HarmonyOS Next platform interface for calling arkts using JavaScript (#18062)
* Fix HarmonyOS Next platform interface for calling arkts using JavaScript * Optimized interface implementation * Improve annotations * Improve the annotation. --------- Co-authored-by: qiuguohua <guohua.qiu@cocos.com>
1 parent eb949c6 commit 40e0769

4 files changed

Lines changed: 59 additions & 33 deletions

File tree

cocos/native-binding/impl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ if (NATIVE) {
3535
globalJsb.__bridge = new globalThis.JavascriptJavaBridge();
3636
} else if (globalThis.JavaScriptObjCBridge && (sys.os === sys.OS.IOS || sys.os === sys.OS.OSX)) {
3737
globalJsb.__bridge = new globalThis.JavaScriptObjCBridge();
38+
} else if (globalThis.JavaScriptArkTsBridge && (sys.os === sys.OS.OPENHARMONY)) {
39+
globalJsb.__bridge = new globalThis.JavaScriptArkTsBridge();
3840
} else {
3941
globalJsb.__bridge = null;
4042
}

cocos/native-binding/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,10 +1262,31 @@ export declare namespace native {
12621262
* @en call Objective-C/Java static methods
12631263
* @zh 调用 Objective-C/Java 静态方法
12641264
*
1265-
* @param className : @en the class name of the Objective-C/Java class @zh Objective-C/Java 类的类名
1266-
* @param methodName : @en the method name of the Objective-C/Java class @zh Objective-C/Java 类的方法名
1267-
* @param methodSignature : @en the method signature of the Objective-C/Java class @zh Objective-C/Java 方法签名
1268-
* @param parameters : @en the parameters of the Objective-C/Java class to translate @zh 传递至该 Objective-C/Java 方法的参数
1265+
* @param className : @en the class name of the Objective-C/Java/ArkTs class @zh Objective-C/Java/ArkTs 类的类名
1266+
* @param methodName : @en the method name of the Objective-C/Java/ArkT class @zh Objective-C/Java/ArkT 类的方法名
1267+
* @param methodSignature :
1268+
* @en
1269+
* - On the Android platform: Represents the method signature of the Java class.
1270+
* - On the Mac/iOS platform: Represents the first parameter of the Objective-C method.
1271+
* - On the HarmonyOS Next platform: Represents all parameters of the ArkTs method.
1272+
* If multiple parameters are required, can be converted into a JSON string.
1273+
* @zh
1274+
* - 在 Android 平台:表示 Java 方法的签名。
1275+
* - 在 Mac 或 iOS 平台:表示 Objective-C 方法的第一个参数。
1276+
* - 在 HarmonyOS Next 平台:表示 ArkTs 方法的所有参数。如果需要传入多个参数,可以将它们转换为 JSON 字符串。
1277+
*
1278+
* @param parameters :
1279+
* @en
1280+
* - On the Android platform: Represents the parameters passed to the Java method.
1281+
* - On the Mac/iOS platform: Represents the second or subsequent parameters of the Objective-C method.
1282+
* - On the HarmonyOS Next platform: Indicates whether the ArkTs method is synchronous or asynchronous.
1283+
* The default is `true`, meaning the ArkTs method is called synchronously.
1284+
* Note that calling asynchronous ArkTs methods may block while waiting for results.
1285+
* @zh
1286+
* - 在 Android 平台:传递到 Java 方法的参数。
1287+
* - 在 Mac 或 iOS 平台:传递到 Objective-C 方法的第二个或更多参数。
1288+
* - 在 HarmonyOS Next 平台:表示调用 ArkTs 方法是同步还是异步。
1289+
* 默认值为 `true`,表示调用ArkTs的同步方法。注意,调用异步方法可能会阻塞以等待结果。
12691290
*/
12701291
export function callStaticMethod(className: string, methodName: string, methodSignature: string, ...parameters: any): any;
12711292
}

native/cocos/bindings/jswrapper/jsvm/CommonHeader.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,26 @@
4848
} while (0)
4949

5050
// Returns nullptr if the_call doesn't return JSVM_OK.
51-
#define NODE_API_CALL(status, env, the_call) \
52-
status = the_call; \
53-
if (status != JSVM_OK) { \
54-
bool isPending = false; \
55-
if (JSVM_OK == OH_JSVM_IsExceptionPending((env), &isPending) && isPending) { \
56-
JSVM_Value error; \
57-
if (JSVM_OK == OH_JSVM_GetAndClearLastException((env), &error)) { \
58-
JSVM_Value stack; \
59-
OH_JSVM_GetNamedProperty((env), error, "stack", &stack); \
60-
JSVM_Value message; \
61-
OH_JSVM_GetNamedProperty((env), error, "message", &message); \
62-
char stackstr[256]; \
63-
OH_JSVM_GetValueStringUtf8(env, stack, stackstr, 256, nullptr); \
64-
SE_LOGE("JSVM error stack: %{public}s", stackstr); \
65-
char messagestr[256]; \
66-
OH_JSVM_GetValueStringUtf8(env, message, messagestr, 256, nullptr); \
67-
SE_LOGE("JSVM error message: %{public}s", messagestr); \
68-
} \
69-
} \
70-
} \
51+
#define NODE_API_CALL(status, env, the_call) \
52+
status = the_call; \
53+
if (status != JSVM_OK) { \
54+
bool isPending = false; \
55+
if (JSVM_OK == OH_JSVM_IsExceptionPending((env), &isPending) && isPending) { \
56+
JSVM_Value error; \
57+
if (JSVM_OK == OH_JSVM_GetAndClearLastException((env), &error)) { \
58+
JSVM_Value stack; \
59+
OH_JSVM_GetNamedProperty((env), error, "stack", &stack); \
60+
JSVM_Value message; \
61+
OH_JSVM_GetNamedProperty((env), error, "message", &message); \
62+
char stackstr[256]; \
63+
OH_JSVM_GetValueStringUtf8(env, stack, stackstr, 256, nullptr); \
64+
SE_LOGE("JSVM error stack: %s", stackstr); \
65+
char messagestr[256]; \
66+
OH_JSVM_GetValueStringUtf8(env, message, messagestr, 256, nullptr); \
67+
SE_LOGE("JSVM error message: %s", messagestr); \
68+
} \
69+
} \
70+
} \
7171
NODE_API_CALL_BASE(env, status, nullptr)
7272

7373
// Returns empty if the_call doesn't return JSVM_OK.

native/cocos/bindings/manual/JavaScriptArkTsBridge.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,27 @@ static bool JavaScriptArkTsBridge_callStaticMethod(se::State& s) {
203203
const auto& args = s.args();
204204
int argc = (int)args.size();
205205

206-
if (argc == 4) {
206+
if (argc == 3 || argc == 4) {
207207
bool ok = false;
208-
bool isSyn;
208+
bool isSync = true;
209209
std::string clsPath, methodName, paramStr;
210210

211-
isSyn = seval_to_type<bool>(args[0], ok);
212-
SE_PRECONDITION2(ok, false, "Converting isSyn failed!");
213-
214-
clsPath = seval_to_type<std::string>(args[1], ok);
211+
clsPath = seval_to_type<std::string>(args[0], ok);
215212
SE_PRECONDITION2(ok, false, "Converting clsPath failed!");
216213

217-
methodName = seval_to_type<std::string>(args[2], ok);
214+
methodName = seval_to_type<std::string>(args[1], ok);
218215
SE_PRECONDITION2(ok, false, "Converting methodName failed!");
219216

220-
paramStr = seval_to_type<std::string>(args[3], ok);
217+
paramStr = seval_to_type<std::string>(args[2], ok);
221218
SE_PRECONDITION2(ok, false, "Converting paramStr failed!");
219+
220+
if(argc == 4) {
221+
ok = args[3].isBoolean();
222+
SE_PRECONDITION2(ok, false, "isSync must be boolean type");
223+
isSync = args[3].toBoolean();
224+
}
222225

223-
JavaScriptArkTsBridge::CallInfo call(isSyn, clsPath.c_str(), methodName.c_str(), paramStr.c_str());
226+
JavaScriptArkTsBridge::CallInfo call(isSync, clsPath.c_str(), methodName.c_str(), paramStr.c_str());
224227
ok = call.execute(s.rval());
225228
if (!ok) {
226229
s.rval().setUndefined();

0 commit comments

Comments
 (0)