diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/build-profile.json5 index 92fc4e620d45307ff4ee2a47e284f1ce6499087b..95fd23914da179cd6c299c588154373b8cc0e732 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/ohosTest.md b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/ohosTest.md index 352aa27ee1ab0f1160acc8cfa5f9c6ca7a11c225..fb9d10553deca53b4025c956baa8e719c6a1673d 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/ohosTest.md +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/ohosTest.md @@ -5,7 +5,6 @@ | 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | | --------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | | 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | -| 在指定的主机和端口上激活inspector,将用来调试JS代码 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | | 获取当前堆快照并将其输出到流 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | | 使用 code cache 加速编译 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | | JSVM-API接口创建多个引擎执行JS代码并销毁 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets index 4b4a8880b44143af8d79d5845427a867745821bc..9d4f8a434d863461a900661b39dc7ac9808997e2 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets @@ -13,40 +13,38 @@ * limitations under the License. */ -import { describe, it, expect } from '@ohos/hypium'; -import { abilityDelegatorRegistry, Driver, ON } from '@kit.TestKit'; -import { UIAbility, Want } from '@kit.AbilityKit'; - -const delegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); -const bundleName = abilityDelegatorRegistry.getArguments().bundleName; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; export default function abilityTest() { describe('ActsAbilityTest', () => { - /** - * 打开应用,点击Hello World执行 - */ - it('testOpenInspector', 0, async (done: Function) => { - console.log('uitest: TestOpenInspector begin'); - - const want: Want = { - bundleName: bundleName, - abilityName: 'EntryAbility' - } - const driver = Driver.create(); - await delegator.startAbility(want); - await driver.delayMs(1000); - - const ability: UIAbility = await delegator.getCurrentTopAbility(); - console.log('get top ability'); - expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); - - await driver.delayMs(1000); - const text = await driver.findComponent(ON.text('Hello World')); - await text.click(); - await driver.delayMs(1000); - await driver.assertComponentExist(ON.text('success')); - console.log('uitest: TestOpenInspector end'); - done(); + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); }) }) } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp index 94174827aae4c33bd34fe99fb0086edc2e5ccd64..4ccc030ff6e9eed72b2e1b88faf48d40bcaee413 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp @@ -97,7 +97,7 @@ static JSVM_Value Consoleinfo(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value args[1]; #define MAX_LOG_LENGTH 255 char log[MAX_LOG_LENGTH + 1] = ""; - size_t logLength; + size_t logLength = 0; JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL)); JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, args[0], log, MAX_LOG_LENGTH, &logLength)); @@ -165,8 +165,8 @@ static JSVM_Value Add(JSVM_Env env, JSVM_CallbackInfo info) size_t argc = 2; JSVM_Value args[2]; JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL)); - double num1; - double num2; + double num1 = 0; + double num2 = 0; JSVM_CALL(OH_JSVM_GetValueDouble(env, args[0], &num1)); JSVM_CALL(OH_JSVM_GetValueDouble(env, args[1], &num2)); JSVM_Value sum = nullptr; @@ -194,7 +194,7 @@ static JSVM_Value AssertEqual(JSVM_Env env, JSVM_CallbackInfo info) static int fromOHStringValue(JSVM_Env &env, JSVM_Value &value, std::string &result) { - size_t size; + size_t size = 0; CHECK_RET(OH_JSVM_GetValueStringUtf8(env, value, nullptr, 0, &size)); char resultStr[size + 1]; CHECK_RET(OH_JSVM_GetValueStringUtf8(env, value, resultStr, size + 1, &size)); @@ -369,7 +369,7 @@ static int32_t TestJSVM() };"; // 创建首个运行环境,并绑定TS回调 - uint32_t coreId1; + uint32_t coreId1 = 0; CHECK_COND(CreateJsCore(&coreId1) == 0); OH_LOG_INFO(LOG_APP, "TEST coreId: %{public}d", coreId1); // 在首个运行环境中执行JS代码 @@ -378,7 +378,7 @@ static int32_t TestJSVM() OH_LOG_INFO(LOG_APP, "TEST evaluateJS: %{public}s", result1.c_str()); // 创建第二个运行环境,并绑定TS回调 - uint32_t coreId2; + uint32_t coreId2 = 0; CHECK_COND(CreateJsCore(&coreId2) == 0); OH_LOG_INFO(LOG_APP, "TEST coreId: %{public}d", coreId2); // 在第二个运行环境中执行JS代码 diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 index 1c36e137158a8da3a583e20711164b97beecb6e7..9612e3efcae071bf78fa012c25fdecba39a2201a 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 @@ -20,6 +20,7 @@ "description": "$string:module_desc", "mainElement": "RuntimetaskAbility", "deviceTypes": [ + "phone", "tablet" ], "deliveryWithInstall": true, diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets index 76178e615f995221e061608e23225c13e7631c02..054c015958f24874c78b1718f195891c6aa08e5a 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets @@ -30,7 +30,7 @@ export default function abilityTest() { const want: Want = { bundleName: bundleName, - abilityName: 'EntryAbility' + abilityName: 'RuntimetaskAbility' } const driver = Driver.create(); await delegator.startAbility(want); @@ -38,7 +38,7 @@ export default function abilityTest() { const ability: UIAbility = await delegator.getCurrentTopAbility(); console.log('get top ability'); - expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + expect(ability.context.abilityInfo.name).assertEqual('RuntimetaskAbility'); await driver.delayMs(1000); const text = await driver.findComponent(ON.text('Hello World')); diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmProcess/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmProcess/build-profile.json5 index a21d5afe8864439233629ea7ea6ae9958d18ee60..1da918aa9e57411b2c1c473ac550a8657f980eda 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmProcess/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmProcess/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 index 37ccc6996072997865e300d2432a11314b82bb24..ae6bc7a7bd25b9c84980e63af04b7d816b3be245 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp index 41151a4fc1727538fb34f9c0098e6479e440202e..861b016970a949ce16ccc8aec75751e64def070b 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp @@ -64,7 +64,6 @@ static int g_aa = 0; #define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) // [EndExclude oh_jsvm_create_array] -// CreateArray注册回调 static int g_diffValueFive = 5; // OH_JSVM_CreateArray的样例方法 static JSVM_Value CreateArray(JSVM_Env env, JSVM_CallbackInfo info) @@ -85,6 +84,7 @@ static JSVM_Value CreateArray(JSVM_Env env, JSVM_CallbackInfo info) } return array; } +// CreateArray注册回调 static JSVM_CallbackStruct param[] = { {.data = nullptr, .callback = CreateArray}, }; diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp index 0600c70a8b2d65608f65964c6ce6194343c9119a..60ecbcbc64e9e0459350139285f90420f18b644e 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp @@ -71,7 +71,7 @@ static JSVM_Value CreateTypedArray(JSVM_Env env, JSVM_CallbackInfo info) size_t argc = 1; JSVM_Value args[1] = {nullptr}; OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); - int32_t typeNum; + int32_t typeNum = 0; OH_JSVM_GetValueInt32(env, args[0], &typeNum); JSVM_TypedarrayType arrayType; // 用于存储每个元素的大小 @@ -215,4 +215,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 index fb1425aa3a942e155a36c0882194e164936775ef..c1a37baeb2a9134a25876e0fae5285a0e950b4cc 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp index 449f21385f3dd18c334b1eae97df053ea275625b..73681596ee784e2cd2af361cb390bca139962e05 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp @@ -71,8 +71,8 @@ static JSVM_Value CreateArraybuffer(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value result = nullptr; // 解析传递的参数 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); - int32_t value; - size_t length; + int32_t value = 0; + size_t length = 0; OH_JSVM_GetValueInt32(env, argv[0], &value); length = size_t(value); void *data; @@ -165,4 +165,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 index 1e50db2443edc61aec0548c175cf1bdd9b7764c9..34e150c952207fb41279af9e922c4627f92f3ab4 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp index da556acb02dbee12e55c72c7704b385d26036a47..3f3eb26e25fce50c02ee3f3190687207668e0c86 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp @@ -70,8 +70,8 @@ static JSVM_Value GetBigintInt64(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value args[1] = {nullptr}; OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); // 从传入的参数中提取64位整数的BigInt数据 - int64_t value; - bool lossLess; + int64_t value = 0; + bool lossLess = false; OH_JSVM_GetValueBigintInt64(env, args[0], &value, &lossLess); // 判断从JS侧获取bigint是否为无损转换,如果不是抛出异常 if (!lossLess) { @@ -160,4 +160,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp index 91c1992fbb8ccfbe28bb3c32939eec22a922d415..cf04863edceb55d4aaa743e8ec45f0819a722688 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp @@ -77,7 +77,7 @@ static JSVM_Value GetValueBigintWords(JSVM_Env env, JSVM_CallbackInfo info) // 调用OH_JSVM_GetValueBigintWords接口获取wordCount JSVM_Status status = OH_JSVM_GetValueBigintWords(env, args[0], nullptr, &wordCount, nullptr); OH_LOG_INFO(LOG_APP, "OH_JSVM_GetValueBigintWords wordCount:%{public}d.", wordCount); - if (wordCount == 0 || wordCount > SOME_REASONABLE_MAX) { + if (wordCount == 0 || wordCount > MAX_MALLOC_SIZE) { OH_LOG_ERROR(LOG_APP, "Invalid wordCount: %{public}zu", wordCount); return nullptr; } diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 index 31b64e85fc254a9eba5e8c5601de3969a6c8b88d..9efca683ee4c3093049bbe9031e374fdb26a0498 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 index f21d0c342cb31410ee3d1de43802e224e3bd2a25..5b171a256ea5d8bf13e55fe1ffe52bf4c42d1125 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp index 9f3162b40b8f42fd789069750900c4d05b41ea6e..279f6a7ae1915b3489966b1ee97157886394b0bc 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp @@ -76,7 +76,7 @@ static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info) bool isDate; JSVM_CALL(OH_JSVM_IsDate(env, returnValue, &isDate)); - if (isDate == false) { + if (!isDate) { OH_LOG_ERROR(LOG_APP, "JSVM IsDate fail"); return returnValue; } @@ -167,4 +167,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp index ea80d807e3c3fb0e3cdf19c63e7a6e10975684fe..33fd34207365bf968bc1a95e34ae26187604d9d2 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp @@ -71,7 +71,7 @@ static JSVM_Value GetDateValue(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value args[1] = {nullptr}; JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); // 获取传入的Unix Time Stamp时间 - double value; + double value = 0; JSVM_CALL(OH_JSVM_GetDateValue(env, args[0], &value)); // 将获取到的Unix Time Stamp时间转化为日期字符串打印 @@ -160,4 +160,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp index 97281822a22a4a70c98e961baa454920ed03ff1f..b707e39da0a127728390cb30b69a9f3a8083932f 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp @@ -68,7 +68,7 @@ static JSVM_Value IsDate(JSVM_Env env, JSVM_CallbackInfo info) size_t argc = 1; JSVM_Value args[1] = {nullptr}; JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); - bool isData; + bool isData = false; JSVM_CALL(OH_JSVM_IsDate(env, args[0], &isData)); OH_LOG_INFO(LOG_APP, "JSVM IsDate success:%{public}d", isData); @@ -152,4 +152,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 index 18fd83aa12f9a4106306dc669fe2685c4a9b67e8..f32503c0eacf92650159156d9ea0ce05a2c632ac 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets index 08042f2a2f9c44c4c015665827faa679aa0b4974..49df81dead8be5cf31bb68b85b0d7c07c600e892 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets @@ -18,7 +18,7 @@ import napitest from 'libcheckobjecttypetag.so'; @Entry @Component struct Index { - @State message: string = 'OH_JSVM_CheckObjectTypeTag'; + @State message: string = 'Hello World'; build() { Row() { diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp index d3aea8acd04f1ea5c72da3fc34f2ffd0546505d9..6b07f7362ecdad30ac535e3518b259bf0023dd73 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp @@ -63,7 +63,6 @@ static int g_aa = 0; #define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) // [EndExclude oh_jsvm_get_prototype] -// GetPrototype注册回调 // OH_JSVM_GetPrototype的样例方法 static JSVM_Value GetPrototype(JSVM_Env env, JSVM_CallbackInfo info) { @@ -79,6 +78,7 @@ static JSVM_Value GetPrototype(JSVM_Env env, JSVM_CallbackInfo info) } return result; } +// GetPrototype注册回调 static JSVM_CallbackStruct param[] = { {.data = nullptr, .callback = GetPrototype}, }; diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets index 235203dd233fe6d11e5afeed7ac3b9a49528e7bb..5b685fd47cbbd4b4ab274bddfd2b22969602b8b9 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets @@ -30,7 +30,7 @@ export default function abilityTest() { const want: Want = { bundleName: bundleName, - abilityName: 'EntryAbility' + abilityName: 'SymbolforAbility' } const driver = Driver.create(); await delegator.startAbility(want); @@ -38,7 +38,7 @@ export default function abilityTest() { const ability: UIAbility = await delegator.getCurrentTopAbility(); console.log('get top ability'); - expect(ability.context.abilityInfo.name).assertEqual('EntryAbility'); + expect(ability.context.abilityInfo.name).assertEqual('SymbolforAbility'); await driver.delayMs(1000); const text = await driver.findComponent(ON.text('Hello World')); diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 index b7afbd979aacf083f79081ba57f94260e41d7122..eb22641000d77616b197d0e248e4f7d1b014f177 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp index ebd0e23ed129bafe28e5dda23753aa592aa36b28..cb08d6a9031a0612a6075d3949767913a1a21f86 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp @@ -100,11 +100,10 @@ static JSVM_CallbackStruct *method = param; static JSVM_PropertyDescriptor descriptor[] = { {"getBoolean", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, }; - +// [End oh_jsvm_get_boolean] // 样例测试js const char *SRC_CALL_NATIVE = R"JS(getBoolean(1, 2))JS"; // const char *SRC_CALL_NATIVE = R"JS(getBoolean(1, 1))JS"; -// [End oh_jsvm_get_boolean] static int32_t TestJSVM() { @@ -169,4 +168,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp index 148def4d1362db21735f509ea7785fd67f01393e..5c2a2b68a8bc17156f9258ab77523c5c15921cdb 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp @@ -63,15 +63,6 @@ static int g_aa = 0; #define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) // [EndExclude oh_jsvm_get_null] -// GetNull注册回调 -static JSVM_CallbackStruct param[] = { - {.data = nullptr, .callback = GetNull}, -}; -static JSVM_CallbackStruct *method = param; -// GetNull方法别名,供JS调用 -static JSVM_PropertyDescriptor descriptor[] = { - {"getNull", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, -}; // OH_JSVM_GetNull的样例方法 static JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info) { @@ -84,6 +75,15 @@ static JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info) } return nullValue; } +// GetNull注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetNull}, +}; +static JSVM_CallbackStruct *method = param; +// GetNull方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getNull", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; // [End oh_jsvm_get_null] // 样例测试js const char *SRC_CALL_NATIVE = R"JS(getNull())JS"; @@ -151,4 +151,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp index 7f587272a1371bc24cdb57d1df50a8b57bc74ad6..44d65bcc13d9e3e3e8d64a6e1bd34f9a0d1081aa 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp @@ -63,15 +63,6 @@ static int g_aa = 0; #define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) // [EndExclude oh_jsvm_get_undefined] -// GetUndefined注册回调 -static JSVM_CallbackStruct param[] = { - {.data = nullptr, .callback = GetUndefined}, -}; -static JSVM_CallbackStruct *method = param; -// GetUndefined方法别名,供JS调用 -static JSVM_PropertyDescriptor descriptor[] = { - {"getUndefined", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, -}; // OH_JSVM_GetUndefined的样例方法 static JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info) { @@ -89,6 +80,15 @@ static JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info) } return value; } +// GetUndefined注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetUndefined}, +}; +static JSVM_CallbackStruct *method = param; +// GetUndefined方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getUndefined", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; // [End oh_jsvm_get_undefined] // 样例测试js const char *SRC_CALL_NATIVE = R"JS(getUndefined())JS"; diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp index ec3f580e1d3bc57b87d2e0970c065fb93ad09000..8b26f7f96949ec0debbb9a0c9d2bfa29c87c0d58 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp @@ -63,15 +63,6 @@ static int g_aa = 0; #define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) // [EndExclude oh_jsvm_get_value_bool] -// GetValueBool注册回调 -static JSVM_CallbackStruct param[] = { - {.data = nullptr, .callback = GetValueBool}, -}; -static JSVM_CallbackStruct *method = param; -// GetValueBool方法别名,供JS调用 -static JSVM_PropertyDescriptor descriptor[] = { - {"getValueBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, -}; // OH_JSVM_GetValueBool的样例方法 static JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info) { @@ -91,6 +82,15 @@ static JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info) OH_JSVM_GetBoolean(env, result, &boolJv); return boolJv; } +// GetValueBool注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueBool}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueBool方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; // [End oh_jsvm_get_value_bool] // 样例测试js const char *SRC_CALL_NATIVE = R"JS(getValueBool("abc"))JS"; @@ -160,4 +160,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 index 9c52a08979c4468f1f8d201f605d621c52703ce6..11165589112deb0438bb0513739a6bfa294a7f3c 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp index c75293bfcc2d9722c95d3ecde763aace877eb5c9..f28b2d1a78dabb35b96307b7d7a3878540f9c3ba 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp @@ -125,8 +125,8 @@ static JSVM_PropertyDescriptor descriptor[] = { // 样例测试js const char *SRC_CALL_NATIVE_CREATE_PROMISE = R"JS(createPromise())JS"; -const char *SRC_CALL_NATIVE_RESOLVE_REJECT_DEFERRED1 = R"JS(resolveRejectDeferred('success','fail', true))JS"; -const char *SRC_CALL_NATIVE_RESOLVE_REJECT_DEFERRED2 = R"JS(resolveRejectDeferred('success','fail', false))JS"; +const char *SRC_CALL_NATIVE_RESOLVE_REJECT_DEFERRED1 = R"JS(resolveRejectDeferred('success', 'fail', true))JS"; +const char *SRC_CALL_NATIVE_RESOLVE_REJECT_DEFERRED2 = R"JS(resolveRejectDeferred('success', 'fail', false))JS"; // [End oh_jsvm_resolvedeferred_and_rejectdeferred] static int32_t TestJSVM() @@ -194,4 +194,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 index 045c2423e4f0ffb9b60db95ffef85f005098f90b..8104d964b31ab5db37e2709c694c216500648246 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp index 66b8d9957db20ceb190f236b6cd938845b72c9e4..26941b2d3f55b48e36f5a25d73823f1559472708 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp @@ -72,7 +72,7 @@ static JSVM_Value GetAllPropertyNames(JSVM_Env env, JSVM_CallbackInfo info) // 获取给定对象的所有属性名称(自有属性) JSVM_Value result; JSVM_Status status = OH_JSVM_GetAllPropertyNames(env, args[0], - JSVM_KeyCollectionMode::JSVM_KEY_O_ONLY, + JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY, JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result); if (status != JSVM_OK) { diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets index 63637d06a6008563416d916d37364204686fb897..ac094a4d0290ab59e0d052509db08bd9b15eb329 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets @@ -18,7 +18,7 @@ import napitest from 'libgetallpropertynames.so'; @Entry @Component struct Index { - @State message: string = 'OH_JSVM_GetAllPropertyNames'; + @State message: string = 'Hello World'; build() { Row() { diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 index d8f9f6ecb591e1e2823bdeee78c1006debab05c5..bbae3db91abce73f31308e5affb94bed5ee4836a 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp index 1a292bed1d3ce828011516009946c355c921abc0..f9d3da4bfb3c88bf91fcd609ead81930d91c8d07 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp @@ -78,7 +78,7 @@ static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) OH_LOG_ERROR(LOG_APP, "JSVM CreateStringLatin1 fail"); } else { char buf[MAX_BUFFER_SIZE]; - size_t lengthInner; + size_t lengthInner = 0; OH_JSVM_GetValueStringLatin1(env, result, buf, MAX_BUFFER_SIZE, &lengthInner); OH_LOG_INFO(LOG_APP, "JSVM CreateStringLatin1 success: %{public}s", buf); } diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp index 22ae0ae91452f6c869e2e5a59d05071f1f227c7f..b5c61ab6285ca6c2704457f3aa7db44dfee11ff9 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp @@ -73,7 +73,7 @@ static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value args[1] = {nullptr}; OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); char buf[MAX_BUFFER_SIZE]; - size_t length; + size_t length = 0; JSVM_Value jsvmRes = nullptr; JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], buf, MAX_BUFFER_SIZE, &length); if (status != JSVM_OK) { @@ -164,4 +164,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp index 6ff3ff67a119c76e0320f9bcd3b692be1af25bb0..ade2f8a643bef79bffbeeb9996fe56d625cb9615 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp @@ -75,7 +75,7 @@ static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], nullptr, 0, &length); if (length == 0 || length > MAX_MALLOC_SIZE) { OH_LOG_ERROR(LOG_APP, "Invalid string length: %{public}zu", length); - return jsVmResult; + return nullptr; } char *buf = (char *)malloc(length + 1); status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length); @@ -171,4 +171,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 index 7592cbeea8097139db112bbcc5001446e76b6dc8..d640eca2b7007ae359d19f5f62e8f607cceaab6e 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp index 03d8f1f8d943b4569056bdfea8eb01464ab7b43e..39640c1f3e1b5859de165875c3410297ef93be16 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp @@ -70,7 +70,7 @@ static JSVM_Value GetDouble(JSVM_Env env, JSVM_CallbackInfo info) size_t argc = 1; JSVM_Value args[1] = {nullptr}; OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); - double value; + double value = 0; JSVM_Status status = OH_JSVM_GetValueDouble(env, args[0], &value); if (status != JSVM_OK) { OH_LOG_ERROR(LOG_APP, "JSVM GetDouble fail"); @@ -157,4 +157,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 index 3a9eaaf8f924a240fa80ae1d0fb27bd6a8b8afb5..454171aea4d609371f08c4d128a1c1f3bdefc9b2 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp index e8013f05ad8a476fe9f11459fbd0775442e9f5d6..9532192d4691d3134f30c5c8ea6f0381db43e1e6 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp @@ -91,7 +91,7 @@ static JSVM_Value JsVmThrowTypeError(JSVM_Env env, JSVM_CallbackInfo info) // 如果没有传递参数,直接抛出错误 OH_JSVM_ThrowTypeError(env, "-1", "throwing type error"); } else if (argc == 1) { - size_t length; + size_t length = 0; // 通过入参获取到javaScript侧传入的字符串长度 OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length); char *buffer = new char[length + 1]; @@ -180,4 +180,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp index 44e52a83f4b2b432de46e9d31612d907dd17fb89..aa37cc9600efe2f4fbef336959653c2ffb21cea2 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp @@ -87,19 +87,71 @@ static int AddFinalizer(JSVM_VM vm, JSVM_Env env) return 0; } -static void RunDemo(JSVM_VM vm, JSVM_Env env) +static JSVM_Value RunDemo(JSVM_Env env, JSVM_CallbackInfo info) { + JSVM_VM vm; + OH_JSVM_GetVM(env, &vm); if (AddFinalizer(vm, env) != 0) { OH_LOG_INFO(LOG_APP, "Run PromiseRegisterHandler failed"); } + + return nullptr; } + +// RunDemo注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = RunDemo}, +}; +static JSVM_CallbackStruct *method = param; +// RunDemo方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"RunDemo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(RunDemo();)JS"; // [End oh_jsvm_add_finalizer] -static napi_value RunTest(napi_env env, napi_callback_info info) +static int32_t TestJSVM() { + JSVM_InitOptions initOptions = {0}; JSVM_VM vm; - JSVM_Env envs = nullptr; - RunDemo(vm, envs); + JSVM_Env env = nullptr; + JSVM_VMScope vmScope; + JSVM_EnvScope envScope; + JSVM_HandleScope handleScope; + JSVM_Value result; + // 初始化JavaScript引擎实例 + if (g_aa == 0) { + g_aa++; + CHECK(OH_JSVM_Init(&initOptions)); + } + // 创建JSVM环境 + CHECK(OH_JSVM_CreateVM(nullptr, &vm)); + CHECK(OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env)); + CHECK(OH_JSVM_OpenVMScope(vm, &vmScope)); + CHECK_RET(OH_JSVM_OpenEnvScope(env, &envScope)); + CHECK_RET(OH_JSVM_OpenHandleScope(env, &handleScope)); + + // 通过script调用测试函数 + JSVM_Script script; + JSVM_Value jsSrc; + CHECK_RET(OH_JSVM_CreateStringUtf8(env, SRC_CALL_NATIVE, JSVM_AUTO_LENGTH, &jsSrc)); + CHECK_RET(OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script)); + CHECK_RET(OH_JSVM_RunScript(env, script, &result)); + + // 销毁JSVM环境 + CHECK_RET(OH_JSVM_CloseHandleScope(env, handleScope)); + CHECK_RET(OH_JSVM_CloseEnvScope(env, envScope)); + CHECK(OH_JSVM_CloseVMScope(vm, vmScope)); + CHECK(OH_JSVM_DestroyEnv(env)); + CHECK(OH_JSVM_DestroyVM(vm)); + return 0; +} + +static napi_value RunTest(napi_env env, napi_callback_info info) +{ + TestJSVM(); return nullptr; } @@ -123,4 +175,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 index 3e71ddabaf056db051a1f2eeb8d3e402777fe03b..a5bf41e5e51e2842aed9d42e87acda99226fedb0 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 index 589d45ae023ea47d753f0993a8b0899ac4a6de65..881c2887fceac9bccd1aec5222c61eada5ee448b 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp index c6fa3cb59ae7c15418374f62e962d82725358a27..2e3bd0d4bdcdcc3579f017b549f1b2d0ac751596 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp @@ -24,40 +24,48 @@ static int g_aa = 0; -#define CHECK_RET(theCall) \ - do { \ - JSVM_Status cond = theCall; \ - if ((cond) != JSVM_OK) { \ - const JSVM_ExtendedErrorInfo *info; \ - OH_JSVM_GetLastErrorInfo(env, &info); \ - OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = %{public}d message = %{public}s", \ - __FILE__, __LINE__, cond, info != nullptr ? info->errorMessage : ""); \ - return -1; \ - } \ +#define CHECK_RET(theCall) \ + do { \ + JSVM_Status cond = theCall; \ + if ((cond) != JSVM_OK) { \ + const JSVM_ExtendedErrorInfo *info; \ + OH_JSVM_GetLastErrorInfo(env, &info); \ + OH_LOG_ERROR(LOG_APP, \ + "jsvm fail file: %{public}s line: %{public}d ret = %{public}d message = %{public}s", \ + __FILE__, \ + __LINE__, \ + cond, \ + info != nullptr ? info->errorMessage : ""); \ + return -1; \ + } \ } while (0) -#define CHECK(theCall) \ - do { \ - JSVM_Status cond = theCall; \ - if ((cond) != JSVM_OK) { \ - OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = %{public}d", __FILE__, __LINE__, \ - cond); \ - return -1; \ - } \ +#define CHECK(theCall) \ + do { \ + JSVM_Status cond = theCall; \ + if ((cond) != JSVM_OK) { \ + OH_LOG_ERROR( \ + LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = %{public}d", __FILE__, __LINE__, cond); \ + return -1; \ + } \ } while (0) // 用于调用theCall并检查其返回值是否为JSVM_OK。 // 如果不是,则调用GET_AND_THROW_LAST_ERROR处理错误并返回retVal。 -#define JSVM_CALL_BASE(env, theCall, retVal) \ - do { \ - JSVM_Status cond = theCall; \ - if (cond != JSVM_OK) { \ - const JSVM_ExtendedErrorInfo *info; \ - OH_JSVM_GetLastErrorInfo(env, &info); \ - OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = %{public}d message = %{public}s", \ - __FILE__, __LINE__, cond, info != nullptr ? info->errorMessage : ""); \ - return retVal; \ - } \ +#define JSVM_CALL_BASE(env, theCall, retVal) \ + do { \ + JSVM_Status cond = theCall; \ + if (cond != JSVM_OK) { \ + const JSVM_ExtendedErrorInfo *info; \ + OH_JSVM_GetLastErrorInfo(env, &info); \ + OH_LOG_ERROR(LOG_APP, \ + "jsvm fail file: %{public}s line: %{public}d ret = %{public}d message = %{public}s", \ + __FILE__, \ + __LINE__, \ + cond, \ + info != nullptr ? info->errorMessage : ""); \ + return retVal; \ + } \ } while (0) // JSVM_CALL_BASE的简化版本,返回nullptr @@ -255,7 +263,8 @@ EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) { OH_LOG_INFO(LOG_APP, "JSVM Init"); - napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}, + napi_property_descriptor desc[] = { + {"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}, }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp index ca0f8add77962d2b872101afa5eb0f60c1165093..45a8016cc6255eebc99bc8cdd33627d60f01dc2d 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp @@ -98,7 +98,7 @@ static JSVM_Value ConsoleInfo(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value args[1]; #define MAX_LOG_LENGTH 255 char log[MAX_LOG_LENGTH + 1] = ""; - size_t logLength; + size_t logLength = 0; JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL)); OH_JSVM_GetValueStringUtf8(env, args[0], log, MAX_LOG_LENGTH, &logLength); @@ -194,4 +194,4 @@ static napi_module demoModule = { .reserved = {0}, }; -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp index 8584008f8f1e7f9eafefcdc8448a0a4316ae741c..3ff615ef482c81a49087a0ef9f0f396b38a097c9 100644 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp @@ -36,6 +36,12 @@ static int g_aa = 0; } \ } while (0) +#define CHECK(cond) \ + if (!(cond)) { \ + OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = %{public}d", __FILE__, __LINE__, \ + cond); \ + return -1; \ + } // 用于调用theCall并检查其返回值是否为JSVM_OK。 // 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回retVal。 @@ -55,13 +61,12 @@ static int g_aa = 0; #define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) // [EndExclude jsvm_wasm] -#ifndef CHECK -#define CHECK(cond) \ - do { \ - if (!(cond)) { \ - OH_LOG_ERROR(LOG_APP, "CHECK FAILED"); \ - abort(); \ - } \ +#ifndef CHECK_STATUS +#define CHECK_STATUS(cond) \ + do { \ + if (!(cond)) { \ + OH_LOG_ERROR(LOG_APP, "CHECK FAILED"); \ + } \ } while (0) #endif @@ -70,7 +75,7 @@ static bool IsWasmModuleObject(JSVM_Env env, JSVM_Value value) { bool result; JSVM_Status status = OH_JSVM_IsWasmModuleObject(env, value, &result); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); return result; } @@ -79,7 +84,7 @@ static JSVM_Value CreateString(JSVM_Env env, const char *str) { JSVM_Value jsvmStr; JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, JSVM_AUTO_LENGTH, &jsvmStr); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); return jsvmStr; } @@ -88,7 +93,7 @@ static JSVM_Value CreateInt32(JSVM_Env env, int32_t val) { JSVM_Value jsvmInt32; JSVM_Status status = OH_JSVM_CreateInt32(env, val, &jsvmInt32); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); return jsvmInt32; } @@ -98,20 +103,20 @@ static JSVM_Value InstantiateWasmModule(JSVM_Env env, JSVM_Value wasmModule) JSVM_Status status = JSVM_OK; JSVM_Value globalThis; status = OH_JSVM_GetGlobal(env, &globalThis); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); JSVM_Value webAssembly; status = OH_JSVM_GetProperty(env, globalThis, CreateString(env, "WebAssembly"), &webAssembly); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); JSVM_Value webAssemblyInstance; status = OH_JSVM_GetProperty(env, webAssembly, CreateString(env, "Instance"), &webAssemblyInstance); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); JSVM_Value instance; JSVM_Value argv[] = {wasmModule}; status = OH_JSVM_NewInstance(env, webAssemblyInstance, 1, argv, &instance); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); return instance; } @@ -141,11 +146,11 @@ static void VerifyAddWasmInstance(JSVM_Env env, JSVM_Value wasmInstance) // 从 wasm instance 获取 exports.add 函数 JSVM_Value exports; status = OH_JSVM_GetProperty(env, wasmInstance, CreateString(env, "exports"), &exports); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); JSVM_Value add; status = OH_JSVM_GetProperty(env, exports, CreateString(env, "add"), &add); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); // 执行 exports.add(1, 2),期望得到结果 3 JSVM_Value undefined; @@ -156,10 +161,10 @@ static void VerifyAddWasmInstance(JSVM_Env env, JSVM_Value wasmInstance) JSVM_Value result; const int argumentCount = 2; status = OH_JSVM_CallFunction(env, undefined, add, argumentCount, argv, &result); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); int32_t resultInt32; OH_JSVM_GetValueInt32(env, result, &resultInt32); - CHECK(resultInt32 == 3); + CHECK_STATUS(resultInt32 == 3); } // WebAssembly demo 主函数 @@ -172,14 +177,14 @@ static JSVM_Value WasmDemo(JSVM_Env env, JSVM_CallbackInfo info) JSVM_Value wasmModule; // 根据 wasm 字节码得到 wasm module status = OH_JSVM_CompileWasmModule(env, wasmBytecode, wasmBytecodeLength, NULL, 0, NULL, &wasmModule); - CHECK(status == JSVM_OK); - CHECK(IsWasmModuleObject(env, wasmModule)); + CHECK_STATUS(status == JSVM_OK); + CHECK_STATUS(IsWasmModuleObject(env, wasmModule)); // 对当前 wasm module 中定义的第一个函数 (即 add) 执行编译优化 int32_t functionIndex = 0; // 注意:当前只支持 high level optimization,即传入 JSVM_WASM_OPT_BASELINE 和传入 JSVM_WASM_OPT_HIGH 效果是一样的 status = OH_JSVM_CompileWasmFunction(env, wasmModule, functionIndex, JSVM_WASM_OPT_HIGH); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); // 对编译得到的 wasm module 进行实例化 JSVM_Value wasmInstance = InstantiateWasmModule(env, wasmModule); // 对实例化的 wasm instance 中的函数进行功能验证 @@ -189,10 +194,10 @@ static JSVM_Value WasmDemo(JSVM_Env env, JSVM_CallbackInfo info) const uint8_t *wasmCacheData = NULL; size_t wasmCacheLength = 0; status = OH_JSVM_CreateWasmCache(env, wasmModule, &wasmCacheData, &wasmCacheLength); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); // 期望 wasm cache 创建成功 - CHECK(wasmCacheData != NULL); - CHECK(wasmCacheLength > 0); + CHECK_STATUS(wasmCacheData != NULL); + CHECK_STATUS(wasmCacheLength > 0); // 通过将 wasm cache 赋值来模拟 cache 持久化,实际使用场景可能将 wasm cache 保存到文件 std::vector cacheBuffer(wasmCacheData, wasmCacheData + wasmCacheLength); @@ -200,31 +205,35 @@ static JSVM_Value WasmDemo(JSVM_Env env, JSVM_CallbackInfo info) // cache 一旦保存完成后,需要显式释放,以免发生内存泄露 // 注意:传入的 JSVM_CacheType 必须匹配 status = OH_JSVM_ReleaseCache(env, wasmCacheData, JSVM_CACHE_TYPE_WASM); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); // 使用 wasm code 反序列化来生成 wasm module bool cacheRejected; JSVM_Value wasmModule2; status = OH_JSVM_CompileWasmModule(env, wasmBytecode, wasmBytecodeLength, cacheBuffer.data(), cacheBuffer.size(), &cacheRejected, &wasmModule2); - CHECK(status == JSVM_OK); + // 传入的 wasm cache 如果是匹配的,且内部校验通过 (如版本),则会接受 cache - CHECK(cacheRejected == false); - CHECK(IsWasmModuleObject(env, wasmModule2)); + CHECK_STATUS(!cacheRejected); + CHECK_STATUS(IsWasmModuleObject(env, wasmModule2)); // 对反序列化得到的 wasmModule2 进行同样的操作:函数编译、实例化、验证功能,期望也都是通过的 status = OH_JSVM_CompileWasmFunction(env, wasmModule2, functionIndex, JSVM_WASM_OPT_HIGH); - CHECK(status == JSVM_OK); + CHECK_STATUS(status == JSVM_OK); + JSVM_Value wasmInstance2 = InstantiateWasmModule(env, wasmModule); VerifyAddWasmInstance(env, wasmInstance2); JSVM_Value result; OH_JSVM_GetBoolean(env, true, &result); + OH_LOG_INFO(LOG_APP, "JSVM resultInt: %{public}d", result); return result; } // WasmDemo 方法注册回调 -static JSVM_CallbackStruct param[] = {{.data = nullptr, .callback = WasmDemo}}; +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = WasmDemo} +}; static JSVM_CallbackStruct *method = param; // 将 C++ callback WasmDemo 函数注册为 JSVM globalThis.wasmDemo 属性,供 JS 侧调用 static JSVM_PropertyDescriptor descriptor[] = { diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 index 38065e7524caaafaa3743939b13bcee624712901..3afd9543a6612e2691cc5c33906a77f01db64978 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 @@ -20,7 +20,6 @@ { "name": "default", "signingConfig": "default", - "compileSdkVersion": "5.0.3(15)", "compatibleSdkVersion": "5.0.3(15)", "targetSdkVersion": "5.0.3(15)", "runtimeOS": "HarmonyOS", diff --git a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets index 485d59283b01ad0e39816668dc6f35cfc8b7ae62..558e66187455a5d68ffdf516393ff7df52dd0aa0 100755 --- a/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ArkTs/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets @@ -23,7 +23,7 @@ const bundleName = abilityDelegatorRegistry.getArguments().bundleName; export default function abilityTest() { describe('ActsAbilityTest', () => { /** - * 打开应用,点击OH_JSVM_StrictEquals执行 + * 打开应用,点击Hello World执行 */ it('testStrictEquals', 0, async (done: Function) => { console.log('uitest: TestStrictEquals begin'); @@ -41,7 +41,7 @@ export default function abilityTest() { expect(ability.context.abilityInfo.name).assertEqual('StrictequalsAbility'); await driver.delayMs(1000); - const text = await driver.findComponent(ON.text('OH_JSVM_StrictEquals')); + const text = await driver.findComponent(ON.text('Hello World')); await text.click(); await driver.delayMs(1000); await driver.assertComponentExist(ON.text('success'));