diff --git a/ArkTS/JSVMAPI/JsvmDebug/.gitignore b/ArkTS/JSVMAPI/JsvmDebug/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmDebug/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..703f9ed9bab50236b5a0f0982d22ba17a60e41dd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmdebug", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmDebug/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f9ac3bc9d51b0f741aea48407a3c035fe191091f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmDebug" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmDebug/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/README.md b/ArkTS/JSVMAPI/JsvmDebug/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9fd86a5dbd2a30c2d93927ed8a21f40e6ea4ea5b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/README.md @@ -0,0 +1,135 @@ +# ArkTS JSVM-API典型使用场景指导 + +### 介绍 + +使用JSVM-API实现跨语言交互,首先需要按照JSVM-API的机制实现模块的注册和加载等相关动作。 + +- ArkTS/JS侧:实现C++方法的调用。代码比较简单,import一个对应的so库后,即可调用C++方法。 +- Native侧:.cpp文件,实现模块的注册。需要提供注册lib库的名称,并在注册回调方法中定义接口的映射关系,即Native方法及对应的JS/ArkTS接口名称等。 + +该工程中展示的代码详细描述可查如下链接: + +- [JSVM-API调试&定位](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/jsvm-debugger-cpuprofiler-heapsnapshot.md) +- [使用 code cache 加速编译](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-code-cache.md) +- [使用JSVM-API接口创建多个引擎执行JS代码并销毁](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-runtime-task.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-----------------------------------------------------------: | :-----------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +aboutcodecache/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libaboutcodecache + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── aboutcodecacheability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +openinspector/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +snapshot/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libsnapshot + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── snapshotability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +runtimetask/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libruntimetask + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── runtimetaskability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +[ohos.permission.INTERNET](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissioninternet) + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmDebug > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/.gitignore b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/build-profile.json5 b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a1d266f456e36c0534d03d1ba57ca7ff2d8d8254 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "aboutcodecache", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libaboutcodecache.so": "file:./src/main/cpp/types/libaboutcodecache" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..cc20c12512451f6ec9698b5dde44430b319ca941 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmDebug) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(aboutcodecache SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(aboutcodecache PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cb5634570dfd40b1997ee22a4178416f81c6ab3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/hello.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start jsvm_code_cache] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +// [StartExclude jsvm_code_cache] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude jsvm_code_cache] + +JSVM_Value UseCodeCache(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 编译参数准备 + JSVM_Value jsSrc; + JSVM_Script script; + JSVM_Value result; + size_t length = 0; + const uint8_t* dataPtr = nullptr; + bool cacheRejected = true; + static std::string src = R"JS( + a = 65536; + b = 32768; + c = a + b; + )JS"; + + // 生成 code cache + { + JSVM_HandleScope handleScope; + OH_JSVM_OpenHandleScope(env, &handleScope); + + // 源码字符串转换为 js 字符串 + OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc); + + // 编译js代码 + OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script); + + // 执行js代码 + OH_JSVM_RunScript(env, script, &result); + int value = 0; + OH_JSVM_GetValueInt32(env, result, &value); + OH_LOG_INFO(LOG_APP, "first run result: %{public}d\n", value); + + if (dataPtr == nullptr) { + // 将js源码编译出的脚本保存到 cache, 可以避免重复编译, 带来性能提升 + OH_JSVM_CreateCodeCache(env, script, &dataPtr, &length); + } + + OH_JSVM_CloseHandleScope(env, handleScope); + } + + // 使用 code cache + { + JSVM_HandleScope handleScope; + OH_JSVM_OpenHandleScope(env, &handleScope); + + // 源码字符串转换为 js 字符串 + OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc); + + // 使用 code cache 编译js代码 + OH_JSVM_CompileScript(env, jsSrc, dataPtr, length, true, &cacheRejected, &script); + + // 执行js代码 + OH_JSVM_RunScript(env, script, &result); + int value = 0; + OH_JSVM_GetValueInt32(env, result, &value); + OH_LOG_INFO(LOG_APP, "second run result: %{public}d\n", value); + + OH_JSVM_CloseHandleScope(env, handleScope); + } + OH_LOG_INFO(LOG_APP, "cache rejected: %{public}d\n", cacheRejected); + return result; +} + +// Register a callback. +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = UseCodeCache} +}; +static JSVM_CallbackStruct *method = param; +// Register the C++ callback as a JSVM globalThis.UseCodeCache property for the JS to call. +static JSVM_PropertyDescriptor descriptor[] = { + {"UseCodeCache", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// [End jsvm_code_cache] + +// 样例测试JS +const char *SRC_CALL_NATIVE = R"JS(UseCodeCache();)JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "aboutcodecache", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/types/libaboutcodecache/Index.d.ts b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/types/libaboutcodecache/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/types/libaboutcodecache/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/types/libaboutcodecache/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/types/libaboutcodecache/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..864f45f378824770265d666f8818af231d042b3a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/cpp/types/libaboutcodecache/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libaboutcodecache.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/ets/aboutcodecacheability/AboutcodecacheAbility.ets b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/ets/aboutcodecacheability/AboutcodecacheAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6cd7641edcacea42d4168ca13b8a879e0c1eb726 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/ets/aboutcodecacheability/AboutcodecacheAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class AboutcodecacheAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..51ba8ce9777190a2e5cd51e663c63433fb54358d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libaboutcodecache.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6e64e7c3a207a8f4a3311a1dc8508057991004e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "aboutcodecache", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "AboutcodecacheAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "AboutcodecacheAbility", + "srcEntry": "./ets/aboutcodecacheability/AboutcodecacheAbility.ets", + "description": "$string:AboutcodecacheAbility_desc", + "icon": "$media:layered_image", + "label": "$string:AboutcodecacheAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7466d16a61207d3ab7a78da766e5a9a445d4da81 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "AboutcodecacheAbility_desc", + "value": "description" + }, + { + "name": "AboutcodecacheAbility_label", + "value": "JsvmDebug" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..51e57c0df48c67804e7953f0f9a7b01f358b7d20 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testcodecache', 0, async (done: Function) => { + console.log('uitest: Testcodecache begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'AboutcodecacheAbility' + } + 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('AboutcodecacheAbility'); + + 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: Testcodecache end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..435ff848b637e47f5ed3383ec626d549086713dd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/aboutcodecache/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "aboutcodecache_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/build-profile.json5 b/ArkTS/JSVMAPI/JsvmDebug/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a6b507adb6dfa317b77ce37220b9f76924b59fd5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/build-profile.json5 @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "openinspector", + "srcPath": "./openinspector", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "snapshot", + "srcPath": "./snapshot", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "aboutcodecache", + "srcPath": "./aboutcodecache", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "runtimetask", + "srcPath": "./runtimetask", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/code-linter.json5 b/ArkTS/JSVMAPI/JsvmDebug/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmDebug/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..47b1a9ebd0480497eeb005badc6c3027428074e4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmDebug/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..acb5922f467d4bc97cb88937f26e971dd4805ecc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/hvigorfile.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} + diff --git a/ArkTS/JSVMAPI/JsvmDebug/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/ohosTest.md b/ArkTS/JSVMAPI/JsvmDebug/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..ff6638ce9050f0647f3ceee382ffe6841ae6dcfc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/ohosTest.md @@ -0,0 +1,10 @@ +# JsvmDebug 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| --------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 获取当前堆快照并将其输出到流 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 使用 code cache 加速编译 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| JSVM-API接口创建多个引擎执行JS代码并销毁 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/.gitignore b/ArkTS/JSVMAPI/JsvmDebug/openinspector/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/build-profile.json5 b/ArkTS/JSVMAPI/JsvmDebug/openinspector/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmDebug/openinspector/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmDebug/openinspector/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/openinspector/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d92ed452ae96fe6dfa75db591db4a7c70def7233 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "openinspector", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libopeninspector.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..fafdfa0936b61c3089a717a66e1854df3efce13b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmDebug) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(openinspector SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(openinspector PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..665b23ced4cf269af5690413d9d5b7f54abfd615 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_open_inspector] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +// [StartExclude oh_jsvm_open_inspector] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_open_inspector] + +// 待调试的JS源码 +static std::string g_srcDebugger = R"JS( +const concat = (...args) => args.reduce((a, b) => a + b); +var dialogue = concat('"What ', 'is ', 'your ', 'name ', '?"'); +dialogue = concat(dialogue, ' --', '"My ', 'name ', 'is ', 'Bob ', '."'); +)JS"; + +// 开启debugger +static const int INSPECTOR_PORT = 9225; +static void EnableInspector(JSVM_Env env) +{ + // 在指定的主机和端口上激活inspector,创建socket。 + OH_JSVM_OpenInspector(env, "localhost", INSPECTOR_PORT); + // 等待建立socket连接。 + OH_JSVM_WaitForDebugger(env, true); +} + +// 关闭debugger +static void CloseInspector(JSVM_Env env) +{ + // 关闭inspector,结束socket连接。 + OH_JSVM_CloseInspector(env); +} + +static void RunScript(JSVM_Env env) +{ + JSVM_HandleScope handleScope; + OH_JSVM_OpenHandleScope(env, &handleScope); + + JSVM_Value jsSrc; + OH_JSVM_CreateStringUtf8(env, g_srcDebugger.c_str(), g_srcDebugger.size(), &jsSrc); + + JSVM_Script script; + OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script); + + JSVM_Value result; + OH_JSVM_RunScript(env, script, &result); + + OH_JSVM_CloseHandleScope(env, handleScope); +} + +void TestJSVM() +{ + JSVM_InitOptions initOptions{}; + OH_JSVM_Init(&initOptions); + + JSVM_VM vm; + OH_JSVM_CreateVM(nullptr, &vm); + JSVM_VMScope vmScope; + OH_JSVM_OpenVMScope(vm, &vmScope); + + JSVM_Env env; + OH_JSVM_CreateEnv(vm, 0, nullptr, &env); + // 执行JS代码之前打开debugger。 + EnableInspector(env); + JSVM_EnvScope envScope; + OH_JSVM_OpenEnvScope(env, &envScope); + + // 执行JS代码。 + RunScript(env); + + OH_JSVM_CloseEnvScope(env, envScope); + // 执行JS代码之后关闭debugger。 + CloseInspector(env); + OH_JSVM_DestroyEnv(env); + OH_JSVM_CloseVMScope(vm, vmScope); + OH_JSVM_DestroyVM(vm); +} +// [End jsvm_open_inspector] + +static napi_value RunTest(napi_env env, napi_callback_info info) +{ + TestJSVM(); + return nullptr; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "openinspector", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8547e3a114fd2d5bf4f9d61252bab1f77cb59991 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libopeninspector.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a3c889cdea0fa1e4d7c8778843a2cee40306b295 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libopeninspector.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..71598eafd4f328515f880fcca1592cb66642b109 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/module.json5 @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "openinspector", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.INTERNET", + "reason": "$string:app_name", + "usedScene": { + "abilities": [ + "FromAbility" + ], + "when": "inuse" + } + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e093041b1b2803be636046cf61a547d2da895f77 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmDebug" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // 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/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d709d9cb6adc9753b540434ca6815a02478ba993 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/openinspector/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "openinspector_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/.gitignore b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/build-profile.json5 b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..52c578358ceef8673400a283f2dbf4714056cc45 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "runtimetask", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libruntimetask.so": "file:./src/main/cpp/types/libruntimetask" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..196f881cee2734ff19b103339d9e26023a2be012 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmDebug) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(runtimetask SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(runtimetask PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19b6619c27f42d81e512b99a3a0c891c08507214 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/hello.cpp @@ -0,0 +1,430 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +// [Start runtime_task] +#include +#include +#include +using namespace std; +// 定义map管理每个独立vm环境 +static map g_vmMap; +static map g_envMap; +static map g_callBackStructMap; +static uint32_t g_envtagNumber = 0; +static std::mutex g_envMapLock; +// [StartExclude runtime_task] + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude runtime_task] + +#define CHECK_COND(cond) \ + do { \ + if (!(cond)) { \ + OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = false", __FILE__, __LINE__); \ + return -1; \ + } \ + } while (0) + +class Task { +public: + virtual ~Task() = default; + virtual void Run() = 0; +}; +static map> g_taskQueueMap; + +// 自定义Consoleinfo方法 +static JSVM_Value Consoleinfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1]; + #define MAX_LOG_LENGTH 255 + char log[MAX_LOG_LENGTH + 1] = ""; + 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)); + log[MAX_LOG_LENGTH] = 0; + OH_LOG_INFO(LOG_APP, "JSVM API TEST: %{public}s", log); + return nullptr; +} + +// 自定义创建Promise方法用以在JS代码中创建Promise +static JSVM_Value CreatePromise(JSVM_Env env, JSVM_CallbackInfo info) +{ + OH_LOG_INFO(LOG_APP, "JSVM API TEST: CreatePromise start"); + int envID = -1; + // 通过当前env获取envID + for (auto it = g_envMap.begin(); it != g_envMap.end(); ++it) { + if (*it->second == env) { + envID = it->first; + break; + } + } + if (envID == -1) { + OH_LOG_ERROR(LOG_APP, "JSVM API TEST: CreatePromise envID failed"); + return nullptr; + } + JSVM_Value promise; + JSVM_Deferred deferred; + JSVM_CALL(OH_JSVM_CreatePromise(env, &deferred, &promise)); + // 设计ReadTask类用以将promise对象的deferred加入执行队列 + class ReadTask : public Task { + public: + ReadTask(JSVM_Env env, JSVM_Deferred deferred, int envNum) : env_(env), envID_(envNum), deferred_(deferred) {} + void Run() override + { + // string str = "TEST RUN OH_JSVM_ResolveDeferred"; + int envID = 0; + for (auto it = g_envMap.begin(); it != g_envMap.end(); ++it) { + if (*it->second == env_) { + envID = it->first; + break; + } + } + OH_LOG_INFO(LOG_APP, "JSVM API TEST: CreatePromise %{public}d", envID); + JSVM_Value result; + if (OH_JSVM_CreateInt32(env_, envID, &result) != JSVM_OK) { + return; + } + if (OH_JSVM_ResolveDeferred(env_, deferred_, result) != JSVM_OK) { + return; + } + } + + private: + JSVM_Env env_; + int envID_; + JSVM_Deferred deferred_; + }; + g_taskQueueMap[envID].push_back(new ReadTask(env, deferred, envID)); + OH_LOG_INFO(LOG_APP, "JSVM API TEST: CreatePromise end"); + return promise; +} + +// 自定义Add方法 +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 = 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; + JSVM_CALL(OH_JSVM_CreateDouble(env, num1 + num2, &sum)); + return sum; +} + +// 自定义AssertEqual方法 +static JSVM_Value AssertEqual(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)); + + bool isStrictEquals = false; + JSVM_CALL(OH_JSVM_StrictEquals(env, args[0], args[1], &isStrictEquals)); + + if (isStrictEquals) { + OH_LOG_INFO(LOG_APP, "JSVM API TEST RESULT: PASS"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM API TEST RESULT: FAILED"); + } + return nullptr; +} + +static int fromOHStringValue(JSVM_Env &env, JSVM_Value &value, std::string &result) +{ + 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)); + result = resultStr; + return 0; +} + +// 提供创建JSVM运行环境的对外接口并返回对应唯一ID +static int CreateJsCore(uint32_t *result) +{ + OH_LOG_INFO(LOG_APP, "JSVM CreateJsCore START"); + g_taskQueueMap[g_envtagNumber] = deque {}; + + if (g_aa == 0) { + JSVM_InitOptions init_options; + memset(&init_options, 0, sizeof(init_options)); + CHECK(OH_JSVM_Init(&init_options)); + g_aa++; + } + std::lock_guard lock_guard(g_envMapLock); + + // 虚拟机实例 + g_vmMap[g_envtagNumber] = new JSVM_VM; + JSVM_CreateVMOptions options; + JSVM_VMScope vmScope; + memset(&options, 0, sizeof(options)); + CHECK(OH_JSVM_CreateVM(&options, g_vmMap[g_envtagNumber])); + CHECK(OH_JSVM_OpenVMScope(*g_vmMap[g_envtagNumber], &vmScope)); + + // 新环境 + g_envMap[g_envtagNumber] = new JSVM_Env; + g_callBackStructMap[g_envtagNumber] = new JSVM_CallbackStruct[4]; + + // 注册用户提供的本地函数的回调函数指针和数据,通过JSVM-API暴露给js + constexpr int kLoopCount = 4; + for (int i = 0; i < kLoopCount; i++) { + g_callBackStructMap[g_envtagNumber][i].data = nullptr; + } + const int consoleinfoIndex = 0; + const int addIndex = 1; + const int assertEqualIndex = 2; + const int createPromiseIndex = 3; + g_callBackStructMap[g_envtagNumber][consoleinfoIndex].callback = Consoleinfo; + g_callBackStructMap[g_envtagNumber][addIndex].callback = Add; + g_callBackStructMap[g_envtagNumber][assertEqualIndex].callback = AssertEqual; + g_callBackStructMap[g_envtagNumber][createPromiseIndex].callback = CreatePromise; + JSVM_PropertyDescriptor descriptors[] = { + { "consoleinfo", NULL, &g_callBackStructMap[g_envtagNumber][consoleinfoIndex], NULL, NULL, NULL, JSVM_DEFAULT }, + { "add", NULL, &g_callBackStructMap[g_envtagNumber][addIndex], NULL, NULL, NULL, JSVM_DEFAULT }, + { "assertEqual", NULL, &g_callBackStructMap[g_envtagNumber][assertEqualIndex], NULL, NULL, NULL, JSVM_DEFAULT }, + { "createPromise", NULL, &g_callBackStructMap[g_envtagNumber][createPromiseIndex], NULL, NULL, NULL, + JSVM_DEFAULT }, + }; + CHECK(OH_JSVM_CreateEnv( + *g_vmMap[g_envtagNumber], sizeof(descriptors) / sizeof(descriptors[0]), descriptors, g_envMap[g_envtagNumber])); + CHECK(OH_JSVM_CloseVMScope(*g_vmMap[g_envtagNumber], vmScope)); + + OH_LOG_INFO(LOG_APP, "JSVM CreateJsCore END"); + *result = g_envtagNumber; + g_envtagNumber++; + return 0; +} + +// 对外提供释放JSVM环境接口,通过envId释放对应环境 +static int ReleaseJsCore(uint32_t coreEnvId) +{ + OH_LOG_INFO(LOG_APP, "JSVM ReleaseJsCore START"); + CHECK_COND(g_envMap.count(coreEnvId) != 0 && g_envMap[coreEnvId] != nullptr); + + std::lock_guard lock_guard(g_envMapLock); + + CHECK(OH_JSVM_DestroyEnv(*g_envMap[coreEnvId])); + g_envMap[coreEnvId] = nullptr; + g_envMap.erase(coreEnvId); + CHECK(OH_JSVM_DestroyVM(*g_vmMap[coreEnvId])); + g_vmMap[coreEnvId] = nullptr; + g_vmMap.erase(coreEnvId); + delete[] g_callBackStructMap[coreEnvId]; + g_callBackStructMap[coreEnvId] = nullptr; + g_callBackStructMap.erase(coreEnvId); + g_taskQueueMap.erase(coreEnvId); + + OH_LOG_INFO(LOG_APP, "JSVM ReleaseJsCore END"); + return 0; +} + +static std::mutex g_mutexLock; +// 对外提供执行JS代码接口,通过coreID在对应的JSVN环境中执行JS代码 +static int EvaluateJS(uint32_t envId, const char *source, std::string &res) +{ + OH_LOG_INFO(LOG_APP, "JSVM EvaluateJS START"); + + CHECK_COND(g_envMap.count(envId) != 0 && g_envMap[envId] != nullptr); + + JSVM_Env env = *g_envMap[envId]; + JSVM_VM vm = *g_vmMap[envId]; + JSVM_VMScope vmScope; + JSVM_EnvScope envScope; + JSVM_HandleScope handleScope; + JSVM_Value result; + + std::lock_guard lock_guard(g_mutexLock); + { + // 创建JSVM环境 + CHECK_RET(OH_JSVM_OpenVMScope(vm, &vmScope)); + CHECK_RET(OH_JSVM_OpenEnvScope(*g_envMap[envId], &envScope)); + CHECK_RET(OH_JSVM_OpenHandleScope(*g_envMap[envId], &handleScope)); + + // 通过script调用测试函数 + JSVM_Script script; + JSVM_Value jsSrc; + CHECK_RET(OH_JSVM_CreateStringUtf8(env, source, 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_ValueType type; + CHECK_RET(OH_JSVM_Typeof(env, result, &type)); + OH_LOG_INFO(LOG_APP, "JSVM API TEST type: %{public}d", type); + // Execute tasks in the current env event queue + while (!g_taskQueueMap[envId].empty()) { + auto task = g_taskQueueMap[envId].front(); + g_taskQueueMap[envId].pop_front(); + task->Run(); + delete task; + } + + if (type == JSVM_STRING) { + CHECK_COND(fromOHStringValue(env, result, res) != -1); + } else if (type == JSVM_BOOLEAN) { + bool ret = false; + CHECK_RET(OH_JSVM_GetValueBool(env, result, &ret)); + ret ? res = "true" : res = "false"; + } else if (type == JSVM_NUMBER) { + int32_t num; + CHECK_RET(OH_JSVM_GetValueInt32(env, result, &num)); + res = std::to_string(num); + } else if (type == JSVM_OBJECT) { + JSVM_Value objResult; + CHECK_RET(OH_JSVM_JsonStringify(env, result, &objResult)); + CHECK_COND(fromOHStringValue(env, objResult, res) != -1); + } + } + { + bool aal = false; + CHECK_RET(OH_JSVM_PumpMessageLoop(*g_vmMap[envId], &aal)); + CHECK_RET(OH_JSVM_PerformMicrotaskCheckpoint(*g_vmMap[envId])); + CHECK_RET(OH_JSVM_CloseHandleScope(*g_envMap[envId], handleScope)); + CHECK_RET(OH_JSVM_CloseEnvScope(*g_envMap[envId], envScope)); + CHECK_RET(OH_JSVM_CloseVMScope(*g_vmMap[envId], vmScope)); + } + OH_LOG_INFO(LOG_APP, "JSVM EvaluateJS END"); + return 0; +} + +static int32_t TestJSVM() +{ + const char source1[] = "{\ + let a = \"hello World\";\ + consoleinfo(a);\ + const mPromise = createPromise();\ + mPromise.then((result) => {\ + assertEqual(result, 0);\ + });\ + a;\ + };"; + + const char source2[] = "{\ + let a = \"second hello\";\ + consoleinfo(a);\ + let b = add(99, 1);\ + assertEqual(100, b);\ + assertEqual(add(99, 1), 100);\ + createPromise().then((result) => {\ + assertEqual(result, 1);\ + });\ + a;\ + };"; + + // 创建首个运行环境,并绑定TS回调 + uint32_t coreId1 = 0; + CHECK_COND(CreateJsCore(&coreId1) == 0); + OH_LOG_INFO(LOG_APP, "TEST coreId: %{public}d", coreId1); + // 在首个运行环境中执行JS代码 + std::string result1; + CHECK_COND(EvaluateJS(coreId1, source1, result1) == 0); + OH_LOG_INFO(LOG_APP, "TEST evaluateJS: %{public}s", result1.c_str()); + + // 创建第二个运行环境,并绑定TS回调 + uint32_t coreId2 = 0; + CHECK_COND(CreateJsCore(&coreId2) == 0); + OH_LOG_INFO(LOG_APP, "TEST coreId: %{public}d", coreId2); + // 在第二个运行环境中执行JS代码 + std::string result2; + CHECK_COND(EvaluateJS(coreId2, source2, result2) == 0); + OH_LOG_INFO(LOG_APP, "TEST evaluateJS: %{public}s", result2.c_str()); + + // 释放首个运行环境 + CHECK_COND(ReleaseJsCore(coreId1) == 0); + // 释放第二个运行环境 + CHECK_COND(ReleaseJsCore(coreId2) == 0); + OH_LOG_INFO(LOG_APP, "Test NAPI end"); + + return 0; +} +// [End runtime_task] + +static napi_value RunTest(napi_env env, napi_callback_info info) +{ + TestJSVM(); + return nullptr; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "runtimetask", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/types/libruntimetask/Index.d.ts b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/types/libruntimetask/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/types/libruntimetask/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/types/libruntimetask/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/types/libruntimetask/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..16f7eeda818963483593e766ddfc5fd9f2bb3357 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/cpp/types/libruntimetask/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libruntimetask.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..18d41e69dc0d2e9d624b9d9e201e392449cf073f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libruntimetask.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/ets/runtimetaskability/RuntimetaskAbility.ets b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/ets/runtimetaskability/RuntimetaskAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..36c48adba891a245c3af61c902b279645d3caf0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/ets/runtimetaskability/RuntimetaskAbility.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class RuntimetaskAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6ad10719149edd274cb19d64e370ec3409f33a36 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "runtimetask", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "RuntimetaskAbility", + "deviceTypes": [ + "phone", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "RuntimetaskAbility", + "srcEntry": "./ets/runtimetaskability/RuntimetaskAbility.ets", + "description": "$string:RuntimetaskAbility_desc", + "icon": "$media:layered_image", + "label": "$string:RuntimetaskAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..bc07847782985142dcd5095562315ed842fb9333 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "RuntimetaskAbility_desc", + "value": "description" + }, + { + "name": "RuntimetaskAbility_label", + "value": "JsvmDebug" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..054c015958f24874c78b1718f195891c6aa08e5a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testJsvmProcess', 0, async (done: Function) => { + console.log('uitest: TestRuntimeTask begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'RuntimetaskAbility' + } + 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('RuntimetaskAbility'); + + 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')); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..99fd7b9e813430145bbc28198f3c11b97a310739 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/runtimetask/src/ohosTest/module.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "runtimetask_test", + "type": "feature", + "deviceTypes": [ + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/screenshots/JsvmDebug_1.png b/ArkTS/JSVMAPI/JsvmDebug/screenshots/JsvmDebug_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/screenshots/JsvmDebug_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/screenshots/JsvmDebug_2.png b/ArkTS/JSVMAPI/JsvmDebug/screenshots/JsvmDebug_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/screenshots/JsvmDebug_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/.gitignore b/ArkTS/JSVMAPI/JsvmDebug/snapshot/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/build-profile.json5 b/ArkTS/JSVMAPI/JsvmDebug/snapshot/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmDebug/snapshot/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..bbb00a28e0ca0201c021f8c2ba30e0e7445cb8e2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmDebug/snapshot/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/snapshot/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dee921dd018c4a38d4f1d78fa800d96e77bf4e18 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "snapshot", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libsnapshot.so": "file:./src/main/cpp/types/libsnapshot" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6730c6ce01e017589553a4410584984626930b90 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmDebug) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(snapshot SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(snapshot PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afa8fdaf11f2f65d0f05ae181db9092bd4cd923d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/hello.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start heap_snapshot] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +// [StartExclude heap_snapshot] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude heap_snapshot] + +using namespace std; + +// 待调优的JS代码。 +static string g_srcProf = R"JS( +function sleep(delay) { + var start = (new Date()).getTime(); + while ((new Date()).getTime() - start < delay) { + continue; + } +} + +function work3() { + sleep(300); +} + +function work2() { + work3(); + sleep(200); +} + +function work1() { + work2(); + sleep(100); +} + +work1(); +)JS"; + +// 数据输出流回调,用户自定义,处理返回的调优数据,此处以写入文件为例。 +static bool OutputStream(const char *data, int size, void *streamData) +{ + auto &os = *reinterpret_cast(streamData); + if (data) { + os.write(data, size); + } else { + os.close(); + } + return true; +} + +static JSVM_CpuProfiler ProfilingBegin(JSVM_VM vm) +{ + // 文件输出流,保存调优数据,/data/storage/el2/base/files为沙箱路径。以包名为com.example.helloworld为例。 + // 实际文件会保存到/data/app/el2/100/base/com.example.helloworld/files/heap-snapshot-begin.heapsnapshot。 + ofstream heapSnapshot("/data/storage/el2/base/files/heap-snapshot-begin.heapsnapshot", + ios::out | ios::binary | ios::trunc); + // 执行JS前获取一次Heap Snapshot数据。 + OH_JSVM_TakeHeapSnapshot(vm, OutputStream, &heapSnapshot); + JSVM_CpuProfiler cpuProfiler; + // 开启CPU Profiler。 + OH_JSVM_StartCpuProfiler(vm, &cpuProfiler); + return cpuProfiler; +} + +// 关闭调优数据采集工具 +static void ProfilingEnd(JSVM_VM vm, JSVM_CpuProfiler cpuProfiler) +{ + // 文件输出流,保存调优数据,/data/storage/el2/base/files为沙箱路径。以包名为com.example.helloworld为例。 + // 实际文件会保存到/data/app/el2/100/base/com.example.helloworld/files/cpu-profile.cpuprofile。 + ofstream cpuProfile("/data/storage/el2/base/files/cpu-profile.cpuprofile", + ios::out | ios::binary | ios::trunc); + // 关闭CPU Profiler,获取数据。 + OH_JSVM_StopCpuProfiler(vm, cpuProfiler, OutputStream, &cpuProfile); + ofstream heapSnapshot("/data/storage/el2/base/files/heap-snapshot-end.heapsnapshot", + ios::out | ios::binary | ios::trunc); + // 执行JS后再获取一次Heap Snapshot数据,与执行前数据作对比,以分析内存问题或者进行内存调优。 + OH_JSVM_TakeHeapSnapshot(vm, OutputStream, &heapSnapshot); +} + +static JSVM_Value RunScriptWithStatistics(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_VM vm; + OH_JSVM_GetVM(env, &vm); + + // 开始调优。 + auto cpuProfiler = ProfilingBegin(vm); + + JSVM_HandleScope handleScope; + OH_JSVM_OpenHandleScope(env, &handleScope); + + JSVM_Value jsSrc; + OH_JSVM_CreateStringUtf8(env, g_srcProf.c_str(), g_srcProf.size(), &jsSrc); + + JSVM_Script script; + OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script); + + JSVM_Value result; + // 执行JS代码。 + OH_JSVM_RunScript(env, script, &result); + + OH_JSVM_CloseHandleScope(env, handleScope); + + // 结束调优。 + ProfilingEnd(vm, cpuProfiler); + return nullptr; +} +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = RunScriptWithStatistics}, +}; +static JSVM_CallbackStruct *method = param; +// runScriptWithStatistics方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"runScriptWithStatistics", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = R"JS(runScriptWithStatistics();)JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} +// [End heap_snapshot] + +static napi_value RunTest(napi_env env, napi_callback_info info) +{ + TestJSVM(); + return nullptr; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "snapshot", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/types/libsnapshot/Index.d.ts b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/types/libsnapshot/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/types/libsnapshot/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/types/libsnapshot/oh-package.json5 b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/types/libsnapshot/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..16c3808c9505f120ac6dea911d49d7901885583e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/cpp/types/libsnapshot/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libsnapshot.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..c4b31cda95a2229bc66464359ae72adbb32c602b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libsnapshot.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/ets/snapshotability/SnapshotAbility.ets b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/ets/snapshotability/SnapshotAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..1c4217cc16e6a7acfbaa14e465984465bd04d2eb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/ets/snapshotability/SnapshotAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class SnapshotAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0c4cda92ed87a720522d6fe7b4bcdb7c12d46c5f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "snapshot", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "SnapshotAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "SnapshotAbility", + "srcEntry": "./ets/snapshotability/SnapshotAbility.ets", + "description": "$string:SnapshotAbility_desc", + "icon": "$media:layered_image", + "label": "$string:SnapshotAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ac0d6697eeedf1c2e45b72f06d9a105a97f9bffc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "SnapshotAbility_desc", + "value": "description" + }, + { + "name": "SnapshotAbility_label", + "value": "JsvmDebug" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..9f1801beec1f481656dc8ea304ca921b75ea9742 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testSnapshot', 0, async (done: Function) => { + console.log('uitest: TestSnapshot begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'SnapshotAbility' + } + 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('SnapshotAbility'); + + 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: TestSnapshot end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2c8526b3853fcb2175f87eeb0819c5c1d604cb12 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmDebug/snapshot/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "snapshot_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/.gitignore b/ArkTS/JSVMAPI/JsvmProcess/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmProcess/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c70654c811615148cd180442465e50beeca4af71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmprocess", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmProcess/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..6c4c265d8c2e7a6a25091f88d0c6425ca4853c0d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmProcess" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmProcess/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmProcess/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmProcess/README.md b/ArkTS/JSVMAPI/JsvmProcess/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b174b3eb95c761065b353c292cbc524fc0f54ebd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/README.md @@ -0,0 +1,76 @@ +# ArkTS使用JSVM-API实现JS与C/C++语言交互开发流程 + +### 介绍 + +使用JSVM-API实现跨语言交互,首先需要按照JSVM-API的机制实现模块的注册和加载等相关动作。 + +- ArkTS/JS侧:实现C++方法的调用。代码比较简单,import一个对应的so库后,即可调用C++方法。 +- Native侧:.cpp文件,实现模块的注册。需要提供注册lib库的名称,并在注册回调方法中定义接口的映射关系,即Native方法及对应的JS/ArkTS接口名称等。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API实现JS与C/C++语言交互开发流程](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-process.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-------------------------------------------------------------: | :-------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +entry/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 使用JSVM-API实现JS与C/C++语言交互开发流程示例代码 + │ │ │ │ ├── oh-package.json5 // 使用JSVM-API实现JS与C/C++语言交互开发流程示例代码 + │ │ ├── CMakeLists.txt // 使用JSVM-API实现JS与C/C++语言交互开发流程示例代码 + │ │ ├── hello.cpp // 使用JSVM-API实现JS与C/C++语言交互开发流程示例代码 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // 使用JSVM-API实现JS与C/C++语言交互开发流程示例代码 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmProcess > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmProcess/build-profile.json5 b/ArkTS/JSVMAPI/JsvmProcess/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..db797918c4174d90d07ef19e7358f0a5ac1b13cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/build-profile.json5 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/code-linter.json5 b/ArkTS/JSVMAPI/JsvmProcess/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/.gitignore b/ArkTS/JSVMAPI/JsvmProcess/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/build-profile.json5 b/ArkTS/JSVMAPI/JsvmProcess/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fea31f4580540c7c560b62bf531292c471c410f1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/build-profile.json5 @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": ["arm64-v8a", "x86_64"] + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmProcess/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmProcess/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmProcess/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..118bdd4fe7699368a010e04c24f5bfc887cf1298 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..27bd6f5841bfad0b32f0bc7821c9ee0a68529ec2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(entry SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(entry PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0031cf18050a8e9f3727bb6d8bda9cfe39df741 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/hello.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_process] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// OH_JSVM_StrictEquals的样例方法 +static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接受两个入参 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); + // 调用OH_JSVM_StrictEquals接口判断给定的两个JavaScript value是否严格相等 + bool result = false; + JSVM_Status status = OH_JSVM_StrictEquals(env, args[0], args[1], &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_StrictEquals: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_StrictEquals: success: %{public}d", result); + } + JSVM_Value isStrictEqual; + JSVM_CALL(OH_JSVM_GetBoolean(env, result, &isStrictEqual)); + return isStrictEqual; +} +// IsStrictEquals注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsStrictEquals}, +}; +static JSVM_CallbackStruct *method = param; +// IsStrictEquals方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isStrictEquals", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( let data = '123'; + let value = 123; + isStrictEquals(data, value);)JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +// [End oh_jsvm_process] diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..15f72a84a72e0916cbdeafcc0895f9c7a4745b74 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start export_native] +export const runTest: () => void; +// [End export_native] \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6abf3b7c20f22c62aaac6a995a25cae672f73f35 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..280db53c570483c7d05c7da85363600719bfecef --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start call_native_cpp] +import napitest from 'libentry.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} +// [End call_native_cpp] diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4144486d1af4c03b0d767cce1cda86fc0d697f91 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8002e233e660a51ebf737040605ed40ef96e1f4e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmProcess" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/en_US/element/string.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8002e233e660a51ebf737040605ed40ef96e1f4e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/en_US/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmProcess" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/zh_CN/element/string.json b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..52d5186346bf8a90831df8e553ac1b5da49aeaa1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "模块描述" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmProcess" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..85648926249bfa905428b7cbf593b1cf7a5f6188 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testJsvmProcess', 0, async (done: Function) => { + console.log('uitest: TestJsvmProcess 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: TestJsvmProcess end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmProcess/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmProcess/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/oh-package.json5 b/ArkTS/JSVMAPI/JsvmProcess/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmProcess/ohosTest.md b/ArkTS/JSVMAPI/JsvmProcess/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..679da07e5c03390f9ddc4dc6f00bd6621ca80db1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmProcess/ohosTest.md @@ -0,0 +1,8 @@ +# JsvmProcess 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ----------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 使用JSVM-API实现JS与C/C++语言交互开发流程 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmProcess/screenshots/JsvmProcess_1.png b/ArkTS/JSVMAPI/JsvmProcess/screenshots/JsvmProcess_1.png new file mode 100644 index 0000000000000000000000000000000000000000..b5d7d8912c33ad5d396165630aac9f733b4cf98e Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmProcess/screenshots/JsvmProcess_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmProcess/screenshots/JsvmProcess_2.png b/ArkTS/JSVMAPI/JsvmProcess/screenshots/JsvmProcess_2.png new file mode 100644 index 0000000000000000000000000000000000000000..7823f8b8205726b9fe7752fad4e95f7cbff529fa Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmProcess/screenshots/JsvmProcess_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e4a86a1d711b241e1ed1492dce68ab80ee1bf474 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutarray", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..fc7ee24f918c7065f5dafe97a7d67fe3e3031e02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutArray" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/README.md new file mode 100644 index 0000000000000000000000000000000000000000..74c9d2f12034a503144897427f897daad67df73d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/README.md @@ -0,0 +1,320 @@ +# ArkTS使用JSVM-API接口进行array相关开发 + +### 介绍 + +使用 JSVM-API 接口进行数组(array)相关开发时,调用相关接口可以在 JSVM 模块中直接操作和处理 JavaScript 中的数组。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行array相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-array.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :----------------------------------------------------------------: | :----------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createarray/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createarraywithlength/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatearraywithlength + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createarraywithlengthability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createdataview/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatedataview + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createdataviewability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createtypedarray/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatetypedarray + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createtypedarrayability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +deleteelement/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libdeleteelement + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── deleteelementability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getarraylength/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetarraylength + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getarraylengthability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getdataviewinfo/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetdataviewinfo + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getdataviewinfoability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getelement/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetelement + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getelementability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +gettypedarrayinfo/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgettypedarrayinfo + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── gettypedarrayinfoability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +haselement/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libhaselement + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── haselementability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +isarray/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libisarray + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── isarrayability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +isdataview/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libisdataview + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── isdataviewability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +istypedarray/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libistypedarray + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── istypedarrayability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +setelement/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libsetelement + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── setelementability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cf6a40d89f718fcf8407886802eff0e8b024d7c1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/build-profile.json5 @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "createarray", + "srcPath": "./createarray", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createarraywithlength", + "srcPath": "./createarraywithlength", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createtypedarray", + "srcPath": "./createtypedarray", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createdataview", + "srcPath": "./createdataview", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getarraylength", + "srcPath": "./getarraylength", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getdataviewinfo", + "srcPath": "./getdataviewinfo", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "deleteelement", + "srcPath": "./deleteelement", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getelement", + "srcPath": "./getelement", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "gettypedarrayinfo", + "srcPath": "./gettypedarrayinfo", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "haselement", + "srcPath": "./haselement", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "isarray", + "srcPath": "./isarray", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "isdataview", + "srcPath": "./isdataview", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "istypedarray", + "srcPath": "./istypedarray", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "setelement", + "srcPath": "./setelement", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ecbf3d7359bb228ab566ebf167568a964461ba2e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createarray", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatearray.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..71ffbe12acf9abf5afa7b4004a238f05b3ea0a6d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createarray SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createarray PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1390f21e2b693ae54793a6df79727ba0ae8d1712 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/hello.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_array] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_array] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_array] + +static int g_diffValueFive = 5; +// OH_JSVM_CreateArray的样例方法 +static JSVM_Value CreateArray(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 创建一个空数组 + JSVM_Value array = nullptr; + JSVM_Status status = OH_JSVM_CreateArray(env, &array); + // 对创建的数组进行赋值 + for (int i = 0; i < g_diffValueFive; i++) { + JSVM_Value element; + OH_JSVM_CreateInt32(env, i, &element); + OH_JSVM_SetElement(env, array, i, element); + } + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateArray fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateArray success"); + } + return array; +} +// CreateArray注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateArray}, +}; +static JSVM_CallbackStruct *method = param; +// CreateArray方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createArray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + function testCreateArray() { + return createArray(); + } + testCreateArray(); +)JS"; +// [End oh_jsvm_create_array] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createarray", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9c1310bc793b05567be9c7fac3eb42b1f3f05531 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatearray.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1ad949a7278f94c63ab7464ea4cb5238cd4aded2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatearray.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d3596cb1a405d77456cf442ca186fdba00a6f806 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createarray", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..cab53b2b24f83534fda539b662f4de609ee1b85b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..16d656e84635c24df0425c220efe1ef0fe8378d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateArray', 0, async (done: Function) => { + console.log('uitest: TestCreateArray 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: TestCreateArray end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..011ac94ef81b0148a099cff950ec37a169c00137 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarray/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createarray_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..10bc7b2060f860234cd6886d1eef14e1c7391e4e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createarraywithlength", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatearraywithlength.so": "file:./src/main/cpp/types/libcreatearraywithlength" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e6511b92654ecd904ab690ff8fd2d0901ef7447c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createarraywithlength SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createarraywithlength PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f28007a2139ef8c82d7c20d87b37b6c6643ba2d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/hello.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_array_with_length] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_array_with_length] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_array_with_length] + +// OH_JSVM_CreateArrayWithLength的样例方法 +static JSVM_Value CreateArrayWithLength(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + JSVM_Value result = nullptr; + // 解析传递的参数OH_JSVM_GetCbInfo + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + // 获取传递的数组长度 + int32_t length; + OH_JSVM_GetValueInt32(env, argv[0], &length); + // 使用OH_JSVM_CreateArrayWithLength创建传递固定长度的数组 + JSVM_Status status = OH_JSVM_CreateArrayWithLength(env, length, &result); + if (status == JSVM_OK) { + // 给创建的数组设置值 + for (int32_t i = 0; i < length; i++) { + JSVM_Value value; + OH_JSVM_CreateInt32(env, i, &value); + OH_JSVM_SetElement(env, result, i, value); + } + OH_LOG_INFO(LOG_APP, "JSVM CreateArrayWithLength success"); + } else { + OH_LOG_ERROR(LOG_APP, "JSVM CreateArrayWithLength fail"); + } + return result; +} +// CreateArrayWithLength注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateArrayWithLength}, +}; +static JSVM_CallbackStruct *method = param; +// CreateArrayWithLength方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createArrayWithLength", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +let num = 7; +function testCreateArrayWithLength(num) { + return createArrayWithLength(num); +} +testCreateArrayWithLength(num); +)JS"; +// [End oh_jsvm_create_array_with_length] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createarraywithlength", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/types/libcreatearraywithlength/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/types/libcreatearraywithlength/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/types/libcreatearraywithlength/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/types/libcreatearraywithlength/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/types/libcreatearraywithlength/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e5fb57edaab34c9e0b819017f76db529cbee5ef0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/cpp/types/libcreatearraywithlength/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatearraywithlength.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/ets/createarraywithlengthability/CreatearraywithlengthAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/ets/createarraywithlengthability/CreatearraywithlengthAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..09df711e0fdab29e55de3779a00a9a643ace6c39 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/ets/createarraywithlengthability/CreatearraywithlengthAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatearraywithlengthAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..40855f42806c694c5836a866512d7ffdc5986596 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatearraywithlength.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(20) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e5012073cc487d4422617cd63e5c506a4da663ec --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createarraywithlength", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatearraywithlengthAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatearraywithlengthAbility", + "srcEntry": "./ets/createarraywithlengthability/CreatearraywithlengthAbility.ets", + "description": "$string:CreatearraywithlengthAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatearraywithlengthAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c887db6610373b5150c613963f11fe10367fc6e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatearraywithlengthAbility_desc", + "value": "description" + }, + { + "name": "CreatearraywithlengthAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1aa404cb806ad447263f75acd7ca3b5536b0371 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateArrayWithLength', 0, async (done: Function) => { + console.log('uitest: TestCreateArrayWithLength begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatearraywithlengthAbility' + } + 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('CreatearraywithlengthAbility'); + + 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: TestCreateArrayWithLength end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ead9aad5172eda12d872b3b802d4bb0f44efe05d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createarraywithlength/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createarraywithlength_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..50d711d2fb2ea6ea6bad9d23d9bb1c08965cad4a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createdataview", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatedataview.so": "file:./src/main/cpp/types/libcreatedataview" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2e3832115135f64116df63b8d1e44da6a7ad50aa --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createdataview SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createdataview PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..653b95f9cccc2abac28c1b1a056c6a045609c410 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/hello.cpp @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_dataview] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_dataview] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +static int g_diffValueFour = 4; +static int g_diffValueTwelve = 12; +// [EndExclude oh_jsvm_create_dataview] + +// OH_JSVM_CreateDataview的样例方法 +static JSVM_Value CreateDataView(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + JSVM_Value arrayBuffer = nullptr; + JSVM_Value result = nullptr; + // DataView的字节长度 + size_t byteLength = g_diffValueFour; + // 字节偏移量 + size_t byteOffset = g_diffValueTwelve; + // 获取回调函数的参数信息 + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 将参数转换为对象类型 + OH_JSVM_CoerceToObject(env, args[0], &arrayBuffer); + // 创建一个数据视图对象,并指定字节长度和字节偏移量 + JSVM_Status status = OH_JSVM_CreateDataview(env, byteLength, arrayBuffer, byteOffset, &result); + // 获取DataView的指针和长度信息 + uint8_t *data = nullptr; + size_t length = 0; + // 为DataView赋值 + for (size_t i = 0; i < length; i++) { + data[i] = static_cast(i + 1); + } + int32_t infoType; + OH_JSVM_GetValueInt32(env, args[1], &infoType); + size_t returnLength; + JSVM_Value returnArrayBuffer = nullptr; + size_t returnOffset; + enum InfoType { BYTE_LENGTHE, ARRAY_BUFFERE, BYTE_OFFSET }; + // 获取dataview信息 + OH_JSVM_GetDataviewInfo(env, result, &returnLength, (void **)&data, &returnArrayBuffer, &returnOffset); + JSVM_Value returnResult = nullptr; + switch (infoType) { + case BYTE_LENGTHE: + JSVM_Value len; + OH_JSVM_CreateInt32(env, returnLength, &len); + returnResult = len; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateDataView fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateDataView success, returnLength: %{public}d", returnLength); + } + break; + case ARRAY_BUFFERE: + bool isArraybuffer; + OH_JSVM_IsArraybuffer(env, returnArrayBuffer, &isArraybuffer); + JSVM_Value isArray; + OH_JSVM_GetBoolean(env, isArraybuffer, &isArray); + returnResult = isArray; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateDataView fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateDataView success, isArraybuffer: %{public}d", isArraybuffer); + } + break; + case BYTE_OFFSET: + JSVM_Value offset; + OH_JSVM_CreateInt32(env, returnOffset, &offset); + returnResult = offset; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateDataView fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateDataView success, returnOffset: %{public}d", returnOffset); + } + break; + default: + break; + } + return returnResult; +} +// CreateDataView注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateDataView}, +}; +static JSVM_CallbackStruct *method = param; +// CreateDataView方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createDataView", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let BYTE_LENGTH = 0; + createDataView(new ArrayBuffer(16), BYTE_LENGTH); + let IS_ARRAYBUFFER = 1; + createDataView(new ArrayBuffer(16), IS_ARRAYBUFFER); + let BYTE_OFFSET = 2; + createDataView(new ArrayBuffer(16), BYTE_OFFSET); +)JS"; +// [End oh_jsvm_create_dataview] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createdataview", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/types/libcreatedataview/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/types/libcreatedataview/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/types/libcreatedataview/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/types/libcreatedataview/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/types/libcreatedataview/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6d5f067aff3a11838b5078968cff7fa9424c21b9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/cpp/types/libcreatedataview/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatedataview.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/ets/createdataviewability/CreatedataviewAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/ets/createdataviewability/CreatedataviewAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..f3329ad686196f6eefa33f99635fd41378fbccfa --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/ets/createdataviewability/CreatedataviewAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatedataviewAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1e0e8375993999da608bf840da74763013719a73 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatedataview.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2edbae32ff9b376cf3eff9092eeab31a8388fb11 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createdataview", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatedataviewAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatedataviewAbility", + "srcEntry": "./ets/createdataviewability/CreatedataviewAbility.ets", + "description": "$string:CreatedataviewAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatedataviewAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..9587b21cf68d76103d5fca5a552319f1ee282ab4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatedataviewAbility_desc", + "value": "description" + }, + { + "name": "CreatedataviewAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..3d76e813752bb8f8d570e0b66a05a455a80ed36e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateDataview', 0, async (done: Function) => { + console.log('uitest: TestCreateDataview begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatedataviewAbility' + } + 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('CreatedataviewAbility'); + + 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: TestCreateDataview end'); + done(); + }) + }) +} + + diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ad513a5815838dcc8288933e7c5a32ade1f55c1b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createdataview/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createdataview_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0d18bda7bb6268636d0d5af32e2ba58d049fa8b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createtypedarray", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatetypedarray.so": "file:./src/main/cpp/types/libcreatetypedarray" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..68979498b9ab5fae85ab745295ba835168bc960a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createtypedarray SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createtypedarray PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfed8c75243a6e9ce04a449ade5aeee92f34686c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/hello.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_typedarray] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_typedarray] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_typedarray] + +// OH_JSVM_CreateTypedarray的样例方法 +static int g_diffValueThree = 3; +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 = 0; + OH_JSVM_GetValueInt32(env, args[0], &typeNum); + JSVM_TypedarrayType arrayType; + // 用于存储每个元素的大小 + size_t elementSize = 0; + // 转换为JSVM_TypedarrayType类型 + arrayType = static_cast(typeNum); + switch (typeNum) { + case JSVM_INT8_ARRAY: + case JSVM_UINT8_ARRAY: + case JSVM_UINT8_CLAMPED_ARRAY: + elementSize = sizeof(int8_t); + break; + case JSVM_INT16_ARRAY: + case JSVM_UINT16_ARRAY: + elementSize = sizeof(int16_t); + break; + case JSVM_INT32_ARRAY: + case JSVM_UINT32_ARRAY: + elementSize = sizeof(int32_t); + break; + case JSVM_FLOAT32_ARRAY: + elementSize = sizeof(float); + break; + case JSVM_FLOAT64_ARRAY: + elementSize = sizeof(double); + break; + case JSVM_BIGINT64_ARRAY: + case JSVM_BIGUINT64_ARRAY: + elementSize = sizeof(int64_t); + break; + default: + // 默认创建JSVM_INT8_ARRAY类型 + arrayType = JSVM_INT8_ARRAY; + elementSize = sizeof(int8_t); + break; + } + size_t length = g_diffValueThree; + JSVM_Value arrayBuffer = nullptr; + JSVM_Value typedArray = nullptr; + void *data; + // 创建一个ArrayBuffer + OH_JSVM_CreateArraybuffer(env, length * elementSize, (void **)&data, &arrayBuffer); + // 根据给定类型创建TypedArray + JSVM_Status status = OH_JSVM_CreateTypedarray(env, arrayType, length, arrayBuffer, 0, &typedArray); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateTypedArray fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateTypedArray success"); + } + return typedArray; +} +// CreateTypedArray注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateTypedArray}, +}; +static JSVM_CallbackStruct *method = param; +// CreateTypedArray方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createTypedArray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +const type = { + INT8_ARRAY: 0, + UINT8_ARRAY: 1, + UINT8_CLAMPED_ARRAY: 2, + INT16_ARRAY: 3, + UINT16_ARRAY: 4, + INT32_ARRAY: 5, + UINT32_ARRAY: 6, + FLOAT32_ARRAY: 7, + FLOAT64_ARRAY: 8, + BIGINT64_ARRAY: 9, + BIGUINT64_ARRAY: 10 +}; +createTypedArray(type.INT8_ARRAY); +createTypedArray(type.INT32_ARRAY); +)JS"; +// [End oh_jsvm_create_typedarray] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createtypedarray", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/types/libcreatetypedarray/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/types/libcreatetypedarray/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/types/libcreatetypedarray/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/types/libcreatetypedarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/types/libcreatetypedarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fcf561ebde3f8cd6fd89f8804bea1c78ef66d794 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/cpp/types/libcreatetypedarray/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatetypedarray.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/ets/createtypedarrayability/CreatetypedarrayAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/ets/createtypedarrayability/CreatetypedarrayAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..fe363aaad25f33395ab7ddd1428563e1ef41783b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/ets/createtypedarrayability/CreatetypedarrayAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatetypedarrayAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b46621430799c4971aa60d845e7c175168c2ac05 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatetypedarray.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1e02d76dcd39c30bab6136cdeb9647ae077ca043 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createtypedarray", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatetypedarrayAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatetypedarrayAbility", + "srcEntry": "./ets/createtypedarrayability/CreatetypedarrayAbility.ets", + "description": "$string:CreatetypedarrayAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatetypedarrayAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7082e7bcf9831a8456f795dbec1f0d0dccd5be3c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatetypedarrayAbility_desc", + "value": "description" + }, + { + "name": "CreatetypedarrayAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d7162797f56d0adc307fc889c8802d868e4c2928 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateTypedarray', 0, async (done: Function) => { + console.log('uitest: TestCreateTypedarray begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatetypedarrayAbility' + } + 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('CreatetypedarrayAbility'); + + 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: TestCreateTypedarray end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f1f3f7cf1d7400d748d671217257615cb0eeee64 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/createtypedarray/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createtypedarray_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1af11c0643f8ce41edf9455c8e50d23b4142026b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "deleteelement", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libdeleteelement.so": "file:./src/main/cpp/types/libdeleteelement" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c799add86de2ae052fe0d004be1e11d251da9325 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(deleteelement SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(deleteelement PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f06c3ab788556337814d4fa15498b69aea964ba2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/hello.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_delete_element] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_delete_element] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_delete_element] + +// OH_JSVM_DeleteElement的样例方法 +static JSVM_Value DeleteElement(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取要删除的元素的索引 + uint32_t index; + OH_JSVM_GetValueUint32(env, args[1], &index); + // 尝试删除请求索引位置的元素 + bool deleted = true; + JSVM_Status status = OH_JSVM_DeleteElement(env, args[0], index, &deleted); + // 将boolean结果转换为JSVM_Value并返回 + JSVM_Value result = nullptr; + OH_JSVM_GetBoolean(env, deleted, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM DeleteElement fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM DeleteElement: %{public}d", deleted); + } + return result; +} +// DeleteElement注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = DeleteElement}, +}; +static JSVM_CallbackStruct *method = param; +// DeleteElement方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"deleteElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +let arr = [10, 'hello', null, true]; +deleteElement(arr, 0); +)JS"; +// [End oh_jsvm_delete_element] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "deleteelement", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/types/libdeleteelement/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/types/libdeleteelement/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/types/libdeleteelement/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/types/libdeleteelement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/types/libdeleteelement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e8875dabd02d5877d484a65aa864f1e8c64eafbf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/cpp/types/libdeleteelement/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libdeleteelement.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/ets/deleteelementability/DeleteelementAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/ets/deleteelementability/DeleteelementAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..e313dd21ca2d4c9faa4c7916d68514edd23f910f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/ets/deleteelementability/DeleteelementAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class DeleteelementAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..c371ac6ee66b9c5062f9851b82d84555bb5107c3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libdeleteelement.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..163d1935ef8463aec41f7914e2bf8635ea1c1544 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "deleteelement", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "DeleteelementAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "DeleteelementAbility", + "srcEntry": "./ets/deleteelementability/DeleteelementAbility.ets", + "description": "$string:DeleteelementAbility_desc", + "icon": "$media:layered_image", + "label": "$string:DeleteelementAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4c5c74c7ace4333c47508db5b5a8b29081a72a69 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "DeleteelementAbility_desc", + "value": "description" + }, + { + "name": "DeleteelementAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..fb6563dcb69fdf90b9c490b6da252fc725a9e15a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testDeleteElement', 0, async (done: Function) => { + console.log('uitest: TestDeleteElement begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'DeleteelementAbility' + } + 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('DeleteelementAbility'); + + 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: TestDeleteElement end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cb29a6bf4e036ec4b0cd8e1751d85ec0593f27ea --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/deleteelement/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "deleteelement_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f2e5e8ee3ba4cc253a0b329dc57aabcb85ca0063 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getarraylength", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetarraylength.so": "file:./src/main/cpp/types/libgetarraylength" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..78e4ce4f5d7f8f30c42ee535de424cc6c693eb91 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getarraylength SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getarraylength PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c703610d0a6a03eb6531191e3b43928c91a38af --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/hello.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_array_length] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_array_length] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_array_length] + +// OH_JSVM_GetArrayLength的样例方法 +static JSVM_Value GetArrayLength(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + JSVM_Value result = nullptr; + uint32_t length; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 检查参数是否为数组 + bool isArray = false; + OH_JSVM_IsArray(env, args[0], &isArray); + if (!isArray) { + OH_LOG_INFO(LOG_APP, "JSVM Argument must be an array"); + return nullptr; + } + OH_JSVM_GetArrayLength(env, args[0], &length); + // 创建返回值 + OH_JSVM_CreateInt32(env, length, &result); + OH_LOG_INFO(LOG_APP, "JSVM length: %{public}d", length); + return result; +} +// GetArrayLength注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetArrayLength}, +}; +static JSVM_CallbackStruct *method = param; +// GetArrayLength方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getArrayLength", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +let data = [0, 1, 2, 3, 4, 5]; +getArrayLength(data); +)JS"; +// [End oh_jsvm_get_array_length] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getarraylength", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/types/libgetarraylength/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/types/libgetarraylength/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/types/libgetarraylength/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/types/libgetarraylength/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/types/libgetarraylength/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..671224c598d18ed69736bf42c1e165f37e7d2f60 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/cpp/types/libgetarraylength/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetarraylength.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/ets/getarraylengthability/GetarraylengthAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/ets/getarraylengthability/GetarraylengthAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..bc99f07f17f9358f39a2c25e45fd54b59e7e883f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/ets/getarraylengthability/GetarraylengthAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetarraylengthAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a6819482982f430379714e5b0ecd2ce2a7d26b46 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetarraylength.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f07b6deaa2dc5754cc072d49714ce2d241267552 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getarraylength", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetarraylengthAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetarraylengthAbility", + "srcEntry": "./ets/getarraylengthability/GetarraylengthAbility.ets", + "description": "$string:GetarraylengthAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetarraylengthAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..75b181bc89c28a638d006d3e6d2b5cd3aa8dad00 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetarraylengthAbility_desc", + "value": "description" + }, + { + "name": "GetarraylengthAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..510d3dc814f1964145dfb0ea34cc464bf5eda8d6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetArrayLength', 0, async (done: Function) => { + console.log('uitest: TestGetArrayLength begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetarraylengthAbility' + } + 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('GetarraylengthAbility'); + + 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: TestGetArrayLength end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..14ad84f0a98ce74ba773607e08faa67951b835d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getarraylength/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getarraylength_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d23005f4923ec5bd7dd37247ef04346dddc3f50e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getdataviewinfo", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetdataviewinfo.so": "file:./src/main/cpp/types/libgetdataviewinfo" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ede5023178e3e3610ce81d849183c5e086a36ec6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getdataviewinfo SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getdataviewinfo PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55257eb6547f94ad2537f2134927f9837f85fc55 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/hello.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_dataview_info] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_dataview_info] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_dataview_info] + +// OH_JSVM_GetDataviewInfo的样例方法 +static JSVM_Value GetDataViewInfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取并解析参数,第一个参数为需要获得的信息的DataView类型数据,第二个参数为需要获得的信息类型的枚举值 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 将第二个参数转为int32类型的数字 + int32_t infoType; + OH_JSVM_GetValueInt32(env, args[1], &infoType); + size_t byteLength; + void *data; + JSVM_Value arrayBuffer = nullptr; + size_t byteOffset; + // 定义枚举类型与ArkTS侧枚举类型infoType顺序含义一致 + enum InfoTypeEnum { BYTE_LENGTHE, ARRAY_BUFFERE, BYTE_OFFSET }; + // 获取dataview信息 + JSVM_Status status = OH_JSVM_GetDataviewInfo(env, args[0], &byteLength, &data, &arrayBuffer, &byteOffset); + JSVM_Value result = nullptr; + switch (infoType) { + case BYTE_LENGTHE: + // 返回查询DataView的长度 + JSVM_Value len; + OH_JSVM_CreateInt32(env, byteLength, &len); + result = len; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetDataViewInfo fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetDataViewInfo success, byteLength: %{public}d", byteLength); + } + break; + case ARRAY_BUFFERE: + // 判断DataView的Info里的arraybuffer是否为arraybuffer + bool isArrayBuffer; + OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer); + JSVM_Value isArray; + OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray); + result = isArray; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetDataViewInfo fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetDataViewInfo success, isArrayBuffer: %{public}d", isArrayBuffer); + } + break; + case BYTE_OFFSET: + // 返回查询DataView的偏移量 + JSVM_Value offset; + OH_JSVM_CreateInt32(env, byteOffset, &offset); + result = offset; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetDataViewInfo fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetDataViewInfo success, byteOffset: %{public}d", byteOffset); + } + break; + default: + break; + } + return result; +} +// GetDataViewInfo注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetDataViewInfo}, +}; +static JSVM_CallbackStruct *method = param; +// GetDataViewInfo方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getDataViewInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +// bytelength +getDataViewInfo(new DataView(new Int8Array([2, 5]).buffer), 0); +// is arraybuffer +let data = 'a'; +let isarraybuffer = 1; +getDataViewInfo(data, isarraybuffer); +// is arraybuffer +data = new DataView(new Int8Array([2, 5, 3]).buffer); +isarraybuffer = 1; +getDataViewInfo(data, isarraybuffer); +// byte_offset +data = new DataView(new Int8Array([2, 5, 3]).buffer); +isarraybuffer = 2; +getDataViewInfo(data, isarraybuffer); +)JS"; +// [End oh_jsvm_get_dataview_info] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getdataviewinfo", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/types/libgetdataviewinfo/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/types/libgetdataviewinfo/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/types/libgetdataviewinfo/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/types/libgetdataviewinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/types/libgetdataviewinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c878cd453d527f7c8e1f15791b756546d0172ef8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/cpp/types/libgetdataviewinfo/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetdataviewinfo.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/ets/getdataviewinfoability/GetdataviewinfoAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/ets/getdataviewinfoability/GetdataviewinfoAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..58476b71f029fd7faa451a57f7f6d4f122520cd7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/ets/getdataviewinfoability/GetdataviewinfoAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetdataviewinfoAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..86ffd8964ee22a6630f6349ceecea35242cf5a82 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetdataviewinfo.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9b95016bac911cc89f2999c9cd864225a4b586d7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getdataviewinfo", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetdataviewinfoAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetdataviewinfoAbility", + "srcEntry": "./ets/getdataviewinfoability/GetdataviewinfoAbility.ets", + "description": "$string:GetdataviewinfoAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetdataviewinfoAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..6439e36e102b3d13c771dbf5fa3f98155efa064a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetdataviewinfoAbility_desc", + "value": "description" + }, + { + "name": "GetdataviewinfoAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..adbf66523ca9120613a3c5bf5d0a24c86e605088 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetDataviewInfo', 0, async (done: Function) => { + console.log('uitest: TestGetDataviewInfo begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetdataviewinfoAbility' + } + 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('GetdataviewinfoAbility'); + + 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: TestGetDataviewInfo end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..714dbba2aee84bf9bc1b1c9413e21f128ea901ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getdataviewinfo/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getdataviewinfo_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..694ee70e9d045004a3e13e520e84aad2a22be518 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getelement", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetelement.so": "file:./src/main/cpp/types/libgetelement" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..dea780a7ac4ffe7f2ba4814b4380ad680e773901 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getelement SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getelement PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e75b7e408e08db72144b9ca993d0ae0fb1ac817 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/hello.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_element] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_element] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_element] + +// OH_JSVM_GetElement的样例方法 +static JSVM_Value GetElement(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取请求元素的索引值 + uint32_t index; + OH_JSVM_GetValueUint32(env, args[1], &index); + // 获取请求索引位置的元素值并存储在result中 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetElement(env, args[0], index, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetElement fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetElement success"); + } + return result; +} +// GetElement注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetElement}, +}; +static JSVM_CallbackStruct *method = param; +// GetElement方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +let arr = [10, 'hello', null, true]; +getElement(arr, 3); +)JS"; +// [End oh_jsvm_get_element] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getelement", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/types/libgetelement/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/types/libgetelement/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/types/libgetelement/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/types/libgetelement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/types/libgetelement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c617e658fbe33255dc8123af921c07be80f19a46 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/cpp/types/libgetelement/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetelement.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/ets/getelementability/GetelementAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/ets/getelementability/GetelementAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..01f96a258a9043681da8f3329690471b58002ca0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/ets/getelementability/GetelementAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetelementAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..cd716c1b4c3ffb950a6b5710a834767e766c3b20 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetelement.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8b4f09b2083801f8ad16fbc3ee050042a6a07a32 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getelement", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetelementAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetelementAbility", + "srcEntry": "./ets/getelementability/GetelementAbility.ets", + "description": "$string:GetelementAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetelementAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5652fa10a13d7d0e1a7465511c9fa5769966950a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetelementAbility_desc", + "value": "description" + }, + { + "name": "GetelementAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..5ce5cae884e0c8ef6a39128b7b3d8ee8d6cefbd4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetElement', 0, async (done: Function) => { + console.log('uitest: TestGetElement begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetelementAbility' + } + 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('GetelementAbility'); + + 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: TestGetElement end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..10c17dc86430c0ec714a39b4dbbcb37c2242f411 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/getelement/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getelement_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a7ec9c7d0a0bb4d596ee2bcec1f0fe9377806d14 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "gettypedarrayinfo", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgettypedarrayinfo.so": "file:./src/main/cpp/types/libgettypedarrayinfo" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..30a71205c4276e1743f58c44ae582658eb12438b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(gettypedarrayinfo SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(gettypedarrayinfo PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffe81877197e2795c4fb02a736f77464ca58eacc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/hello.cpp @@ -0,0 +1,227 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_typedarray_info] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_typedarray_info] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_typedarray_info] + +// OH_JSVM_GetTypedarrayInfo的样例方法 +static JSVM_Value GetTypedArrayInfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取并解析参数,第一个参数为需要获得的信息的TypedArray类型数据,第二个参数为需要获得的信息类型的枚举值 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + + // 将第二个参数转为int32类型便于比较 + int32_t infoTypeParam; + OH_JSVM_GetValueInt32(env, args[1], &infoTypeParam); + // 定义枚举类型与ArkTS侧枚举类型infoType顺序含义一致 + enum InfoType { INFO_TYPE, INFO_LENGTH, INFO_ARRAY_BUFFER, INFO_BYTE_OFFSET }; + void *data; + JSVM_TypedarrayType type; + size_t byteOffset; + size_t length; + JSVM_Value arrayBuffer = nullptr; + // 调用接口OH_JSVM_GetTypedarrayInfo获得TypedArray类型数据的信息 + JSVM_Status status = OH_JSVM_GetTypedarrayInfo(env, args[0], &type, &length, &data, &arrayBuffer, &byteOffset); + JSVM_Value result = nullptr; + // 根据属性名,返回TypedArray对应的属性值 + switch (infoTypeParam) { + case INFO_TYPE: + // 如果传入的参数是int8类型的TypedArray数据,它的类型(type)为JSVM_INT8_ARRAY + JSVM_Value int8_type; + OH_JSVM_GetBoolean(env, type == JSVM_INT8_ARRAY, &int8_type); + result = int8_type; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); + } else { + OH_LOG_INFO( + LOG_APP, "JSVM GetTypedArrayInfo success, JSVM_INT8_ARRAY: %{public}d", type == JSVM_INT8_ARRAY); + } + break; + case INFO_LENGTH: + // TypedArray中的元素数 + JSVM_Value jsvmLength; + OH_JSVM_CreateInt32(env, length, &jsvmLength); + result = jsvmLength; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success, length: %{public}d", length); + } + break; + case INFO_BYTE_OFFSET: + // TypedArray数组的第一个元素所在的基础原生数组中的字节偏移量 + JSVM_Value jsvmOffset; + OH_JSVM_CreateInt32(env, byteOffset, &jsvmOffset); + result = jsvmOffset; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success, byteOffset: %{public}d", byteOffset); + } + break; + case INFO_ARRAY_BUFFER: + // TypedArray下的ArrayBuffer + bool isArrayBuffer; + OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer); + JSVM_Value isArray; + OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray); + result = isArray; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success, isArrayBuffer: %{public}d", isArrayBuffer); + } + break; + default: + break; + } + return result; +} +// GetTypedArrayInfo注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetTypedArrayInfo}, +}; +static JSVM_CallbackStruct *method = param; +// GetTypedArrayInfo方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getTypedArrayInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +// is JSVM_INT8_ARRAY +getTypedArrayInfo(new Int8Array(3), 0); +// length +getTypedArrayInfo(new Int8Array(5), 1); +// is_arraybuffer +getTypedArrayInfo(new Int8Array(5), 2); +// byteoffset +getTypedArrayInfo(new Int8Array(1), 3); +)JS"; +// [End oh_jsvm_get_typedarray_info] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "gettypedarrayinfo", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/types/libgettypedarrayinfo/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/types/libgettypedarrayinfo/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/types/libgettypedarrayinfo/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/types/libgettypedarrayinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/types/libgettypedarrayinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..31378eaf067c060d81a1a28a7ad4f86901758b7e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/cpp/types/libgettypedarrayinfo/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgettypedarrayinfo.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/ets/gettypedarrayinfoability/GettypedarrayinfoAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/ets/gettypedarrayinfoability/GettypedarrayinfoAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..f58741051a64bb68a3afff446010e9a5ade00de6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/ets/gettypedarrayinfoability/GettypedarrayinfoAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GettypedarrayinfoAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..40315899072546e08670005700b3a7978544fadf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgettypedarrayinfo.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0916244dcb0e828544f2753c04e1f49e39d2a7b3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "gettypedarrayinfo", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GettypedarrayinfoAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GettypedarrayinfoAbility", + "srcEntry": "./ets/gettypedarrayinfoability/GettypedarrayinfoAbility.ets", + "description": "$string:GettypedarrayinfoAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GettypedarrayinfoAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7a7172a8284bc4cfe9a419bc28ce86e0a151040d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GettypedarrayinfoAbility_desc", + "value": "description" + }, + { + "name": "GettypedarrayinfoAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d45009355932531a3797557bc34d84cb497707b4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetTypedarrayInfo', 0, async (done: Function) => { + console.log('uitest: TestGetTypedarrayInfo begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GettypedarrayinfoAbility' + } + 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('GettypedarrayinfoAbility'); + + 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: TestGetTypedarrayInfo end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8c2115c22bbd9d6e18c58661612fe1c78802d82b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/gettypedarrayinfo/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "gettypedarrayinfo_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..146d18ff7079fd58c5a86be7564543eb5356c861 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "haselement", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libhaselement.so": "file:./src/main/cpp/types/libhaselement" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2090b5c9a0a3dac5c8630df57ab2b80b9abb5d35 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(haselement SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(haselement PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb477ae473206386201605e95a3e48c44c978e92 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/hello.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_has_element] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_has_element] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_has_element] + +// OH_JSVM_HasElement的样例方法 +static JSVM_Value HasElement(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取要判断的元素的索引 + uint32_t index; + OH_JSVM_GetValueUint32(env, args[1], &index); + // 判断指定索引位置的元素是否存在 + bool hasElement = true; + JSVM_Status status = OH_JSVM_HasElement(env, args[0], index, &hasElement); + // 将boolean结果转换为JSVM_Value并返回 + JSVM_Value result = nullptr; + OH_JSVM_GetBoolean(env, hasElement, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM hasElement fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM hasElement: %{public}d", hasElement); + } + return result; +} +// HasElement注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = HasElement}, +}; +static JSVM_CallbackStruct *method = param; +// HasElement方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"hasElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *srcCallNative = R"JS( +let arr = [10, 'hello', null, true]; +hasElement(arr, 0); +hasElement(arr, 4); +)JS"; +// [End oh_jsvm_has_element] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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, srcCallNative, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "haselement", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/types/libhaselement/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/types/libhaselement/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/types/libhaselement/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/types/libhaselement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/types/libhaselement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f096725231cdb06d929c0bcc97a701ee6256a426 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/cpp/types/libhaselement/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libhaselement.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/ets/haselementability/HaselementAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/ets/haselementability/HaselementAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..3a3d098ca79cc2b86cbf06e08de9c60f804838d3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/ets/haselementability/HaselementAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class HaselementAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0eddb902d3228fda65fc849e47e2b0658d87d67b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libhaselement.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c4df04c786d808b21afb3e62f8cd2f9f825916ef --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "haselement", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "HaselementAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "HaselementAbility", + "srcEntry": "./ets/haselementability/HaselementAbility.ets", + "description": "$string:HaselementAbility_desc", + "icon": "$media:layered_image", + "label": "$string:HaselementAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0f181f5fe6eeddadbc3b1723a8d8af09c02dbbb4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "HaselementAbility_desc", + "value": "description" + }, + { + "name": "HaselementAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..9907a4623b0b88af42148e0423873eb48439a474 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testHasElement', 0, async (done: Function) => { + console.log('uitest: TestHasElement begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'HaselementAbility' + } + 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('HaselementAbility'); + + 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: TestHasElement end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8c4f4995a88c645a73aa6d962052410fc41ff0b1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/haselement/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "haselement_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ab38673af064673e8170babdf75fc4bc30d18add --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "isarray", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libisarray.so": "file:./src/main/cpp/types/libisarray" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..deb9a0bd2494ea5a3e3695c5495c43f83fde6a52 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(isarray SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(isarray PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6572e09cf4de241e9bd0b808944fe31248fdbc6d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_is_array] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_is_array] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_is_array] + +// OH_JSVM_IsArray的样例方法 +static JSVM_Value IsArray(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + bool result = false; + JSVM_Status status = OH_JSVM_IsArray(env, args[0], &result); + JSVM_Value returnValue = nullptr; + OH_JSVM_GetBoolean(env, result, &returnValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM IsArray fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM IsArray success, IsArray: %{public}d", result); + } + return returnValue; +} +// IsArray注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsArray}, +}; +static JSVM_CallbackStruct *method = param; +// IsArray方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isArray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *srcCallNative = R"JS( +let data = [1, 2, 3, 4, 5]; +isArray(data); +)JS"; +// [End oh_jsvm_is_array] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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, srcCallNative, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "isarray", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/types/libisarray/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/types/libisarray/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/types/libisarray/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/types/libisarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/types/libisarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5565a148eb7bcdc4ce91d120bf30667f35b1cb33 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/cpp/types/libisarray/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libisarray.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/ets/isarrayability/IsarrayAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/ets/isarrayability/IsarrayAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..adb10e1e6632bc4677ea3463c8e83dfc2eef1119 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/ets/isarrayability/IsarrayAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IsarrayAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b5d5708e83b0cf87c48e21f23e025061cfeb4080 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libisarray.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..74147792b47299446b24f7afa84108500d8ceda1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isarray", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IsarrayAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IsarrayAbility", + "srcEntry": "./ets/isarrayability/IsarrayAbility.ets", + "description": "$string:IsarrayAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IsarrayAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8eff575f5e411a2621640e678b1b8d7b9997c974 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IsarrayAbility_desc", + "value": "description" + }, + { + "name": "IsarrayAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d47f60af1d97f9499d9a23770bcbb5f5dfaf2866 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsArray', 0, async (done: Function) => { + console.log('uitest: TestIsArray begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IsarrayAbility' + } + 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('IsarrayAbility'); + + 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: TestIsArray end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f91518e902aa843c67f0dcb6e14020107f95160f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isarray/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isarray_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8459e56f4a117495420eff5e70310922750c120f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "isdataview", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libisdataview.so": "file:./src/main/cpp/types/libisdataview" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2ffaa819a24146f6e47a3c48a4819d53fa6ec9e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(isdataview SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(isdataview PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaeab782df7cd19fa41f6bb0ed464859ad038c2c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/hello.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_is_dataview] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_is_dataview] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_is_dataview] + +// OH_JSVM_IsDataview的样例方法 +static JSVM_Value IsDataView(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 调用OH_JSVM_IsDataview接口判断给定入参是否为DataView数据。 + bool result = false; + JSVM_Status status = OH_JSVM_IsDataview(env, args[0], &result); + JSVM_Value isDateView = nullptr; + OH_JSVM_GetBoolean(env, result, &isDateView); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM IsDataView fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM IsDataView: %{public}d", result); + } + return isDateView; +} +// IsDataView注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsDataView}, +}; +static JSVM_CallbackStruct *method = param; +// IsDataView方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isDataView", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *srcCallNative = R"JS( +let buffer = new ArrayBuffer(16); +let dataView = new DataView(buffer); +isDataView(dataView); +)JS"; +// [End oh_jsvm_is_dataview] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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, srcCallNative, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "isdataview", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/types/libisdataview/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/types/libisdataview/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/types/libisdataview/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/types/libisdataview/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/types/libisdataview/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..22941059791f75b5914ed7c6c81cb2e43a0b7703 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/cpp/types/libisdataview/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libisdataview.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/ets/isdataviewability/IsdataviewAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/ets/isdataviewability/IsdataviewAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..460ad01950eb67637051cc9d942e4974b843434a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/ets/isdataviewability/IsdataviewAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IsdataviewAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1f2d7cee549d730be6b3000f1e5c55f603a4f8e4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libisdataview.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..bad4d685353ba412d38899c0653bb31abd2dc375 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isdataview", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IsdataviewAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IsdataviewAbility", + "srcEntry": "./ets/isdataviewability/IsdataviewAbility.ets", + "description": "$string:IsdataviewAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IsdataviewAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..df24b674f0d4bc4bfff7105092f8cdb1d68e4e8f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IsdataviewAbility_desc", + "value": "description" + }, + { + "name": "IsdataviewAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ee817cc5d0f42c134b7183ab56cb3c62895fc02a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsDataview', 0, async (done: Function) => { + console.log('uitest: TestIsDataview begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IsdataviewAbility' + } + 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('IsdataviewAbility'); + + 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: TestIsDataview end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4bbbdf0034cca4db08f5308f9226346a2c31d7e7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/isdataview/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isdataview_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..107934447f3455488c22307c2b344e4bacc154c5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "istypedarray", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libistypedarray.so": "file:./src/main/cpp/types/libistypedarray" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f13755fac9b39122337f71f3b9e9ab108969db3f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(istypedarray SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(istypedarray PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54e342f3f676640638f17ea51c6648cb4d9ec2cd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/hello.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_is_typedarray] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_is_typedarray] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_is_typedarray] + +// OH_JSVM_IsTypedarray的样例方法 +static JSVM_Value IsTypedarray(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + bool result = false; + JSVM_Status status = OH_JSVM_IsTypedarray(env, args[0], &result); + JSVM_Value isTypedArray = nullptr; + OH_JSVM_GetBoolean(env, result, &isTypedArray); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM IsTypedarray fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM IsTypedarray: %{public}d", result); + } + return isTypedArray; +} +// IsTypedarray注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsTypedarray}, +}; +static JSVM_CallbackStruct *method = param; +// IsTypedarray方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isTypedarray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *srcCallNative = R"JS( +isTypedarray(new Uint16Array([1, 2, 3, 4])); +)JS"; +// [End oh_jsvm_is_typedarray] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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, srcCallNative, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "istypedarray", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/types/libistypedarray/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/types/libistypedarray/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/types/libistypedarray/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/types/libistypedarray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/types/libistypedarray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..435fcd9766e8d6c992a137ae95aa623691a8ef70 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/cpp/types/libistypedarray/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libistypedarray.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/ets/istypedarrayability/IstypedarrayAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/ets/istypedarrayability/IstypedarrayAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..586d03f9607ab22bb262f1886331771b1b2f76de --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/ets/istypedarrayability/IstypedarrayAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IstypedarrayAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7d3f74513ab35bf976041d9b979469e7c57ec8db --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libistypedarray.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..875e3d13a10e243635b8a8b2579295bcd25806f4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "istypedarray", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IstypedarrayAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IstypedarrayAbility", + "srcEntry": "./ets/istypedarrayability/IstypedarrayAbility.ets", + "description": "$string:IstypedarrayAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IstypedarrayAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..01682acd0d217ba94ef609c41e3cbfcd76cfcdb0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IstypedarrayAbility_desc", + "value": "description" + }, + { + "name": "IstypedarrayAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..9fee65d72c48461b14d9e0919587be329749940e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsTypedarray', 0, async (done: Function) => { + console.log('uitest: TestIsTypedarray begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IstypedarrayAbility' + } + 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('IstypedarrayAbility'); + + 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: TestIsTypedarray end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..eb1d73b6885f663a53ce8b8b8b46fd03ecdc4862 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/istypedarray/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "istypedarray_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..2d5caf585dd75ef6f81b5f92cad3a6ef074940cb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/ohosTest.md @@ -0,0 +1,21 @@ +# JsvmAboutArray 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 创建一个新的 JavaScript 数组对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建一个指定长度的 JavaScript 数组对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 在现有的 ArrayBuffer 上创建一个 JavaScript DataView 对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 在现有的 ArrayBuffer 上创建一个 JavaScript TypedArray 对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 尝试删除给定对象的指定索引处的元素 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 返回 Array 对象的长度 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取 Dataview 对象的信息 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定对象指定索引处的元素 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取 TypedArray(类型化数组)对象的信息 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 若给定对象的指定索引处拥有属性,获取该元素 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断一个 JavaScript 对象是否为 Array 类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断一个 JavaScript 对象是否为 Dataview 类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断一个 JavaScript 对象是否为 Typedarray 类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 在给定对象的指定索引处设置元素 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/screenshots/JsvmAboutArray_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/screenshots/JsvmAboutArray_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/screenshots/JsvmAboutArray_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/screenshots/JsvmAboutArray_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/screenshots/JsvmAboutArray_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/screenshots/JsvmAboutArray_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0cef4be6a39c859633e6e9fb86d65dedd0b15062 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "setelement", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libsetelement.so": "file:./src/main/cpp/types/libsetelement" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..98f3e8a2dd8f4ed35db24c2f943a070b88746487 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(setelement SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(setelement PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..018496ac65c2f4e9712bffc0b9be83c31faad7f6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/hello.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_set_element] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_set_element] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_set_element] + +// OH_JSVM_SetElement的样例方法 +static int g_diffValueThree = 3; +static JSVM_Value SetElement(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = g_diffValueThree; + JSVM_Value args[3] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + int32_t index = 0; + OH_JSVM_GetValueInt32(env, args[1], &index); + JSVM_Status status = OH_JSVM_SetElement(env, args[0], index, args[2]); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM SetElement fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM SetElement success"); + } + return args[0]; +} +// SetElement注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = SetElement}, +}; +static JSVM_CallbackStruct *method = param; +// SetElement方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"setElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +setElement(3); +)JS"; +// [End oh_jsvm_set_element] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "setelement", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/types/libsetelement/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/types/libsetelement/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/types/libsetelement/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/types/libsetelement/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/types/libsetelement/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2d7971a31d17f8fa9d80ec4b4e7271440b4e1d6c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/cpp/types/libsetelement/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libsetelement.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..34ff1332d8688956ad3e94bb64eb039e10494d83 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libsetelement.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/ets/setelementability/SetelementAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/ets/setelementability/SetelementAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..53e8ce06e1f98ee1bb9723ace968e68e0d108e2f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/ets/setelementability/SetelementAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class SetelementAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a23090f1df7ff2e203287ed69684066a24e53378 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "setelement", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "SetelementAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "SetelementAbility", + "srcEntry": "./ets/setelementability/SetelementAbility.ets", + "description": "$string:SetelementAbility_desc", + "icon": "$media:layered_image", + "label": "$string:SetelementAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7a34b15ba771be4f8f1ebda67de443575ffbe559 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "SetelementAbility_desc", + "value": "description" + }, + { + "name": "SetelementAbility_label", + "value": "JsvmAboutArray" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..83482007cec3dfca312fcec41f3ceddd68328034 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testSetElement', 0, async (done: Function) => { + console.log('uitest: TestSetElement begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'SetelementAbility' + } + 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('SetelementAbility'); + + 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: TestSetElement end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ff765a5fcb3b16b2d0946bf4e67098fe14a4cbbd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArray/setelement/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "setelement_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dd76d3797807a5142e608864f78170b3e8279a7f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutarraybuffer", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..be2a53f0f30e1a958840aa56368e753625a225c4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutArraybuffer" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/README.md new file mode 100644 index 0000000000000000000000000000000000000000..836f01efe8d57847f2be5fb05995589a3117cde2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/README.md @@ -0,0 +1,130 @@ +# ArkTS使用JSVM-API接口进行ArrayBuffer相关开发 + +### 介绍 + +ArrayBuffer 是 JavaScript 中的一种数据类型,用于表示通用的、固定长度的原始二进制数据缓冲区。它提供了一种在 JavaScript 中有效地表示和操作原始二进制数据的方式。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行ArrayBuffer相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-arraybuffer.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :----------------------------------------------------------------------: | :----------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createarraybuffer/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatearraybuffer + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createarraybufferability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getarraybufferinfo/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +isarraybuffer/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libisarraybuffer + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── isarraybufferability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +isdetachedarraybuffer/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libisdetachedarraybuffer + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── isdetachedarraybufferability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..31b2359a98092cfe200a0ebda7ba9c8508dbe351 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/build-profile.json5 @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getarraybufferinfo", + "srcPath": "./getarraybufferinfo", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "isarraybuffer", + "srcPath": "./isarraybuffer", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "isdetachedarraybuffer", + "srcPath": "./isdetachedarraybuffer", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createarraybuffer", + "srcPath": "./createarraybuffer", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c7ef987202fc8582b86bcd47a6364430ffd98965 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createarraybuffer", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatearraybuffer.so": "file:./src/main/cpp/types/libcreatearraybuffer" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..8b6769decfe8713815d178a6812994c7fb2c27e4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsFive) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createarraybuffer SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createarraybuffer PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..344adf7827fa9c45443f5117f8f574f1bf43b641 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/hello.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_arraybuffer] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_create_arraybuffer] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_arraybuffer] + +// OH_JSVM_CreateArraybuffer的样例方法 +static JSVM_Value CreateArraybuffer(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + JSVM_Value result = nullptr; + // 解析传递的参数 + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + int32_t value = 0; + size_t length = 0; + OH_JSVM_GetValueInt32(env, argv[0], &value); + length = size_t(value); + void *data; + // 创建一个新的ArrayBuffer + JSVM_Status status = OH_JSVM_CreateArraybuffer(env, length, &data, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateArraybuffer: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateArraybuffer: success"); + OH_LOG_INFO(LOG_APP, "JSVM ArrayBuffer length: %{public}d", length); + } + // 返回创建好的ArrayBuffer + return result; +} +// CreateArraybuffer注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateArraybuffer}, +}; +static JSVM_CallbackStruct *method = param; +// CreateArraybuffer方法别名,供TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createArraybuffer", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +createArraybuffer(8); +)JS"; +// [End oh_jsvm_create_arraybuffer] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createarraybuffer", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/types/libcreatearraybuffer/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/types/libcreatearraybuffer/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/types/libcreatearraybuffer/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/types/libcreatearraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/types/libcreatearraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ebcd307a4ab1d378da76653b36f60652d85056fd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/cpp/types/libcreatearraybuffer/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatearraybuffer.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/ets/createarraybufferability/CreatearraybufferAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/ets/createarraybufferability/CreatearraybufferAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..e0536f2782f2f85dbd1792be08621674b04fae29 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/ets/createarraybufferability/CreatearraybufferAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatearraybufferAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..9ddd7c89c0e72e03a1287b05f3d42a768f460e19 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatearraybuffer.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..15287f77110ad7b7f2ffd8433f70112f9070da3b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createarraybuffer", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatearraybufferAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatearraybufferAbility", + "srcEntry": "./ets/createarraybufferability/CreatearraybufferAbility.ets", + "description": "$string:CreatearraybufferAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatearraybufferAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..6f0babdfaa8a16430d195e5356db65e23b390595 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatearraybufferAbility_desc", + "value": "description" + }, + { + "name": "CreatearraybufferAbility_label", + "value": "JsvmAboutArraybuffer" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d3343ee782217f949aa569dfdda9d05c6b8416df --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateArraybuffer', 0, async (done: Function) => { + console.log('uitest: TestCreateArraybuffer begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatearraybufferAbility' + } + 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('CreatearraybufferAbility'); + + 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: TestCreateArraybuffer end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..04231245952f78b5b6849ada6359e55f9385dbba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/createarraybuffer/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createarraybuffer_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7525bbe93f9ecfe71129ddb2430ae1f66bc8c984 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getarraybufferinfo", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetarraybufferinfo.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..d05b01552c3bceb4ef0d057bdaec9c8d48b8311b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsFive) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getarraybufferinfo SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getarraybufferinfo PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97834ceb65c12bb34defe62a376e17ae299d2e06 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/hello.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_arraybuffer_info] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_arraybuffer_info] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_arraybuffer_info] + +// OH_JSVM_GetArraybufferInfo的样例方法 +static JSVM_Value GetArraybufferInfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + // 解析传递的参数 + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 检查参数是否为ArrayBuffer + bool isArrayBuffer = false; + OH_JSVM_IsArraybuffer(env, args[0], &isArrayBuffer); + if (!isArrayBuffer) { + OH_LOG_ERROR(LOG_APP, "JSVM GetArraybufferInfo isArrayBuffer:false"); + } + void *data; + size_t byteLength; + // 获取ArrayBuffer的底层数据缓冲区和长度 + JSVM_Status status = OH_JSVM_GetArraybufferInfo(env, args[0], &data, &byteLength); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetArraybufferInfo: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetArraybufferInfo: success"); + } + return args[0]; +} +// GetArraybufferInfo注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetArraybufferInfo}, +}; +static JSVM_CallbackStruct *method = param; +// GetArraybufferInfo方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getArraybufferInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +getArraybufferInfo(new ArrayBuffer(10)); +)JS"; +// [End oh_jsvm_get_arraybuffer_info] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getarraybufferinfo", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..36110c3808103016e9effaeb7439bca1a9243bce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetarraybufferinfo.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a654430123e7f95b50afc77f66bfcb5ab0b0265d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetarraybufferinfo.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..15f1044e02ccef92222a02af7966d8ad5a93ba28 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getarraybufferinfo", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0878d320041f9ad39bbc03670d7c88cde4e68170 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutArraybuffer" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..904159f6e6a6c448a338d13afdd5db2fc8a471e2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetArraybufferInfo', 0, async (done: Function) => { + console.log('uitest: TestGetArraybufferInfo 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: TestGetArraybufferInfo end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..90eb73f5732e3725c2c869a2b3ace5ed956872cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/getarraybufferinfo/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getarraybufferinfo_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..accae1ce0d3daf7972bb9aefc203bb5be14f9cdb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "isarraybuffer", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libisarraybuffer.so": "file:./src/main/cpp/types/libisarraybuffer" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..38a29ee6fcbd07ef289de040479fc95570057168 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsFive) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(isarraybuffer SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(isarraybuffer PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95abdfe935990df6d815edc4d43802914df937cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_is_arraybuffer] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_is_arraybuffer] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_is_arraybuffer] + +// OH_JSVM_IsArraybuffer的样例方法 +static JSVM_Value IsArrayBuffer(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 调用OH_JSVM_IsArraybuffer接口判断给定入参是否为ArrayBuffer数据 + bool isArrayBuffer = false; + JSVM_Status status = OH_JSVM_IsArraybuffer(env, args[0], &isArrayBuffer); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM IsArrayBuffer: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM IsArrayBuffer: success"); + OH_LOG_INFO(LOG_APP, "JSVM IsArrayBuffer: %{public}d", isArrayBuffer); + } + JSVM_Value boolean = nullptr; + OH_JSVM_GetBoolean(env, isArrayBuffer, &boolean); + return boolean; +} +// IsArrayBuffer注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsArrayBuffer}, +}; +static JSVM_CallbackStruct *method = param; +// IsArrayBuffer方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isArrayBuffer", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +isArrayBuffer(new ArrayBuffer(8)); +)JS"; +// [End oh_jsvm_is_arraybuffer] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "isarraybuffer", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/types/libisarraybuffer/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/types/libisarraybuffer/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/types/libisarraybuffer/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/types/libisarraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/types/libisarraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0d0f89ea18ff6ee64f07aec91f938c033bec8fed --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/cpp/types/libisarraybuffer/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libisarraybuffer.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/ets/isarraybufferability/IsarraybufferAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/ets/isarraybufferability/IsarraybufferAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..92e42cd7cdb59af4bb30a1962d61751c41d36698 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/ets/isarraybufferability/IsarraybufferAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IsarraybufferAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..92986da4b555627e2f2ffa3db4ed06a05664d561 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libisarraybuffer.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f9f4a0a746ba8480722ec340c563e157d951e1e1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isarraybuffer", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IsarraybufferAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IsarraybufferAbility", + "srcEntry": "./ets/isarraybufferability/IsarraybufferAbility.ets", + "description": "$string:IsarraybufferAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IsarraybufferAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..dd0e15f88bd7aa2cc9c13cae6ae0eb8bce8cc01c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IsarraybufferAbility_desc", + "value": "description" + }, + { + "name": "IsarraybufferAbility_label", + "value": "JsvmAboutArraybuffer" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..5a25784b7ea3e9e556b7584f91195e3c2ad075bc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsArraybuffer', 0, async (done: Function) => { + console.log('uitest: TestIsArraybuffer begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IsarraybufferAbility' + } + 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('IsarraybufferAbility'); + + 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: TestIsArraybuffer end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cfae646a7ddf4bc5387a3e78069eeb001dc06048 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isarraybuffer/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isarraybuffer_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e08f1de466d01b1232f7a5b9afc0f4aad59ae74f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "isdetachedarraybuffer", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libisdetachedarraybuffer.so": "file:./src/main/cpp/types/libisdetachedarraybuffer" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..d61b43a65ce7171c8cb448e8ce190161d9de8af0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsFive) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(isdetachedarraybuffer SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(isdetachedarraybuffer PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6287ddb23fa11e49ff3570305fb0a827bf3a47e1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/hello.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_is_detached_arraybuffer] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_is_detached_arraybuffer] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_is_detached_arraybuffer] + +// OH_JSVM_DetachArraybuffer、OH_JSVM_IsDetachedArraybuffer的样例方法 +static JSVM_Value DetachArraybuffer(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value arraybuffer = args[0]; + JSVM_Status status = OH_JSVM_DetachArraybuffer(env, arraybuffer); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM DetachArraybuffer: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM DetachArraybuffer: success"); + } + return arraybuffer; +} +static JSVM_Value IsDetachedArraybuffer(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value arraybuffer = args[0]; + OH_JSVM_DetachArraybuffer(env, arraybuffer); + bool result = false; + JSVM_Status status = OH_JSVM_IsDetachedArraybuffer(env, arraybuffer, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM IsDetachedArraybuffer: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM IsDetachedArraybuffer: success"); + OH_LOG_INFO(LOG_APP, "JSVM IsArrayBuffer: %{public}d", result); + } + JSVM_Value isDetached = nullptr; + OH_JSVM_GetBoolean(env, result, &isDetached); + return isDetached; +} +// DetachArraybuffer、IsDetachedArraybuffer注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = DetachArraybuffer}, + {.data = nullptr, .callback = IsDetachedArraybuffer}, +}; +static JSVM_CallbackStruct *method = param; +// DetachArraybuffer、IsDetachedArraybuffer方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"detachArraybuffer", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + {"isDetachedArraybuffer", nullptr, method+1, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( +let arrayBuffer = new ArrayBuffer(10); +detachArraybuffer(arrayBuffer); +isDetachedArraybuffer(arrayBuffer); +)JS"; +// [End oh_jsvm_is_detached_arraybuffer] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "isdetachedarraybuffer", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/types/libisdetachedarraybuffer/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/types/libisdetachedarraybuffer/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/types/libisdetachedarraybuffer/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/types/libisdetachedarraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/types/libisdetachedarraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4d43e7fc610f4238570c34ad582e3eb8f0b03c26 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/cpp/types/libisdetachedarraybuffer/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libisdetachedarraybuffer.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/ets/isdetachedarraybufferability/IsdetachedarraybufferAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/ets/isdetachedarraybufferability/IsdetachedarraybufferAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..115ea549e99b92aeff3d07c08b2dce99e88feb94 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/ets/isdetachedarraybufferability/IsdetachedarraybufferAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IsdetachedarraybufferAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..d169c6584ae801bf5c6c236af6d73e4905cc76c8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libisdetachedarraybuffer.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e8d00d61168ae6193c7bb285db1e6016c51cfd24 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isdetachedarraybuffer", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IsdetachedarraybufferAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IsdetachedarraybufferAbility", + "srcEntry": "./ets/isdetachedarraybufferability/IsdetachedarraybufferAbility.ets", + "description": "$string:IsdetachedarraybufferAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IsdetachedarraybufferAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..350c9cfa5c3ea940e624b4cbb171acbadd0e9978 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IsdetachedarraybufferAbility_desc", + "value": "description" + }, + { + "name": "IsdetachedarraybufferAbility_label", + "value": "JsvmAboutArraybuffer" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..8a80ca20263e3eb4764dba57b259142bd2c6448b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsDetachedArraybuffer', 0, async (done: Function) => { + console.log('uitest: TestIsDetachedArraybuffer begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IsdetachedarraybufferAbility' + } + 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('IsdetachedarraybufferAbility'); + + 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: TestIsDetachedArraybuffer end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7140dc89464009bc639587be2b12c38b7ff850d7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/isdetachedarraybuffer/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isdetachedarraybuffer_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..ff22838956b2b80c8bd4ac3fdbe11896a360b069 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/ohosTest.md @@ -0,0 +1,11 @@ +# JsvmAboutArraybuffer 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| --------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 创建一个指定大小的 ArrayBuffer 对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 检索 ArrayBuffer 的底层数据缓冲区及其长度 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断一个 JavaScript 对象是否为 ArrayBuffer 类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 检查给定的 ArrayBuffer 是否已被分离(detached) | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/screenshots/JsvmAboutArraybuffer_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/screenshots/JsvmAboutArraybuffer_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/screenshots/JsvmAboutArraybuffer_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/screenshots/JsvmAboutArraybuffer_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/screenshots/JsvmAboutArraybuffer_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutArraybuffer/screenshots/JsvmAboutArraybuffer_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b2d9a927300e1b9654f0c831c3963d169f9b55f4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutbigint", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e240af11babe1d1c2bd9bfec77bd42aa290249d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutBigint" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ab754c73bfc3bbc5e5fac331064f1316bf509b41 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/README.md @@ -0,0 +1,168 @@ +# ArkTS使用JSVM-API接口操作bigint类型值 + +### 介绍 + +BigInt是JavaScript中用于表示任意精度整数的数据类型,它能够处理比Number类型更大范围的整数值。通过JSVM-API提供的接口,可以在JSVM模块中创建、获取和操作BigInt类型值,从而实现与BigInt相关的功能扩展。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口操作bigint类型值](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-bigint.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-----------------------------------------------------------------: | :-----------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createbigintint64/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatebigintint64 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createbigintint64ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createbigintuint64/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatebigintuint64 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createbigintuint64ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createbigintwords/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatebigintwords + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createbigintwordsability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluebigintint64/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvaluebigintint64 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvaluebigintint64ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluebigintuint64/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvaluebigintuint64 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvaluebigintuint64ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluebigintwords/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..92a2bd8b7de8c7b2825702b5cebaaa154c1a4e73 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/build-profile.json5 @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getvaluebigintwords", + "srcPath": "./getvaluebigintwords", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createbigintwords", + "srcPath": "./createbigintwords", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createbigintuint64", + "srcPath": "./createbigintuint64", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvaluebigintuint64", + "srcPath": "./getvaluebigintuint64", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createbigintint64", + "srcPath": "./createbigintint64", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvaluebigintint64", + "srcPath": "./getvaluebigintint64", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..24310f1b2b1e768ea28d954eb7196ea9453db20b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createbigintint64", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatebigintint64.so": "file:./src/main/cpp/types/libcreatebigintint64" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..66e21840c890e442f5ba2739a7f79d737ced670f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createbigintint64 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createbigintint64 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10a8ab8a937d6ee10b6fff4387245da9e3b9f5c7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/hello.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_bigint_int64] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_create_bigint_int64] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_bigint_int64] + +// 声明int64_t的变量value +static int64_t g_testValueDemo = -5555555555555555555; +// OH_JSVM_CreateBigintInt64的样例方法 +static JSVM_Value CreateBigintInt64(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value returnValue = nullptr; + JSVM_Status status = OH_JSVM_CreateBigintInt64(env, g_testValueDemo, &returnValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateBigintInt64 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateBigintInt64 success"); + } + return returnValue; +} +// CreateBigintInt64注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateBigintInt64}, +}; +static JSVM_CallbackStruct *method = param; +// CreateBigintInt64方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createBigintInt64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createBigintInt64())JS"; +// [End oh_jsvm_create_bigint_int64] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createbigintint64", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/types/libcreatebigintint64/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/types/libcreatebigintint64/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/types/libcreatebigintint64/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/types/libcreatebigintint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/types/libcreatebigintint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1039157d9bc26f86d8435c75d5222b0fcf0b0259 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/cpp/types/libcreatebigintint64/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatebigintint64.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/ets/createbigintint64ability/Createbigintint64Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/ets/createbigintint64ability/Createbigintint64Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..54b0d127ef4a54661213636bac00954acdeffc36 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/ets/createbigintint64ability/Createbigintint64Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Createbigintint64Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..6fbeb81a911c1039779a2040b44a815dc05bf485 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatebigintint64.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2e973d7deaa435b2887bd54f6a08f70238089de0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createbigintint64", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createbigintint64Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createbigintint64Ability", + "srcEntry": "./ets/createbigintint64ability/Createbigintint64Ability.ets", + "description": "$string:Createbigintint64Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createbigintint64Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3734dc3222ce6c96f24ae078f5862e6ad9d8f779 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createbigintint64Ability_desc", + "value": "description" + }, + { + "name": "Createbigintint64Ability_label", + "value": "JsvmAboutBigint" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a9a5af9fceb5a8d1a347a480e23848425f1b2da5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateBigintInt64', 0, async (done: Function) => { + console.log('uitest: TestCreateBigintInt64 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createbigintint64Ability' + } + 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('Createbigintint64Ability'); + + 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: TestCreateBigintInt64 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d4b914b9dc2df6fd2d327786cc2958d2888d32e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintint64/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createbigintint64_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..adc44991579386c3cb6b9ba5d63974e1c622d5b1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createbigintuint64", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatebigintuint64.so": "file:./src/main/cpp/types/libcreatebigintuint64" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f920b2ca4548c04c8c3b3933e6293bb813199818 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsSix) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createbigintuint64 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createbigintuint64 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7bb92d66bc337882f02ac088cb573576947b30c7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/hello.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_bigint_uint64] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_create_bigint_uint64] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_bigint_uint64] + +// 声明uint64_t的变量value +static uint64_t g_testValue = 5555555555555555555; +// OH_JSVM_CreateBigintUint64的样例方法 +static JSVM_Value CreateBigintUint64(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 将value转化为JSVM_Value类型返回 + JSVM_Value returnValue = nullptr; + JSVM_Status status = OH_JSVM_CreateBigintUint64(env, g_testValue, &returnValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateBigintUint64 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateBigintUint64 success"); + } + return returnValue; +} +// CreateBigintUint64注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateBigintUint64}, +}; +static JSVM_CallbackStruct *method = param; +// CreateBigintUint64方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createBigintUint64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(createBigintUint64())JS"; +// [End oh_jsvm_create_bigint_uint64] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createbigintuint64", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/types/libcreatebigintuint64/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/types/libcreatebigintuint64/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/types/libcreatebigintuint64/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/types/libcreatebigintuint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/types/libcreatebigintuint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..60767790c0819694f74ab3982847b01a7a644cf4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/cpp/types/libcreatebigintuint64/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatebigintuint64.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/ets/createbigintuint64ability/Createbigintuint64Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/ets/createbigintuint64ability/Createbigintuint64Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..2cd000ac840c04a5b9b1099fb2bab6e1e41faa9f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/ets/createbigintuint64ability/Createbigintuint64Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Createbigintuint64Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..86c3436cf21be7ba9627b50755f212cbf87fe0f6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatebigintuint64.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5d90e5c19e5e9fb462ec2100a9efcac824513876 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createbigintuint64", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createbigintuint64Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createbigintuint64Ability", + "srcEntry": "./ets/createbigintuint64ability/Createbigintuint64Ability.ets", + "description": "$string:Createbigintuint64Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createbigintuint64Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..453a15bb9e1c0ac47a35e5b9cc9792b9fda7040f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createbigintuint64Ability_desc", + "value": "description" + }, + { + "name": "Createbigintuint64Ability_label", + "value": "JsvmAboutBigint" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..15db66924a5133dd09ceece4c0487f14a5c23aa4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateBigintUint64', 0, async (done: Function) => { + console.log('uitest: TestCreateBigintUint64 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createbigintuint64Ability' + } + 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('Createbigintuint64Ability'); + + 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: TestCreateBigintUint64 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..22b19149cf0b50a73f58a5c3bee9d9a90524f9e1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintuint64/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createbigintuint64_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a2d75a437afb730ed06e4b121178be7adbbed147 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createbigintwords", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatebigintwords.so": "file:./src/main/cpp/types/libcreatebigintwords" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..cfcf91bc154ff9c87c63d56bd95c50c414f6d904 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsSix) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createbigintwords SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createbigintwords PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4854cc07297c90d3ced5b0d285e962f36492b516 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_bigint_words] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_create_bigint_words] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_bigint_words] + +// OH_JSVM_CreateBigintWords的样例方法 +static int g_diffValueThree = 3; +static JSVM_Value CreateBigintWords(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 使用OH_JSVM_CreateBigintWords接口创建一个BigInt对象 + int signBit = 0; + size_t wordCount = g_diffValueThree; + uint64_t words[] = {12ULL, 34ULL, 56ULL}; + JSVM_Value returnValue = nullptr; + JSVM_Status status = OH_JSVM_CreateBigintWords(env, signBit, wordCount, words, &returnValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateBigintWords fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateBigintWords success"); + } + return returnValue; +} +// CreateBigintWords注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateBigintWords}, +}; +static JSVM_CallbackStruct *method = param; +// CreateBigintWords方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createBigintWords", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createBigintWords())JS"; +// [EndExclude oh_jsvm_create_bigint_words] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createbigintwords", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/types/libcreatebigintwords/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/types/libcreatebigintwords/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/types/libcreatebigintwords/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/types/libcreatebigintwords/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/types/libcreatebigintwords/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8139562209b9d493ce7760dd91ff70b132b79d2e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/cpp/types/libcreatebigintwords/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatebigintwords.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/ets/createbigintwordsability/CreatebigintwordsAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/ets/createbigintwordsability/CreatebigintwordsAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6882d87f8202345606860f555520945d969cad43 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/ets/createbigintwordsability/CreatebigintwordsAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatebigintwordsAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..eaa34dcf8d1ff63354a96ec0bfb600e4bcbee14e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatebigintwords.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..70ae7e137f4991b1398d6bf583bfa0ea271726b7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createbigintwords", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatebigintwordsAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatebigintwordsAbility", + "srcEntry": "./ets/createbigintwordsability/CreatebigintwordsAbility.ets", + "description": "$string:CreatebigintwordsAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatebigintwordsAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..19ddee92e5a864360528dc3300b60855b3778482 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatebigintwordsAbility_desc", + "value": "description" + }, + { + "name": "CreatebigintwordsAbility_label", + "value": "JsvmAboutBigint" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..6660d833f35a307b325f6e20d56fa639ccd2e961 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateBigintWords', 0, async (done: Function) => { + console.log('uitest: TestCreateBigintWords begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatebigintwordsAbility' + } + 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('CreatebigintwordsAbility'); + + 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: TestCreateBigintWords end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ef673a26fea2be7143d4717d055f21edad06aefc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/createbigintwords/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createbigintwords_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..61f2056b38af04d3f48cd813ee695f76c9efdc50 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluebigintint64", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluebigintint64.so": "file:./src/main/cpp/types/libgetvaluebigintint64" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..3b70a939a1f7b3df894ebdd2f2dd52eb089fd962 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvaluebigintint64 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluebigintint64 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea88c04961ead1a5aa59a6ea8cb3d57382b9190b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_bigint_int64] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_value_bigint_int64] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_bigint_int64] + +// OH_JSVM_GetValueBigintInt64的样例方法 +static JSVM_Value GetBigintInt64(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 从传入的参数中提取64位整数的BigInt数据 + int64_t value = 0; + bool lossLess = false; + OH_JSVM_GetValueBigintInt64(env, args[0], &value, &lossLess); + // 判断从JS侧获取bigint是否为无损转换,如果不是抛出异常 + if (!lossLess) { + OH_JSVM_ThrowError(env, nullptr, "BigInt values have no lossless converted"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetBigintInt64 success:%{public}d", lossLess); + } + JSVM_Value returnValue = nullptr; + OH_JSVM_CreateBigintInt64(env, value, &returnValue); + return returnValue; +} +// GetBigintInt64注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetBigintInt64}, +}; +static JSVM_CallbackStruct *method = param; +// GetBigintInt64方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getBigintInt64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getBigintInt64(BigInt(-5555555555555555)))JS"; +// [End oh_jsvm_get_value_bigint_int64] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluebigintint64", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/types/libgetvaluebigintint64/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/types/libgetvaluebigintint64/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/types/libgetvaluebigintint64/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/types/libgetvaluebigintint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/types/libgetvaluebigintint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..76d755dfeb0ccf12a2f54f4081650fd84f48ae59 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/cpp/types/libgetvaluebigintint64/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluebigintint64.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/ets/getvaluebigintint64ability/Getvaluebigintint64Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/ets/getvaluebigintint64ability/Getvaluebigintint64Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..b0db1a92dfd4c59075d48dad88c47822a90a33ac --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/ets/getvaluebigintint64ability/Getvaluebigintint64Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Getvaluebigintint64Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8cfef82a52050021990beb70acc21a4db51dc5f3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluebigintint64.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..16def6fbe4dc2203d4d185ad572f0000dcac7282 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebigintint64", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Getvaluebigintint64Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvaluebigintint64Ability", + "srcEntry": "./ets/getvaluebigintint64ability/Getvaluebigintint64Ability.ets", + "description": "$string:Getvaluebigintint64Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvaluebigintint64Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b1099263f5385dcd226ca5c14b79b859eeb18f9d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvaluebigintint64Ability_desc", + "value": "description" + }, + { + "name": "Getvaluebigintint64Ability_label", + "value": "JsvmAboutBigint" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e520467a8011eef5f8f518b414dae83a4876d14 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueBigintInt64', 0, async (done: Function) => { + console.log('uitest: TestGetValueBigintInt64 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvaluebigintint64Ability' + } + 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('Getvaluebigintint64Ability'); + + 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: TestGetValueBigintInt64 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8fa9a60e6c948875a04fae9475f5752cdbc8049e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintint64/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebigintint64_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e13b7234716a9d4bb027426386bf017d10ded607 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluebigintuint64", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluebigintuint64.so": "file:./src/main/cpp/types/libgetvaluebigintuint64" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1380e03dafa237bb4f214e27334f009dff5e9288 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvaluebigintuint64 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluebigintuint64 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a264a73f72a24b005a0ae9dfb95ed9fba8e9d132 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_bigint_uint64] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_value_bigint_uint64] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_bigint_uint64] + +// OH_JSVM_GetValueBigintUint64的样例方法 +static JSVM_Value GetValueBigintUint64(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 从参数值中获取BigInt的数值 + uint64_t value = 0; + bool lossLess = false; + OH_JSVM_GetValueBigintUint64(env, args[0], &value, &lossLess); + // 判断从JS侧获取bigint是否为无损转换,如果不是抛出异常 + if (!lossLess) { + OH_JSVM_ThrowError(env, nullptr, "BigInt values have no lossless converted"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueBigintUint64 success:%{public}d", lossLess); + } + JSVM_Value returnValue = nullptr; + OH_JSVM_CreateBigintUint64(env, value, &returnValue); + return returnValue; +} +// GetValueBigintUint64注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueBigintUint64}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueBigintUint64方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueBigintUint64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getValueBigintUint64(BigInt(5555555555555555)))JS"; +// [End oh_jsvm_get_value_bigint_uint64] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluebigintuint64", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/types/libgetvaluebigintuint64/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/types/libgetvaluebigintuint64/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/types/libgetvaluebigintuint64/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/types/libgetvaluebigintuint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/types/libgetvaluebigintuint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5a4b69ee6d31ec3a1fad83dfd991c0bbdf7eb39b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/cpp/types/libgetvaluebigintuint64/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluebigintuint64.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/ets/getvaluebigintuint64ability/Getvaluebigintuint64Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/ets/getvaluebigintuint64ability/Getvaluebigintuint64Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..aa335815c85927f1ed5ff4d97abc9b7d50774c0a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/ets/getvaluebigintuint64ability/Getvaluebigintuint64Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Getvaluebigintuint64Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..990bc7b00cfc1d98d64bca13b373e8a3f5a0bba0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluebigintuint64.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e96cb223039ab1b07dcb2239751d1c789f90f663 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebigintuint64", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Getvaluebigintuint64Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvaluebigintuint64Ability", + "srcEntry": "./ets/getvaluebigintuint64ability/Getvaluebigintuint64Ability.ets", + "description": "$string:Getvaluebigintuint64Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvaluebigintuint64Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..fadfd15d2aa8cfad945f72d49f11a9312a5bbb1d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvaluebigintuint64Ability_desc", + "value": "description" + }, + { + "name": "Getvaluebigintuint64Ability_label", + "value": "JsvmAboutBigint" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a066752b3f60114454fedd9f7574b7763b331d5c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueBigintUint64', 0, async (done: Function) => { + console.log('uitest: TestGetValueBigintUint64 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvaluebigintuint64Ability' + } + 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('Getvaluebigintuint64Ability'); + + 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: TestGetValueBigintUint64 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e2f7820c6da73fbf5ac79923892c222066cc2935 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintuint64/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebigintuint64_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..adcecf185db4c006b827b4a5c4c08e3ee9b89057 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluebigintwords", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluebigintwords.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f8d4d03e59d8e5654328b57acf2df3ee93d1fa9a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsSix) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getvaluebigintwords SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluebigintwords PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5b42f2b6025bc9ff4abb092afef0efeafa6daee --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/hello.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_bigint_words] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +// [StartExclude oh_jsvm_get_value_bigint_words] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" +#define MAX_MALLOC_SIZE 0x800000 + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_bigint_words] + +// OH_JSVM_GetValueBigintWords的样例方法 +static JSVM_Value GetValueBigintWords(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + int signBit = 0; + size_t wordCount = 0; + uint64_t* words{nullptr}; + // 调用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 > MAX_MALLOC_SIZE) { + OH_LOG_ERROR(LOG_APP, "Invalid wordCount: %{public}zu", wordCount); + return nullptr; + } + words = (uint64_t*)malloc(wordCount*sizeof(uint64_t)); + if (words == nullptr) { + OH_LOG_ERROR(LOG_APP, "OH_JSVM_GetValueBigintWords malloc failed."); + return nullptr; + } + // 调用OH_JSVM_GetValueBigintWords接口获取传入bigInt相关信息,如:signBit传入bigInt正负信息 + status = OH_JSVM_GetValueBigintWords(env, args[0], &signBit, &wordCount, words); + free(words); + words = nullptr; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "OH_JSVM_GetValueBigintWords fail, status:%{public}d.", status); + } else { + OH_LOG_INFO(LOG_APP, "OH_JSVM_GetValueBigintWords signBit: %{public}d.", signBit); + } + // 将符号位转化为int类型传出去 + JSVM_Value returnValue = nullptr; + OH_JSVM_CreateInt32(env, signBit, &returnValue); + return returnValue; +} +// GetValueBigintWords注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueBigintWords}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueBigintWords方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueBigintWords", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getValueBigintWords(BigInt(5555555555555555)))JS"; +// [End oh_jsvm_get_value_bigint_words] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluebigintwords", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9049a01a9cb6083e5b5b7b845982931f9838ea83 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluebigintwords.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..2a4f046ebf4965155fc7be527b181bec1ed2589d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluebigintwords.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8860dbc775e0fac14fda8b300e85caf57104d884 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebigintwords", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4954adb219c50aba12827b99827c9425f889cc36 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutBigint" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..6c136a7eeb25a3435da3db9e140eb1fa96f39d95 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueBigintWords', 0, async (done: Function) => { + console.log('uitest: TestGetValueBigintWords 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: TestGetValueBigintWords end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0cb4f1d1cd505b260cd10cf91037b5769dcaa034 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/getvaluebigintwords/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebigintwords_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..fc40b5f95e9033430d0a94b39c7048cff096b542 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/ohosTest.md @@ -0,0 +1,13 @@ +# JsvmAboutBigint 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 将C int64_t类型的值转换为JavaScript BigInt类型 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 将C uint64_t类型的值转换为JavaScript BigInt类型 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 将一组无符号64位字转换为单个BigInt值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 返回给定JavaScript BigInt的C int64_t基础类型等价值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 返回给定JavaScript BigInt的C uint64_t基础类型等价值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 将单个BigInt值转换为一个符号位、一个64位的小端数组和该数组的长度 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/screenshots/JsvmAboutBigint_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/screenshots/JsvmAboutBigint_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/screenshots/JsvmAboutBigint_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/screenshots/JsvmAboutBigint_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/screenshots/JsvmAboutBigint_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutBigint/screenshots/JsvmAboutBigint_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..40f0a262dabb04294260bc5faa395dc7d7801327 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutclass", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ebd7615caef10da12ea2874af839b03f6c502cfd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutClass" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d1094899a55e4ccbbdf79f388e1a6b3903c394e5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/README.md @@ -0,0 +1,111 @@ +# ArkTS使用JSVM接口进行class相关开发 + +### 介绍 + +使用JSVM-API接口进行class相关开发,处理JavaScript中的类,例如定义类、构造实例等。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM进行class相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-class.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :----------------------------------------------------------------: | :----------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +defineclass/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libdefineclass + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── defineclassability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +newinstance/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +removewrap/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libremovewrap + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── removewrapability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a8d4557b0c0ac7126a10da3df8988f5689ec9fde --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/build-profile.json5 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "newinstance", + "srcPath": "./newinstance", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "defineclass", + "srcPath": "./defineclass", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "removewrap", + "srcPath": "./removewrap", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..64b19291d0d4531eae4520314c8a3f4a2194526e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "defineclass", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libdefineclass.so": "file:./src/main/cpp/types/libdefineclass" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..59145d6c51db7e0716905c6495a076b2085c3796 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutClass) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(defineclass SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(defineclass PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bdc26d598360a481c3220a1ef2bd057459a9947 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/hello.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [Start oh_jsvm_defineclass] +#include +// [StartExclude oh_jsvm_defineclass] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_defineclass] + +JSVM_Value CreateInstance(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value newTarget; + // 获取构造函数的new.target值 + JSVM_CALL(OH_JSVM_GetNewTarget(env, info, &newTarget)); + OH_LOG_INFO(LOG_APP, "Create Instance"); + OH_LOG_INFO(LOG_APP, + "NAPI MyObject::New %{public}s", + newTarget != nullptr ? "newTarget != nullptr" : "newTarget == nullptr"); + JSVM_Value jsObject = nullptr; + JSVM_CALL(OH_JSVM_CreateObject(env, &jsObject)); + JSVM_Value jsName = nullptr; + JSVM_CALL(OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &jsName)); + JSVM_Value jsValue = nullptr; + JSVM_CALL(OH_JSVM_CreateStringUtf8(env, "lilei", JSVM_AUTO_LENGTH, &jsValue)); + JSVM_CALL(OH_JSVM_SetProperty(env, jsObject, jsName, jsValue)); + return jsObject; +} + +std::string ToString(JSVM_Env env, JSVM_Value val) +{ + JSVM_Value jsonString; + JSVM_CALL(OH_JSVM_JsonStringify(env, val, &jsonString)); + size_t totalLen = 0; + JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, nullptr, 0, &totalLen)); + size_t needLen = totalLen + 1; + char* buff = new char[needLen]; + std::memset(buff, 0, needLen); + JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, buff, needLen, &totalLen)); + std::string str(buff); + delete[] buff; + return str; +} + +// 封装c++中的自定义数据结构 +JSVM_Value DefineClass(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_CallbackStruct param; + param.data = nullptr; + param.callback = CreateInstance; + JSVM_Value cons; + // 用于在JavaScript中定义一个类 + JSVM_CALL(OH_JSVM_DefineClass(env, "MyObject", JSVM_AUTO_LENGTH, ¶m, 0, nullptr, &cons)); + JSVM_Value instanceValue = nullptr; + // 作为class的构造函数调用 + JSVM_CALL(OH_JSVM_NewInstance(env, cons, 0, nullptr, &instanceValue)); + std::string str = ToString(env, instanceValue); + OH_LOG_INFO(LOG_APP, "NewInstance:%{public}s", str.c_str()); + + // 作为普通的函数调用 + JSVM_Value global; + JSVM_CALL(OH_JSVM_GetGlobal(env, &global)); + JSVM_Value key; + JSVM_CALL(OH_JSVM_CreateStringUtf8(env, "Constructor", JSVM_AUTO_LENGTH, &key)); + JSVM_CALL(OH_JSVM_SetProperty(env, global, key, cons)); + JSVM_Value result; + JSVM_CALL(OH_JSVM_CallFunction(env, global, cons, 0, nullptr, &result)); + std::string buf = ToString(env, result); + OH_LOG_INFO(LOG_APP, "NewInstance:%{public}s", buf.c_str()); + return nullptr; +} + +// 注册DefineClass的方法 +JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = DefineClass}, +}; + +static JSVM_CallbackStruct *method = param; + +// DefineClass方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"defineClass", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// [End oh_jsvm_defineclass] + +const char *SRC_CALL_NATIVE = R"JS( defineClass(); )JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "defineclass", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/types/libdefineclass/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/types/libdefineclass/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/types/libdefineclass/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/types/libdefineclass/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/types/libdefineclass/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d658bf92ef03e40b877c930ef079e72aa75ea19a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/cpp/types/libdefineclass/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libdefineclass.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/ets/defineclassability/DefineclassAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/ets/defineclassability/DefineclassAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..98602fa428d1bd9830b82c0f06c5942b43acc19b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/ets/defineclassability/DefineclassAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class DefineclassAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..f7a286b6ee5b59c4a86764aa1431b20c201bd620 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libdefineclass.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..98a4883aad4035e3812d8e10c2e15c0407936a4e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "defineclass", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "DefineclassAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "DefineclassAbility", + "srcEntry": "./ets/defineclassability/DefineclassAbility.ets", + "description": "$string:DefineclassAbility_desc", + "icon": "$media:layered_image", + "label": "$string:DefineclassAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..509b4f88ca1f00073e6c18ad7d32f307fe84da73 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "DefineclassAbility_desc", + "value": "description" + }, + { + "name": "DefineclassAbility_label", + "value": "JsvmAboutClass" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d40d3e9afaf1b95dc52b2805f544be7d3f2e274 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testDefineClass', 0, async (done: Function) => { + console.log('uitest: TestDefineClass begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'DefineclassAbility' + } + 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('DefineclassAbility'); + + 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: TestDefineClass end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2ca64655460a20cc39572d29383900c4aa5de568 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/defineclass/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "defineclass_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8cdef9667a752ffebcd70dbcac5d4be537888f92 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "newinstance", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libnewinstance.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..bbd46b2a34e8ecb0f9e7663bae2497f0ec4aaca5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutClass) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(newinstance SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(newinstance PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f41d968610e2349d54cc063bf856657639c9420 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/hello.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [Start oh_jsvm_newinstance] +#include +// [StartExclude oh_jsvm_newinstance] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_newinstance] + +std::string ToString(JSVM_Env env, JSVM_Value val) +{ + JSVM_Value jsonString; + JSVM_CALL(OH_JSVM_JsonStringify(env, val, &jsonString)); + size_t totalLen = 0; + JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, nullptr, 0, &totalLen)); + size_t needLen = totalLen + 1; + char* buff = new char[needLen]; + std::memset(buff, 0, needLen); + JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, buff, needLen, &totalLen)); + std::string str(buff); + delete[] buff; + return str; +} + +// OH_JSVM_NewInstance的样例方法 +static JSVM_Value NewInstance(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); + JSVM_Value result = nullptr; + // 调用OH_JSVM_NewInstance接口,实例化一个对象,将这个对象返回 + JSVM_CALL(OH_JSVM_NewInstance(env, args[0], 1, &args[1], &result)); + std::string str = ToString(env, result); + OH_LOG_INFO(LOG_APP, "NewInstance:%{public}s", str.c_str()); + return nullptr; +} + +// 通过给定的构造函数,构建一个实例。 +// NewInstance注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = NewInstance}, +}; + +static JSVM_CallbackStruct *method = param; + +// NewInstance方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"newInstance", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// [End oh_jsvm_newinstance] + +const char *SRC_CALL_NATIVE = R"JS( function Fruit(name) { this.name = name; } newInstance(Fruit, "apple"); )JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "newinstance", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..87c3109518b6cb2a890e93d937fbbe7efabfbb3d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libnewinstance.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..072eebfa6bba87ea372e1010d76a81828de797c3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libnewinstance.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c62425b62debf462c73fc0f106028a9f08518429 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "newinstance", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..959097630f503847464408d0662c82e98c5c5d68 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutClass" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bddbc1102b828d91134a356ded775f0e4be6e2ca --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testNewInstance', 0, async (done: Function) => { + console.log('uitest: TestNewInstance 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: TestNewInstance end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9ce5eb8c27891f052aaa5644c07b0fc613f80c21 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/newinstance/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "newinstance_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..837470505b005591e1a46d1de3e94e427417736c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/ohosTest.md @@ -0,0 +1,10 @@ +# JsvmAboutClass 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +|------------------------------------| ------------ |------------------------| ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 在JavaScript中定义一个类,并与对应的C类进行封装和交互 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 通过给定的构造函数,构建一个实例 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 解包先前封装在JavaScript对象中的原生实例并释放封装 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9c974bf11b567f6e91b07a613720549dbbae2e60 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "removewrap", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libremovewrap.so": "file:./src/main/cpp/types/libremovewrap" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a0d5309512c2f0e1f1792630f7bfca105a4aef00 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutClass) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(removewrap SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(removewrap PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c26356e44e2d02cea8637d9d99dce8ea0ecb7b53 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/hello.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [Start oh_jsvm_removewrap] +#include +// [StartExclude oh_jsvm_removewrap] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_removewrap] + +// OH_JSVM_GetNewTarget、OH_JSVM_DefineClass、OH_JSVM_Wrap、OH_JSVM_Unwrap、OH_JSVM_RemoveWrap的样例方法 + +// 自定义类结构体Object +struct Object { + std::string name; + int32_t age; +}; + +// 定义一个回调函数 +static void DerefItem(JSVM_Env env, void *data, void *hint) +{ + OH_LOG_INFO(LOG_APP, "JSVM deref_item"); + (void)hint; +} + +static JSVM_Value WrapObject(JSVM_Env env, JSVM_CallbackInfo info) +{ + OH_LOG_INFO(LOG_APP, "JSVM wrap"); + Object obj; + // 设置Object属性 + obj.name = "lilei"; + const int adultAge = 18; + obj.age = adultAge; + Object *objPointer = &obj; + // 获取回调信息中的参数数量和将要被封装的值 + size_t argc = 1; + JSVM_Value toWrap = nullptr; + JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, &toWrap, nullptr, nullptr)); + // OH_JSVM_Wrap将自定义结构Object进行封装 + JSVM_CALL(OH_JSVM_Wrap(env, toWrap, reinterpret_cast(objPointer), DerefItem, NULL, NULL)); + Object *data; + // OH_JSVM_UnWrap解包先前封装在JavaScript对象中的原生实例 + JSVM_CALL(OH_JSVM_Unwrap(env, toWrap, reinterpret_cast(&data))); + OH_LOG_INFO(LOG_APP, "JSVM name: %{public}s", data->name.c_str()); + OH_LOG_INFO(LOG_APP, "JSVM age: %{public}d", data->age); + return nullptr; +} + +static JSVM_Value RemoveWrap(JSVM_Env env, JSVM_CallbackInfo info) +{ + OH_LOG_INFO(LOG_APP, "JSVM removeWrap"); + Object obj; + // 设置Object属性 + obj.name = "lilei"; + const int adultAge = 18; + obj.age = adultAge; + Object *objPointer = &obj; + // 获取回调信息中的参数数量和将要被封装的值 + size_t argc = 1; + JSVM_Value toWrap = nullptr; + JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, &toWrap, nullptr, nullptr)); + // 将自定义结构Object封装 + JSVM_CALL(OH_JSVM_Wrap(env, toWrap, reinterpret_cast(objPointer), DerefItem, NULL, NULL)); + Object *data; + // 解包先前封装的object,并移除封装 + JSVM_CALL(OH_JSVM_RemoveWrap(env, toWrap, reinterpret_cast(&objPointer))); + // 检查是否已被移除 + JSVM_Status status = OH_JSVM_Unwrap(env, toWrap, reinterpret_cast(&data)); + if (status != JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_RemoveWrap success"); + } + return nullptr; +} + +// WrapObject、RemoveWrap注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = WrapObject}, + {.data = nullptr, .callback = RemoveWrap}, +}; +static JSVM_CallbackStruct *method = param; +// WrapObject、RemoveWrap方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"wrapObject", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + {"removeWrap", nullptr, method+1, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// [End oh_jsvm_removewrap] + +const char *SRC_CALL_NATIVE = R"JS( class Obj {}; wrapObject(new Obj()); removeWrap(new Obj()); )JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "removewrap", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/types/libremovewrap/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/types/libremovewrap/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/types/libremovewrap/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/types/libremovewrap/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/types/libremovewrap/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8d69688ce47d4ef098f0d47ca1bb8f6d1584e0d8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/cpp/types/libremovewrap/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libremovewrap.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..59959343b37fdb3aa2f1b5030668936a15084af8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libremovewrap.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/ets/removewrapability/RemovewrapAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/ets/removewrapability/RemovewrapAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..2baa653836d5fd3c52f98a4fdeb3f298199db803 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/ets/removewrapability/RemovewrapAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class RemovewrapAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dba5b8b160fc7b964f5890ae2f11524d78437ae1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "removewrap", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "RemovewrapAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "RemovewrapAbility", + "srcEntry": "./ets/removewrapability/RemovewrapAbility.ets", + "description": "$string:RemovewrapAbility_desc", + "icon": "$media:layered_image", + "label": "$string:RemovewrapAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..01554b806ecf19aed36028fe154c5d683164758e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "RemovewrapAbility_desc", + "value": "description" + }, + { + "name": "RemovewrapAbility_label", + "value": "JsvmAboutClass" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..519a8b1a4e10fc86fab23741ad178a6f09a91b94 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testRemoveWrap', 0, async (done: Function) => { + console.log('uitest: TestRemoveWrap begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'RemovewrapAbility' + } + 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('RemovewrapAbility'); + + 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: TestRemoveWrap end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..89b6b4602da68ca839178509bc7563aedba07b48 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/removewrap/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "removewrap_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/screenshots/JsvmAboutClass_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/screenshots/JsvmAboutClass_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/screenshots/JsvmAboutClass_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/screenshots/JsvmAboutClass_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/screenshots/JsvmAboutClass_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutClass/screenshots/JsvmAboutClass_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7dc0586640be430bbbb644dd4fc0557fd19f5154 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutdate", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5afae2877f941aec936a287edf67c384033c2305 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutDate" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/README.md new file mode 100644 index 0000000000000000000000000000000000000000..54eeb9e486c7f70200146de1fa8694ef6cf0c18d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/README.md @@ -0,0 +1,111 @@ +# ArkTS使用JSVM-API接口进行Date相关开发 + +### 介绍 + +JSVM-API中date相关接口用于处理JavaScript Date对象,并在JSVM模块和JavaScript代码之间进行日期数据的转换和处理。这对于在JSVM模块中处理时间和日期相关逻辑非常有用。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行Date相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-date.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :---------------------------------------------------------------: | :---------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createdate/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getdatevalue/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetdatevalue + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── getdatevalueability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +isdate/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libisdate + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── isdateability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a6a79cc37893ef4cef6634e6e3660ab63b390806 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/build-profile.json5 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "createdate", + "srcPath": "./createdate", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getdatevalue", + "srcPath": "./getdatevalue", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "isdate", + "srcPath": "./isdate", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..85cea6f4b8dec3ddc571321ca342aa074b65b9ea --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createdate", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatedate.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..554f98f9c40d53adbbdf5818c4e871ae452dc02b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutDate) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createdate SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createdate PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1c8cd52821fcbb307f798e380bc7bfd4423fdd2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/hello.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_date] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +// [StartExclude oh_jsvm_create_date] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_date] + +// OH_JSVM_CreateDate的样例方法 +static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 通过c接口获取Unix纪元以来经过的秒数,并转化为毫秒数为单位 + double value = static_cast(time(nullptr) * 1000); + // 调用OH_JSVM_CreateDate接口将double值转换成表示日期时间的JavaScript值返回出去 + JSVM_Value returnValue = nullptr; + + JSVM_CALL(OH_JSVM_CreateDate(env, value, &returnValue)); + + bool isDate; + JSVM_CALL(OH_JSVM_IsDate(env, returnValue, &isDate)); + if (!isDate) { + OH_LOG_ERROR(LOG_APP, "JSVM IsDate fail"); + return returnValue; + } + + value = 0; + JSVM_CALL(OH_JSVM_GetDateValue(env, returnValue, &value)); + + uint64_t time = static_cast(value) / 1000; + char *date = ctime(reinterpret_cast(&time)); + OH_LOG_INFO(LOG_APP, "JSVM CreateDate success:%{public}s", date); + + return returnValue; +} + +// CreateDate注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateDate}, +}; +static JSVM_CallbackStruct *method = param; +// CreateDate方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createDate", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(createDate())JS"; +// [End oh_jsvm_create_date] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createdate", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..98100150ef16e9ab5a3ebb8644457b46a73a550c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatedate.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..49fca32a24ba6b8a7f8887b6bd35f1a2bff2a38b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatedate.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..340f4e73eda4084c17e156de6beb8e1651038b4d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createdate", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..460a69fe459d08ab8e811cd7c83ba0a6cee66419 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutDate" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..09099662a5ee92c957f944f4d0893af8388d88f2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateDate', 0, async (done: Function) => { + console.log('uitest: TestCreateDate 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: TestCreateDate end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7e1ae01ec347c1fc6e665aa1d6e245243c2a5e7d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/createdate/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createdate_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d87886f1cc232ae34c7006bf710f65ac15cee8e6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getdatevalue", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetdatevalue.so": "file:./src/main/cpp/types/libgetdatevalue" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..519bebf639f50b4928244fc5e381316f6d4795ac --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutDate) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getdatevalue SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getdatevalue PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4637a978e8722b214f295d55a6a3e6aa50686253 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [Stat oh_jsvm_get_date_value] +#include +// [StartExclude oh_jsvm_get_date_value] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_date_value] + +// OH_JSVM_GetDateValue的样例方法 +static JSVM_Value GetDateValue(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)); + // 获取传入的Unix Time Stamp时间 + double value = 0; + JSVM_CALL(OH_JSVM_GetDateValue(env, args[0], &value)); + + // 将获取到的Unix Time Stamp时间转化为日期字符串打印 + uint64_t time = static_cast(value) / 1000; + char *date = ctime(reinterpret_cast(&time)); + OH_LOG_INFO(LOG_APP, "JSVM GetDateValue success:%{public}s", date); + + JSVM_Value returnValue = nullptr; + JSVM_CALL(OH_JSVM_CreateDouble(env, value, &returnValue)); + return returnValue; +} + +// CreateDate注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetDateValue}, +}; +static JSVM_CallbackStruct *method = param; +// CreateDate方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getDateValue", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(getDateValue(new Date(Date.now())))JS"; +// [End oh_jsvm_get_date_value] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getdatevalue", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/types/libgetdatevalue/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/types/libgetdatevalue/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/types/libgetdatevalue/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/types/libgetdatevalue/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/types/libgetdatevalue/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a4f8967b575d5b2ef0409444fa0fadfd4a0444c2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/cpp/types/libgetdatevalue/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetdatevalue.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/ets/getdatevalueability/GetdatevalueAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/ets/getdatevalueability/GetdatevalueAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..4197cee54fde2f30f42210657b6e3d4aa84d92b8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/ets/getdatevalueability/GetdatevalueAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetdatevalueAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..fedabe9554430cfdbac232c6b678941e9ee9d24b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetdatevalue.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..54da6858a03c35b9be6d604c46107ae0eb46e9d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getdatevalue", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetdatevalueAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetdatevalueAbility", + "srcEntry": "./ets/getdatevalueability/GetdatevalueAbility.ets", + "description": "$string:GetdatevalueAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetdatevalueAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..56aaf0feaaf364440bffd54c118b8453dabd5e87 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetdatevalueAbility_desc", + "value": "description" + }, + { + "name": "GetdatevalueAbility_label", + "value": "JsvmAboutDate" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..8740d051bd5fa977a71ff9dcedaa316063a5c43e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetDateValue', 0, async (done: Function) => { + console.log('uitest: TestGetDateValue begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetdatevalueAbility' + } + 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('GetdatevalueAbility'); + + 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: TestGetDateValue end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7029c62d68d3c9feff602d95e6b8e0a93f1144ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/getdatevalue/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getdatevalue_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..66e0533be2a9441c901cfcff9471f98dc9743714 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "isdate", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libisdate.so": "file:./src/main/cpp/types/libisdate" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ecb3665d3335b9e0e2c51a4b441b424b754a7d6e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutDate) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(isdate SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(isdate PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10e687b320f63b06ad81e180a274f2dbb533b513 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/hello.cpp @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_is_date] +// OH_JSVM_IsDate的样例方法 +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 = false; + JSVM_CALL(OH_JSVM_IsDate(env, args[0], &isData)); + OH_LOG_INFO(LOG_APP, "JSVM IsDate success:%{public}d", isData); + + JSVM_Value result = nullptr; + JSVM_CALL(OH_JSVM_GetBoolean(env, isData, &result)); + return result; +} +// CreateDate注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsDate}, +}; +static JSVM_CallbackStruct *method = param; +// CreateDate方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isDate", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(isDate(new Date(Date.now())))JS"; +// [End oh_jsvm_is_date] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "isdate", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/types/libisdate/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/types/libisdate/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5bb4004b04c2d80c35770653def1fac87f339cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/types/libisdate/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/types/libisdate/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/types/libisdate/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1e49a449d5cd1c463e814a4335799c7511d484d6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/cpp/types/libisdate/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libisdate.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/ets/isdateability/IsdateAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/ets/isdateability/IsdateAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..29f9cd7504b5c430ba395ef15b6df928f67f609b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/ets/isdateability/IsdateAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IsdateAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..9114e76f35838a979932c6098d5685baae8e6c25 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libisdate.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7d8595bb96379d04c40c8994e0e6dff99eadc10c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isdate", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IsdateAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IsdateAbility", + "srcEntry": "./ets/isdateability/IsdateAbility.ets", + "description": "$string:IsdateAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IsdateAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c52973df9869308559b2fbb6ce1fbaa91f6dfadf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IsdateAbility_desc", + "value": "description" + }, + { + "name": "IsdateAbility_label", + "value": "JsvmAboutDate" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..cc16b02e16cb8e385f501469303aeba48f788041 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsDate', 0, async (done: Function) => { + console.log('uitest: TestIsDate begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IsdateAbility' + } + 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('IsdateAbility'); + + 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: TestIsDate end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..70177e87b6e6e0de60a35df99ea6a7ca7b5e924d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/isdate/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isdate_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..1e30ecb80ff41f354bbb12c1cf8e9938d4f915b8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/ohosTest.md @@ -0,0 +1,10 @@ +# JsvmAboutDate 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 创建了一个表示给定毫秒数的Date对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript Date的时间值的Double基础类型值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断一个JavaScript对象是否为Date类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/screenshots/JsvmAboutDate_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/screenshots/JsvmAboutDate_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/screenshots/JsvmAboutDate_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/screenshots/JsvmAboutDate_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/screenshots/JsvmAboutDate_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutDate/screenshots/JsvmAboutDate_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dda96436534c0e6fc0d01b56ade49a387d57ac75 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutobject", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ef233ddf9974f4f9d48542091b9bd51d722cf129 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutObject" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ad1087b5adb5a5901e851409e865d70f2081d2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/README.md @@ -0,0 +1,263 @@ +# ArkTS使用JSVM-API接口进行object相关开发 + +### 介绍 + +使用JSVM-API接口进行object相关开发,处理JavaScript对象的基本操作的功能,例如创建对象、获取原型、冻结和密封对象,检查对象的类型等。这些操作是在处理JavaScript对象时非常常见的,提供了一种与JavaScript对象交互的方式。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行object相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-object.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-----------------------------------------------------------------: | :-----------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +checkobjecttypetag/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcheckobjecttypetag + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── checkobjecttypetagability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createexternal/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreateexternal + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createexternalability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createobject/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreateobject + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createobjectability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createsymbol/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatesymbol + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createsymbolability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getprototype/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvalueexternal/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvalueexternal + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvalueexternalability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +instanceof/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libinstanceof + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── instanceofability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +objectfreeze/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libobjectfreeze + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── objectfreezeability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +objectseal/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libobjectseal + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── objectsealability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +symbolfor/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libsymbolfor + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── symbolforability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +typeof/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libtypeof + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── typeofability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a4b5ee1bf1d0d8932687976f9c50ef6f0b29ad2e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/build-profile.json5 @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getprototype", + "srcPath": "./getprototype", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createobject", + "srcPath": "./createobject", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "objectfreeze", + "srcPath": "./objectfreeze", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "objectseal", + "srcPath": "./objectseal", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "typeof", + "srcPath": "./typeof", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "instanceof", + "srcPath": "./instanceof", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "checkobjecttypetag", + "srcPath": "./checkobjecttypetag", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createexternal", + "srcPath": "./createexternal", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvalueexternal", + "srcPath": "./getvalueexternal", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createsymbol", + "srcPath": "./createsymbol", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "symbolfor", + "srcPath": "./symbolfor", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2ca9a7fcfe8e19836c6822f011ce2abe4941df27 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "checkobjecttypetag", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcheckobjecttypetag.so": "file:./src/main/cpp/types/libcheckobjecttypetag" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ededd4e1177c5d146ddc1b390aeb32cdc3717276 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(checkobjecttypetag SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(checkobjecttypetag PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6cc4a36449d3f83238790d821d67c3c7384ddd6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/hello.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_check_object_type_tag] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#define NUMBERINT_FOUR +// [StartExclude oh_jsvm_check_object_type_tag] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_check_object_type_tag] + +// 定义一个静态常量JSVM_TypeTag数组存储类型标签 +static const JSVM_TypeTag TagsData[NUMBERINT_FOUR] = { + {0x9e4b2449547061b3, 0x33999f8a6516c499}, + {0x1d55a794c53a726d, 0x43633f509f9c944e}, + {0, 0}, // 用于表示无标签或默认标签 + {0x6a971439f5b2e5d7, 0x531dc28a7e5317c0}, +}; +// OH_JSVM_TypeTagObject的样例方法 +static JSVM_Value SetTypeTagToObject(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取两个JavaScript侧传入的参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取索引数字转换为JSVM_Value + int32_t index = 0; + OH_JSVM_GetValueInt32(env, args[1], &index); + // 给参数(对象)设置类型标签 + JSVM_Status status = OH_JSVM_TypeTagObject(env, args[0], &TagsData[index]); + // 将bool结果转换为JSVM_Value并返回 + JSVM_Value result = nullptr; + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM SetTypeTagToObject fail"); + OH_JSVM_GetBoolean(env, false, &result); + } else { + OH_LOG_INFO(LOG_APP, "JSVM SetTypeTagToObject success"); + OH_JSVM_GetBoolean(env, true, &result); + } + return result; +} +// OH_JSVM_CheckObjectTypeTag的样例方法 +static JSVM_Value CheckObjectTypeTag(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取两个JavaScript侧传入的参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取索引数字转换为JSVM_Value + int32_t index = 0; + OH_JSVM_GetValueInt32(env, args[1], &index); + // 检查对象的类型标签 + bool checkResult = false; + JSVM_Status status = OH_JSVM_CheckObjectTypeTag(env, args[0], &TagsData[index], &checkResult); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM SetTypeTagToObject fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM SetTypeTagToObject:%{public}d", checkResult); + } + // 将bool结果转换为JSVM_Value并返回 + JSVM_Value checked = nullptr; + OH_JSVM_GetBoolean(env, checkResult, &checked); + return checked; +} +// SetTypeTagToObject,CheckObjectTypeTag注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = SetTypeTagToObject}, + {.data = nullptr, .callback = CheckObjectTypeTag}, +}; +static JSVM_CallbackStruct *method = param; +// SetTypeTagToObject,CheckObjectTypeTag方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"setTypeTagToObject", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + {"checkObjectTypeTag", nullptr, method+1, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS( + class Obj { + data; + message; + } + let obj= { data: 0, message: "hello world"}; + setTypeTagToObject(obj, 0); + checkObjectTypeTag(obj, 0);)JS"; +// [End oh_jsvm_check_object_type_tag] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "checkobjecttypetag", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/types/libcheckobjecttypetag/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/types/libcheckobjecttypetag/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/types/libcheckobjecttypetag/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/types/libcheckobjecttypetag/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/types/libcheckobjecttypetag/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0e9da982043323bf5f48260a795a2698ade097bc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/cpp/types/libcheckobjecttypetag/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcheckobjecttypetag.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/checkobjecttypetagability/CheckobjecttypetagAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/checkobjecttypetagability/CheckobjecttypetagAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..d4572bdd887d1746a97ee66d7dd07173420be38e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/checkobjecttypetagability/CheckobjecttypetagAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CheckobjecttypetagAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..e6acbafcca9094cbfac2e69457ec99d877f4cf92 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcheckobjecttypetag.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..146fe9ab4aff39e915904beb7d6723ed5de9588d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "checkobjecttypetag", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CheckobjecttypetagAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CheckobjecttypetagAbility", + "srcEntry": "./ets/checkobjecttypetagability/CheckobjecttypetagAbility.ets", + "description": "$string:CheckobjecttypetagAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CheckobjecttypetagAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..31eca34aa45ad344fbd2e3fd25472f72b59f9b76 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CheckobjecttypetagAbility_desc", + "value": "description" + }, + { + "name": "CheckobjecttypetagAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..e718a6bda7ff4c3583af283468a2163e4ac32a55 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCheckObjectTypeTag', 0, async (done: Function) => { + console.log('uitest: TestCheckObjectTypeTag begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CheckobjecttypetagAbility' + } + 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('CheckobjecttypetagAbility'); + + 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: TestCheckObjectTypeTag end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..12d16b4f61e3ef5981c4806ed74c485934c6a907 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/checkobjecttypetag/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "checkobjecttypetag_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a08c5142234dc5775f1bc3c6dbbb20ef23a41815 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createexternal", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreateexternal.so": "file:./src/main/cpp/types/libcreateexternal" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..97202c0cfa30df9ed8f8a473c732fe4d7a0ab808 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createexternal SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createexternal PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb4034c4a658229ad5b18612be1dc4422b572c3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/hello.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_external] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +#include +// [StartExclude oh_jsvm_create_external] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_external] + +// OH_JSVM_CreateExternal的样例方法 +static JSVM_Value CreateExternal(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t dataSize = 10; + void *data = malloc(dataSize); + if (data == nullptr) { + OH_LOG_ERROR(LOG_APP, "JSVM Failed to malloc."); + return nullptr; + } + memset(data, 0, dataSize); + const char* testStr = "test"; + JSVM_Value external = nullptr; + JSVM_Status status = OH_JSVM_CreateExternal( + env, data, [](JSVM_Env env, void *data, void *hint) {free(data);}, (void *)testStr, &external); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM Failed to create external data, status:%{public}d.", status); + free(data); + data = nullptr; + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateExternal success"); + } + return external; +} +// CreateExternal注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateExternal}, +}; +static JSVM_CallbackStruct *method = param; +// CreateExternal方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createExternal", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createExternal())JS"; +// [End oh_jsvm_create_external] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createexternal", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/types/libcreateexternal/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/types/libcreateexternal/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/types/libcreateexternal/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/types/libcreateexternal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/types/libcreateexternal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..be27396720a0fa328e6560a486516bb0e5e08102 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/cpp/types/libcreateexternal/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreateexternal.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/ets/createexternalability/CreateexternalAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/ets/createexternalability/CreateexternalAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..a1d7aa147cc6488fdb8e4eb2be8afcd73f6cd7a2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/ets/createexternalability/CreateexternalAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreateexternalAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..895125b49b1f39dfbd60b6c41ca5d199fd5208ed --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreateexternal.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7cfef6f75e0e9370f4873f224ec55bb31059be1c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createexternal", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreateexternalAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreateexternalAbility", + "srcEntry": "./ets/createexternalability/CreateexternalAbility.ets", + "description": "$string:CreateexternalAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreateexternalAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..477cec44f86d60e163fcebb3602292105b4df3d7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreateexternalAbility_desc", + "value": "description" + }, + { + "name": "CreateexternalAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..67f1a8df729543962c968f1bba318c6dc5762dc4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateExternal', 0, async (done: Function) => { + console.log('uitest: TestCreateExternal begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreateexternalAbility' + } + 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('CreateexternalAbility'); + + 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: TestCreateExternal end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3d00fb89ec9c037fe1ed589e104e28e2a866e293 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createexternal/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createexternal_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..92e42ff1a8ab10ad5628b569fa7ea1cfc90d289b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createobject", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreateobject.so": "file:./src/main/cpp/types/libcreateobject" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..bf648023637b963c15082bd295e73ec70cb871df --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createobject SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createobject PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f0966b941944eae0cfeb751ee5baa6f3a07a67d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/hello.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_object] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_create_object] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_object] + +// OH_JSVM_CreateObject的样例方法 +static JSVM_Value CreateObject(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value object = nullptr; + // 创建一个空对象 + JSVM_Status status = OH_JSVM_CreateObject(env, &object); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateObject fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateObject success"); + } + // 设置对象的属性 + JSVM_Value name = nullptr; + // 设置属性名为 "name" + OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &name); + JSVM_Value value = nullptr; + // 设置属性值为 "Hello from N-API!" + OH_JSVM_CreateStringUtf8(env, "Hello OH_JSVM_CreateObject!", JSVM_AUTO_LENGTH, &value); + // 将属性设置到对象上 + OH_JSVM_SetProperty(env, object, name, value); + return object; +} +// CreateObject注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateObject}, +}; +static JSVM_CallbackStruct *method = param; +// CreateObject方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createObject())JS"; +// [End oh_jsvm_create_object] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createobject", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/types/libcreateobject/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/types/libcreateobject/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/types/libcreateobject/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/types/libcreateobject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/types/libcreateobject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f3b63257128ba9eed6e0ca3002f0e0deceb6998f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/cpp/types/libcreateobject/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreateobject.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/ets/createobjectability/CreateobjectAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/ets/createobjectability/CreateobjectAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..7ba5ff339f5d6ba39efba44191ad0121b615e0c2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/ets/createobjectability/CreateobjectAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreateobjectAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..81c6c22b047c45f4650af982b703c60983eb7d34 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreateobject.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..80b23bf8c7f2034afbf22d88e6e2f83e9cfdb503 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createobject", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreateobjectAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreateobjectAbility", + "srcEntry": "./ets/createobjectability/CreateobjectAbility.ets", + "description": "$string:CreateobjectAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreateobjectAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..bb24ee9ff92361ab10e8cb7642b7e605fef2970e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreateobjectAbility_desc", + "value": "description" + }, + { + "name": "CreateobjectAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..9eaa609e06d0c3b4212a60c50f5542d0e812b6ad --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateObject', 0, async (done: Function) => { + console.log('uitest: TestCreateObject begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreateobjectAbility' + } + 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('CreateobjectAbility'); + + 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: TestCreateObject end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..34d9e19e41a5236faacaec98abd0726d3774f2df --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createobject/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createobject_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b4e40ae1092e8e74579c437dcfa666373d0afddd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createsymbol", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatesymbol.so": "file:./src/main/cpp/types/libcreatesymbol" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5aab5eaaa26effb2521a129ca3984aab60191f16 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createsymbol SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createsymbol PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82f0935c3e018c7df4481797fc5e2f8ad6bb08fb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_symbol] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [StartExclude oh_jsvm_create_symbol] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_symbol] + +// OH_JSVM_CreateSymbol的样例方法 +static JSVM_Value CreateSymbol(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value result = nullptr; + const char *des = "only"; + OH_JSVM_CreateStringUtf8(env, des, JSVM_AUTO_LENGTH, &result); + JSVM_Value returnSymbol = nullptr; + OH_JSVM_CreateSymbol(env, result, &returnSymbol); + JSVM_ValueType valuetypeSymbol; + OH_JSVM_Typeof(env, returnSymbol, &valuetypeSymbol); + if (valuetypeSymbol == JSVM_SYMBOL) { + OH_LOG_INFO(LOG_APP, "JSVM CreateSymbol Success"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateSymbol fail"); + } + return returnSymbol; +} +// CreateSymbol注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateSymbol}, +}; +static JSVM_CallbackStruct *method = param; +// CreateSymbol方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createSymbol", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createSymbol())JS"; +// [End oh_jsvm_create_symbol] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createsymbol", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/types/libcreatesymbol/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/types/libcreatesymbol/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/types/libcreatesymbol/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/types/libcreatesymbol/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/types/libcreatesymbol/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3a1844f0f4845f682f99a5136b4fe2270e40a917 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/cpp/types/libcreatesymbol/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatesymbol.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/ets/createsymbolability/CreatesymbolAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/ets/createsymbolability/CreatesymbolAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..c1ec919eeb2cfcb9e6352ffa3a8fbd2db84794de --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/ets/createsymbolability/CreatesymbolAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatesymbolAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7aea0e047487468b40b9fef77aa8e2df82af3e40 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatesymbol.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5bd3807a9e3d5db9df293a876aa634115113ce28 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createsymbol", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatesymbolAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatesymbolAbility", + "srcEntry": "./ets/createsymbolability/CreatesymbolAbility.ets", + "description": "$string:CreatesymbolAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatesymbolAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..51f7a086694e28e108d2f844f9db50576f4cbf6f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatesymbolAbility_desc", + "value": "description" + }, + { + "name": "CreatesymbolAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ecf83c2a133bab57d2c69a143b430a4f8f46b7e7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateSymbol', 0, async (done: Function) => { + console.log('uitest: TestCreateSymbol begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatesymbolAbility' + } + 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('CreatesymbolAbility'); + + 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: TestCreateSymbol end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fea12ddaa844024145de6f4c27c1cc9a4c5ed42d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/createsymbol/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createsymbol_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..74058a69fffe9b6ef5d5e12a17fae4020e9456b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getprototype", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetprototype.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f95e3b6e48bfa59704b4cf419b9039a5bb7dfc36 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getprototype SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getprototype PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a896511deaf6d01ac8ea8ce0af4ddb0bd3ece673 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_prototype] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [StartExclude oh_jsvm_get_prototype] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_prototype] + +// OH_JSVM_GetPrototype的样例方法 +static JSVM_Value GetPrototype(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + JSVM_Value result{nullptr}; + JSVM_Status status = OH_JSVM_GetPrototype(env, argv[0], &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetPrototype fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetPrototype success"); + } + return result; +} +// GetPrototype注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetPrototype}, +}; +static JSVM_CallbackStruct *method = param; +// GetPrototype方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getPrototype", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(const myObject = {}; + const proto = getPrototype(myObject); + console.log(proto === Object.prototype);)JS"; +// [End oh_jsvm_get_prototype] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getprototype", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dc2fbe8986e82c4852d851c282b544be062ed225 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetprototype.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..52727f910acaf11bfe500425ce5c4520b88a9f9c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetprototype.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8e78e77d7e3ad2e451e7367c3c1a05aeee65def7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getprototype", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3da670abdb7561ffc22ded5d1d87916d3a3e6755 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..9064d60bce1d3cddb16af12dd7150196c7ce2198 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetPrototype', 0, async (done: Function) => { + console.log('uitest: TestGetPrototype 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: TestGetPrototype end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8d28f054f1448ba4b0dcde0151053187d316ef38 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getprototype/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getprototype_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e3853ee142d14f8a28e790a60d1a156a1ca58747 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvalueexternal", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvalueexternal.so": "file:./src/main/cpp/types/libgetvalueexternal" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c5b9dd4661d9205efff144cfbcbece42bd7ccdb0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvalueexternal SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvalueexternal PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff0511a63221cd63698e2162a4e143de3078fd75 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/hello.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_external] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_value_external] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_external] + +// OH_JSVM_GetValueExternal的样例方法 +static JSVM_Value GetValueExternal(JSVM_Env env, JSVM_CallbackInfo info) +{ + static int data = 0x12345; + JSVM_Value externalValue = nullptr; + JSVM_Status status = OH_JSVM_CreateExternal(env, (void*)&data, nullptr, nullptr, &externalValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateExternal fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateExternal success"); + } + void *dataValue; + status = OH_JSVM_GetValueExternal(env, externalValue, &dataValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueExternal fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueExternal success"); + } + // 将符号位转化为int类型传出去 + JSVM_Value returnValue = nullptr; + int retData = *static_cast(dataValue); + OH_JSVM_CreateInt32(env, retData, &returnValue); + return returnValue; +} +// GetValueExternal注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueExternal}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueExternal方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueExternal", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getValueExternal())JS"; +// [End oh_jsvm_get_value_external] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvalueexternal", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/types/libgetvalueexternal/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/types/libgetvalueexternal/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/types/libgetvalueexternal/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/types/libgetvalueexternal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/types/libgetvalueexternal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3a52b5b551641a46c7b8bc7050af3b5688bd58a8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/cpp/types/libgetvalueexternal/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvalueexternal.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/ets/getvalueexternalability/GetvalueexternalAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/ets/getvalueexternalability/GetvalueexternalAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..854e76dae73727ff8f0e60d20cfbc024c2b0d15b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/ets/getvalueexternalability/GetvalueexternalAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetvalueexternalAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..75b4b1456e769929a8c96b8a49b91a5bee3d2f63 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvalueexternal.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..478a853dbc37215488366da603298d4bdd0e7f2e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueexternal", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetvalueexternalAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetvalueexternalAbility", + "srcEntry": "./ets/getvalueexternalability/GetvalueexternalAbility.ets", + "description": "$string:GetvalueexternalAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetvalueexternalAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..15cee882c62dab4c1560db476d6c9a5469315fbe --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetvalueexternalAbility_desc", + "value": "description" + }, + { + "name": "GetvalueexternalAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..8a0d1cd86ec22c97469d8a697aceca0c24789852 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueExternal', 0, async (done: Function) => { + console.log('uitest: TestGetValueExternal begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetvalueexternalAbility' + } + 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('GetvalueexternalAbility'); + + 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: TestGetValueExternal end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..73f83efdafaf24eac2152b2eb3b0a0876d9f4486 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/getvalueexternal/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueexternal_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..84060f72213192f04fea7d90e20066906a9983ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "instanceof", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libinstanceof.so": "file:./src/main/cpp/types/libinstanceof" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..d584dc0bec723d558936004a07e0d4fc9b8f9c36 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(instanceof SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(instanceof PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..673e900837d52e63f558e964b424198b81451567 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/hello.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_instanceof] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_instanceof] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_instanceof] + +// OH_JSVM_Instanceof的样例方法 +static JSVM_Value InstanceOf(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取两个JavaScript侧传入的参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + bool result = false; + JSVM_Status status = OH_JSVM_Instanceof(env, args[0], args[1], &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM InstanceOf fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM InstanceOf:%{public}d", result); + } + JSVM_Value returnValue = nullptr; + OH_JSVM_GetBoolean(env, result, &returnValue); + return returnValue; +} +// InstanceOf注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = InstanceOf}, +}; +static JSVM_CallbackStruct *method = param; +// InstanceOf方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"instanceOf", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(class Person { + name; + age; + constructor(name, age) { + this.name = name; + this.age = age; + } + } + instanceOf(new Person('Alice', 30), Person); + ;)JS"; +// [End oh_jsvm_instanceof] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "instanceof", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/types/libinstanceof/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/types/libinstanceof/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/types/libinstanceof/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/types/libinstanceof/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/types/libinstanceof/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c5074f726033952faca790580c1224dae65af879 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/cpp/types/libinstanceof/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libinstanceof.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/ets/instanceofability/InstanceofAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/ets/instanceofability/InstanceofAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..9031c4e9ccc01e1ec017f02d232eddaa02bdf0bc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/ets/instanceofability/InstanceofAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class InstanceofAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..4c1efd2b974d111c0b9e63d19bdcf4fef0eb280b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libinstanceof.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4a40496aa13fbd77814c999721bdaa2450ea7c07 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "instanceof", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "InstanceofAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "InstanceofAbility", + "srcEntry": "./ets/instanceofability/InstanceofAbility.ets", + "description": "$string:InstanceofAbility_desc", + "icon": "$media:layered_image", + "label": "$string:InstanceofAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d9adfa2b71875596f0fe78675c560da7af62767f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "InstanceofAbility_desc", + "value": "description" + }, + { + "name": "InstanceofAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ad4b59a5598f8cafc718213283c4fe7eab9d4f7d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testInstanceof', 0, async (done: Function) => { + console.log('uitest: TestInstanceof begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'InstanceofAbility' + } + 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('InstanceofAbility'); + + 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: TestInstanceof end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..579d34e1b8a7e2ce2d47bc1d3ffe0aa2e74c8a8e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/instanceof/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "instanceof_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e0961a75159bc8916e8806636d42fba5bff1fc9f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "objectfreeze", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libobjectfreeze.so": "file:./src/main/cpp/types/libobjectfreeze" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0d091da41ab135d8621b860f79205cc843b0dbeb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(objectfreeze SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(objectfreeze PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d56b978b5f3b9e629740b284967f5abd6570e6a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_object_freeze] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_object_freeze] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_object_freeze] + +// OH_JSVM_ObjectFreeze的样例方法 +const int FREEZE_TEST_VALUE = 111111; +static JSVM_Value ObjectFreeze(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接受一个JavaScript侧传入的object + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + // 调用接口OH_JSVM_ObjectFreeze将传入的object冻结 + JSVM_Status status = OH_JSVM_ObjectFreeze(env, argv[0]); + if (status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "Test JSVM OH_JSVM_ObjectFreeze success"); + } + // 测试冻结后的对象中属性能否修改 + JSVM_Value value = nullptr; + OH_JSVM_CreateInt32(env, FREEZE_TEST_VALUE, &value); + OH_JSVM_SetNamedProperty(env, argv[0], "data", value); + // 将冻结后修改过的属性返回JavaScript侧 + return argv[0]; +} +// ObjectFreeze注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = ObjectFreeze}, +}; +static JSVM_CallbackStruct *method = param; +// ObjectFreeze方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"objectFreeze", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(let obj = { data: 55, message: "hello world"}; + objectFreeze(obj))JS"; +// [End oh_jsvm_object_freeze] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "objectfreeze", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/types/libobjectfreeze/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/types/libobjectfreeze/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/types/libobjectfreeze/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/types/libobjectfreeze/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/types/libobjectfreeze/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..bb8749ef3a296e53aaa63a8c11faf48a0fabfea2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/cpp/types/libobjectfreeze/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libobjectfreeze.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/ets/objectfreezeability/ObjectfreezeAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/ets/objectfreezeability/ObjectfreezeAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..4f071e4b9a40abb04e6a15a4840a8b0f8686e5c2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/ets/objectfreezeability/ObjectfreezeAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ObjectfreezeAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..9855c7bcc81b77d1d37bd8b082a90b2aafcff3be --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libobjectfreeze.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..20e2ca5492e4d9263a92773022dabafdc9ecc084 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "objectfreeze", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ObjectfreezeAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ObjectfreezeAbility", + "srcEntry": "./ets/objectfreezeability/ObjectfreezeAbility.ets", + "description": "$string:ObjectfreezeAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ObjectfreezeAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..127d4344fc0e1a83dcf659463886dfac50e38bc6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ObjectfreezeAbility_desc", + "value": "description" + }, + { + "name": "ObjectfreezeAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..6f02636e2b790afe65136ada305cd44b9ffa1ec5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testObjectFreeze', 0, async (done: Function) => { + console.log('uitest: TestObjectFreeze begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ObjectfreezeAbility' + } + 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('ObjectfreezeAbility'); + + 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: TestObjectFreeze end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb3e6f9ad1246fc6448b325f8548af3ace349e85 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectfreeze/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "objectfreeze_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7c69b69ab1c4b04fd5040a4e08654bca6e175b14 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "objectseal", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libobjectseal.so": "file:./src/main/cpp/types/libobjectseal" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..eba25f767d656cc0890581b90af3c52d04679355 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(objectseal SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(objectseal PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bc0cf6af528be56ccfdb4d0014e1880571fb15b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/hello.cpp @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_object_seal] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_object_seal] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_object_seal] + +// OH_JSVM_ObjectSeal的样例方法 +const int TEST_VALUE = 111111; +static JSVM_Value ObjectSeal(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接受一个JavaScript侧传入的object + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + // 调用接口OH_JSVM_ObjectSeal将传入的object封闭,使其无法添加新的属性 + JSVM_Status status = OH_JSVM_ObjectSeal(env, argv[0]); + if (status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "Test JSVM OH_JSVM_ObjectSeal success"); + } + // 检查封闭后的对象中属性能否修改、删除、新增 + // 封闭后对象修改 + JSVM_Value changeValue = nullptr; + OH_JSVM_CreateInt32(env, TEST_VALUE, &changeValue); + OH_JSVM_SetNamedProperty(env, argv[0], "data", changeValue); + // 封闭后对象删除 + JSVM_Value deleteProperty = nullptr; + OH_JSVM_CreateStringUtf8(env, "message", JSVM_AUTO_LENGTH, &deleteProperty); + bool result = false; + OH_JSVM_DeleteProperty(env, argv[0], deleteProperty, &result); + if (result) { + OH_LOG_INFO(LOG_APP, "Test JSVM OH_JSVM_ObjectSeal failed"); + } + // 封闭后对象新增 + JSVM_Value addValue = nullptr; + OH_JSVM_CreateStringUtf8(env, "addValue", JSVM_AUTO_LENGTH, &addValue); + OH_JSVM_SetNamedProperty(env, argv[0], "newProperty", addValue); + // 将封闭后改动过的对象返回JavaScript侧 + return argv[0]; +} +// ObjectSeal注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = ObjectSeal}, +}; +static JSVM_CallbackStruct *method = param; +// ObjectSeal方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"objectSeal", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS( let obj = { data: 55, message: "hello world"}; + objectSeal(obj))JS"; +// [End oh_jsvm_object_seal] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "objectseal", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/types/libobjectseal/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/types/libobjectseal/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/types/libobjectseal/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/types/libobjectseal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/types/libobjectseal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..913f2ad794e418a55985361d363e831ca7ed1b65 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/cpp/types/libobjectseal/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libobjectseal.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/ets/objectsealability/ObjectsealAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/ets/objectsealability/ObjectsealAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6a4926e705e3af70c3103cc5e0b9fb3ed196574b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/ets/objectsealability/ObjectsealAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ObjectsealAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..5a0d8d097ebd38aa049b550dc517702dcb541f04 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libobjectseal.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ba8e2d0bce2d6926be0839cc92421887e0500324 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "objectseal", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ObjectsealAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ObjectsealAbility", + "srcEntry": "./ets/objectsealability/ObjectsealAbility.ets", + "description": "$string:ObjectsealAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ObjectsealAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d3b4bf96f9d1dd55c2aba9f500eff17877867db3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ObjectsealAbility_desc", + "value": "description" + }, + { + "name": "ObjectsealAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..4c9c7dc2d940ae1ed60c543d616ae4a864a73e24 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testObjectSeal', 0, async (done: Function) => { + console.log('uitest: TestObjectSeal begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ObjectsealAbility' + } + 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('ObjectsealAbility'); + + 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: TestObjectSeal end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4f94ebe3421861066c14de55238abbcb2ec4acf4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/objectseal/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "objectseal_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..3e518f73a7edd68b80d3780ccf949b38e8978920 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/ohosTest.md @@ -0,0 +1,18 @@ +# JsvmAboutObject 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------------------------------------------------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 检查给定的类型标签是否与对象上的类型标签匹配 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建一个包装了外部指针的JavaScript对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建一个默认的JavaScript Object对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据给定的描述符创建一个Symbol对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript对象的原型 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取先前传递给Hello World的外部数据指针 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断一个对象是否是某个构造函数的实例 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 冻结给定的对象,防止向其添加新属性,删除现有属性,防止更改现有属性的可枚举性、可配置性或可写性,并防止更改现有属性的值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 密封给定的对象。这可以防止向其添加新属性,以及将所有现有属性标记为不可配置 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 在全局注册表中搜索具有给定描述的现有Symbol,如果该Symbol已经存在,它将被返回,否则将在注册表中创建一个新Symbol | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 返回JavaScript对象的类型 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/screenshots/JsvmAboutObject_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/screenshots/JsvmAboutObject_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/screenshots/JsvmAboutObject_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/screenshots/JsvmAboutObject_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/screenshots/JsvmAboutObject_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/screenshots/JsvmAboutObject_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6c503d8ba3249c911439c9dce6de0051328cfe16 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "symbolfor", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libsymbolfor.so": "file:./src/main/cpp/types/libsymbolfor" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..7a64d446b3a6b2a460f41ffb54387273c98642cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(symbolfor SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(symbolfor PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50cd55a55c9a821e374350097f1fa44f943a7e3d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/hello.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_symbol_for] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_symbol_for] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_symbol_for] + +static const size_t TESTMO_LENGTH = 9; +// 定义一个常量,用于存储最大字符串长度 +static const int MAX_BUFFER_SIZE = 128; +// OH_JSVM_SymbolFor的样例方法 +static JSVM_Value SymbolFor(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value description = nullptr; + OH_JSVM_CreateStringUtf8(env, "test_demo", TESTMO_LENGTH, &description); + char buffer[MAX_BUFFER_SIZE]; + size_t bufferSize = MAX_BUFFER_SIZE; + size_t copied = 0; + OH_JSVM_GetValueStringUtf8(env, description, buffer, bufferSize, &copied); + JSVM_Value symbol = nullptr; + OH_JSVM_CreateSymbol(env, description, &symbol); + JSVM_Value result_symbol = nullptr; + JSVM_Status status = OH_JSVM_SymbolFor(env, buffer, copied, &result_symbol); + JSVM_ValueType valuetypeSymbol; + OH_JSVM_Typeof(env, result_symbol, &valuetypeSymbol); + if (valuetypeSymbol == JSVM_SYMBOL && status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_SymbolFor success"); + } + // 返回结果 + return result_symbol; +} +// SymbolFor注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = SymbolFor}, +}; +static JSVM_CallbackStruct *method = param; +// SymbolFor方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"symbolFor", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(symbolFor())JS"; +// [End oh_jsvm_symbol_for] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "symbolfor", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/types/libsymbolfor/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/types/libsymbolfor/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/types/libsymbolfor/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/types/libsymbolfor/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/types/libsymbolfor/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f7836eab0c92a8999632ec003c52a202e13a6281 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/cpp/types/libsymbolfor/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libsymbolfor.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7e319a64c0e475e3c0f73197e4c3a8a5c3d1c72a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libsymbolfor.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/ets/symbolforability/SymbolforAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/ets/symbolforability/SymbolforAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..619d5e0e813c5399eaef08f3db36d370ecebbeb0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/ets/symbolforability/SymbolforAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class SymbolforAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..990e56565d96d45f1fb944ff5389bca6df6207f8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/module.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "symbolfor", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "SymbolforAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "SymbolforAbility", + "srcEntry": "./ets/symbolforability/SymbolforAbility.ets", + "description": "$string:SymbolforAbility_desc", + "icon": "$media:layered_image", + "label": "$string:SymbolforAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a1845a148fe20bb6f8925d35eadac0c2c4181761 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "SymbolforAbility_desc", + "value": "description" + }, + { + "name": "SymbolforAbility_label", + "value": "label" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a9fcdbc33bfce75d5079680c3c0baf87e6fdc875 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testSymbolFor', 0, async (done: Function) => { + console.log('uitest: TestSymbolFor begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'SymbolforAbility' + } + 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('SymbolforAbility'); + + 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: TestSymbolFor end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..26ee7cdcb37c84d4b964df753679ca44c006cf9f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/symbolfor/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "symbolfor_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9c59fc83064cd57b98524237b2334c0b1b576de --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "typeof", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libtypeof.so": "file:./src/main/cpp/types/libtypeof" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5281c648ba631c26b6c02f354ca38cf19da3fcfd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutObject) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(typeof SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(typeof PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20882f1bb747af06edb0804be32034e9d12a58e7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/hello.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_typeof] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_typeof] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_typeof] + +// OH_JSVM_Typeof的样例方法 +static JSVM_Value GetTypeof(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_ValueType valueType; + OH_JSVM_Typeof(env, args[0], &valueType); + JSVM_Value type = nullptr; + switch (valueType) { + case JSVM_UNDEFINED: + OH_LOG_INFO(LOG_APP, "JSVM Input type is undefined"); + OH_JSVM_CreateStringUtf8(env, "Input type is undefined", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_NULL: + OH_LOG_INFO(LOG_APP, "JSVM Input type is null"); + OH_JSVM_CreateStringUtf8(env, "Input type is null", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_BOOLEAN: + OH_LOG_INFO(LOG_APP, "JSVM Input type is boolean"); + OH_JSVM_CreateStringUtf8(env, "Input type is boolean", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_NUMBER: + OH_LOG_INFO(LOG_APP, "JSVM Input type is number"); + OH_JSVM_CreateStringUtf8(env, "Input type is number", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_STRING: + OH_LOG_INFO(LOG_APP, "JSVM Input type is string"); + OH_JSVM_CreateStringUtf8(env, "Input type is string", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_SYMBOL: + OH_LOG_INFO(LOG_APP, "JSVM Input type is symbol"); + OH_JSVM_CreateStringUtf8(env, "Input type is symbol", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_OBJECT: + OH_LOG_INFO(LOG_APP, "JSVM Input type is object"); + OH_JSVM_CreateStringUtf8(env, "Input type is object", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_FUNCTION: + OH_LOG_INFO(LOG_APP, "JSVM Input type is function"); + OH_JSVM_CreateStringUtf8(env, "Input type is function", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_EXTERNAL: + OH_LOG_INFO(LOG_APP, "JSVM Input type is external"); + OH_JSVM_CreateStringUtf8(env, "Input type is external", JSVM_AUTO_LENGTH, &type); + break; + case JSVM_BIGINT: + OH_LOG_INFO(LOG_APP, "JSVM Input type is bigint"); + OH_JSVM_CreateStringUtf8(env, "Input type is bigint", JSVM_AUTO_LENGTH, &type); + break; + default: + OH_LOG_INFO(LOG_APP, "JSVM Input type does not match any"); + OH_JSVM_CreateStringUtf8(env, " ", JSVM_AUTO_LENGTH, &type); + break; + } + return type; +} +// GetTypeof注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetTypeof}, +}; +static JSVM_CallbackStruct *method = param; +// GetTypeof方法别名,TS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getTypeof", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getTypeof(true);)JS"; +// [End oh_jsvm_typeof] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "typeof", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/types/libtypeof/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/types/libtypeof/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/types/libtypeof/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/types/libtypeof/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/types/libtypeof/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cc8c0984833add392968b5c8260b9028462ef3d9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/cpp/types/libtypeof/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libtypeof.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..ba0644d764ecbb7a94d7f73c0da0adb351b73931 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libtypeof.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/ets/typeofability/TypeofAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/ets/typeofability/TypeofAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..5db4403ad0ae1b1b8ac346cf707b314d326ba978 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/ets/typeofability/TypeofAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class TypeofAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dfeabf17a16abc4abbfee218e6fd01701e37883c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "typeof", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "TypeofAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "TypeofAbility", + "srcEntry": "./ets/typeofability/TypeofAbility.ets", + "description": "$string:TypeofAbility_desc", + "icon": "$media:layered_image", + "label": "$string:TypeofAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..99c1866e9cef517425d6ce2c9112d95479f81579 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "TypeofAbility_desc", + "value": "description" + }, + { + "name": "TypeofAbility_label", + "value": "JsvmAboutObject" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..99908a153bb83fa4930dc5caae83a2bb687b6300 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testTypeof', 0, async (done: Function) => { + console.log('uitest: TestTypeof begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'TypeofAbility' + } + 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('TypeofAbility'); + + 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: TestTypeof end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..589903faa07d63a7c80db06d80eb03704c5e6b4b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutObject/typeof/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "typeof_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..951d28629e67cbb47321a23b258701d5221b3c2f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutprimitive", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5fc6906c32e3ecaf31a876c953fa40dd525fd213 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutPrimitive" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/README.md new file mode 100644 index 0000000000000000000000000000000000000000..289a43e5e67d621c818135a422fc135a4fd8fc55 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/README.md @@ -0,0 +1,225 @@ +# ArkTs使用JSVM-API接口进行primitive类相关开发 + +### 介绍 + +在使用JSVM-API接口时,开发人员可以实现在JSVM模块中与JavaScript对象的交互,并进行数据转换和获取特定对象的操作,它们在不同的场景中发挥着重要的作用,使开发人员能够更灵活地处理JavaScript值和对象。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行primitive类相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-primitive.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :--------------------------------------------------------------------: | :--------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +coercetobool/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +coercetonumber/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcoercetonumber + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── coercetonumberability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +coercetoobject/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcoercetoobject + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── coercetoobjectability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +coercetostring/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcoercetostring + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── coercetostringability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getboolean/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetboolean + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getbooleanability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getglobal/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetglobal + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getglobalability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getnull/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetnull + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getnullability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getundefined/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetundefined + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getundefinedability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluebool/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvaluebool + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvalueboolability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..592c45bc8e557f73749dd1884384923701ed1a96 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/build-profile.json5 @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "coercetobool", + "srcPath": "./coercetobool", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "coercetonumber", + "srcPath": "./coercetonumber", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "coercetoobject", + "srcPath": "./coercetoobject", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "coercetostring", + "srcPath": "./coercetostring", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getboolean", + "srcPath": "./getboolean", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvaluebool", + "srcPath": "./getvaluebool", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getglobal", + "srcPath": "./getglobal", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getnull", + "srcPath": "./getnull", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getundefined", + "srcPath": "./getundefined", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..520ffbdd0bb7ce9712176b7bb367030b34f58d3b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "coercetobool", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcoercetobool.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c196aec3ccd4b8fa2e222984d3e9525df0fb12f9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPrimitiveOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(coercetobool SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(coercetobool PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49bd5a10b1f126d91a02faa77f8539e436adf7fb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_coerce_to_bool] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_coerce_to_bool] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_coerce_to_bool] + +// OH_JSVM_CoerceToBool的样例方法 +static JSVM_Value CoerceToBool(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value boolean = nullptr; + JSVM_Status status = OH_JSVM_CoerceToBool(env, args[0], &boolean); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToBool failed"); + } else { + bool result = false; + OH_JSVM_GetValueBool(env, boolean, &result); + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToBool success:%{public}d", result); + } + return boolean; +} +// CoerceToBool注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CoerceToBool}, +}; +static JSVM_CallbackStruct *method = param; +// CoerceToBool方法别名,ArkTS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"coerceToBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(coerceToBool("123"))JS"; +// [End oh_jsvm_coerce_to_bool] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "coercetobool", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..10a24b2643eb1a995abdf262abe3c2b01f35a36e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcoercetobool.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1c2933b5639bfc93679662821c998d486096b7f5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcoercetobool.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..574c7b77d7a628e6adbe96764a1583e34745f760 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetobool", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7c00d4195f1b057baef9855dc29e653b488275cf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..68479d14f59dfa8e284974a5cbe75e067d3de470 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCoerceToBool', 0, async (done: Function) => { + console.log('uitest: TestCoerceToBool 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: TestCoerceToBool end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ae9cbd3f31fb6154e06f20dfa17db00537c72108 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetobool/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetobool_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1c31ed541df2da8ac28babfdc69a307d9570f66f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "coercetonumber", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcoercetonumber.so": "file:./src/main/cpp/types/libcoercetonumber" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c17bb3eff70c83adc5a181c1ca1cfc85b32cfc32 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPrimitiveOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(coercetonumber SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(coercetonumber PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e0d5f5ac7c54c0b382a5afda78e057d43f031cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_coerce_to_number] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [StartExclude oh_jsvm_coerce_to_number] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_coerce_to_number] + +// OH_JSVM_CoerceToNumber的样例方法 +static JSVM_Value CoerceToNumber(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value number = nullptr; + JSVM_Status status = OH_JSVM_CoerceToNumber(env, args[0], &number); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToNumber failed"); + } else { + int32_t result = 0; + OH_JSVM_GetValueInt32(env, number, &result); + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToNumber success:%{public}d", result); + } + return number; +} +// CoerceToNumber注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CoerceToNumber}, +}; +static JSVM_CallbackStruct *method = param; +// CoerceToNumber方法别名,ArkTS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"coerceToNumber", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(coerceToNumber(true))JS"; +// [End oh_jsvm_coerce_to_number] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "coercetonumber", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/types/libcoercetonumber/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/types/libcoercetonumber/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/types/libcoercetonumber/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/types/libcoercetonumber/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/types/libcoercetonumber/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..417e718843a3f981e625acfa88922196ba4c2052 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/cpp/types/libcoercetonumber/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcoercetonumber.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/ets/coercetonumberability/CoercetonumberAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/ets/coercetonumberability/CoercetonumberAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..fc2c75d969311327861044a14bf51afcb8d85e60 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/ets/coercetonumberability/CoercetonumberAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CoercetonumberAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..baccd05bd192b552ea1d2d9c61544e45788a3109 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcoercetonumber.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4c714c1d578bc45d4f2c46de2c843029ff14d91f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetonumber", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CoercetonumberAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CoercetonumberAbility", + "srcEntry": "./ets/coercetonumberability/CoercetonumberAbility.ets", + "description": "$string:CoercetonumberAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CoercetonumberAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..50f8113573dd8e92840d65b0b65004281b61a7eb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CoercetonumberAbility_desc", + "value": "description" + }, + { + "name": "CoercetonumberAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..01f8e96410f91b292e88b73c2b73699e04119829 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCoerceToNumber', 0, async (done: Function) => { + console.log('uitest: TestCoerceToNumber begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CoercetonumberAbility' + } + 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('CoercetonumberAbility'); + + 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: TestCoerceToNumber end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3f1fb390b304c4030444ac6f0f4e6555cbeca33f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetonumber/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetonumber_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d0290aa78befac75ef1d38df39d6757d5c1f2fd4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "coercetoobject", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcoercetoobject.so": "file:./src/main/cpp/types/libcoercetoobject" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..39a94fca6df64390f3c13b79ba0b3a8f7d5f8e56 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPrimitiveOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(coercetoobject SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(coercetoobject PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b926aab8144d3b0032a5a2ec55f0639062716ca2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/hello.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_coerce_to_object] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_coerce_to_object] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_coerce_to_object] + +// OH_JSVM_CoerceToObject的样例方法 +static JSVM_Value CoerceToObject(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value obj = nullptr; + JSVM_Status status = OH_JSVM_CoerceToObject(env, args[0], &obj); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToObject failed"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToObject success"); + } + return obj; +} +// CoerceToObjec注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CoerceToObject}, +}; +static JSVM_CallbackStruct *method = param; +// CoerceToObject方法别名,ArkTS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"coerceToObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(coerceToObject(123))JS"; +// [End oh_jsvm_coerce_to_object] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "coercetoobject", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/types/libcoercetoobject/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/types/libcoercetoobject/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/types/libcoercetoobject/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/types/libcoercetoobject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/types/libcoercetoobject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c8f5a610569d426c407ea92011df136628eeec73 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/cpp/types/libcoercetoobject/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcoercetoobject.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/ets/coercetoobjectability/CoercetoobjectAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/ets/coercetoobjectability/CoercetoobjectAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..e4f782d492c8b9ea9f7c84f142a592d41d290a6f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/ets/coercetoobjectability/CoercetoobjectAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CoercetoobjectAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d412b0978190268f40b6c1c2c5f51eeb06d571d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcoercetoobject.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..90d5f155b28c362a2b58335b98394c88582b764e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetoobject", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CoercetoobjectAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CoercetoobjectAbility", + "srcEntry": "./ets/coercetoobjectability/CoercetoobjectAbility.ets", + "description": "$string:CoercetoobjectAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CoercetoobjectAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..15bf2a904381a5de6d5d8871c916ebab6234dfaf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CoercetoobjectAbility_desc", + "value": "description" + }, + { + "name": "CoercetoobjectAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..88e013b83f5c1b6499c7316f0a14347cfd586f1f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCoerceToObject', 0, async (done: Function) => { + console.log('uitest: TestCoerceToObject begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CoercetoobjectAbility' + } + 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('CoercetoobjectAbility'); + + 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: TestCoerceToObject end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6bd3133055637f3346afe7981435fe4fe09f3523 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetoobject/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetoobject_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0e658fa97fd84655cc251d36bf1e1e6fd93615ca --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "coercetostring", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcoercetostring.so": "file:./src/main/cpp/types/libcoercetostring" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..33b18fc51061679d1d392c08a8dfb511bdae7dbb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(coercetostring SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(coercetostring PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78878510f4698adbb9ac36eb36e6d90b9d13d0e5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/hello.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_coerce_to_string] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_coerce_to_string] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_coerce_to_string] + +// OH_JSVM_CoerceToString的样例方法 +static JSVM_Value CoerceToString(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value str = nullptr; + JSVM_Status status = OH_JSVM_CoerceToString(env, args[0], &str); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToString fail"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToString success"); + } + return str; +} +// CoerceToString注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CoerceToString}, +}; +static JSVM_CallbackStruct *method = param; +// CoerceToString方法别名,ArkTS侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"coerceToString", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(coerceToString(22222))JS"; +// [End oh_jsvm_coerce_to_string] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "coercetostring", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/types/libcoercetostring/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/types/libcoercetostring/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/types/libcoercetostring/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/types/libcoercetostring/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/types/libcoercetostring/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c239252cc90b3ac394d8cd278e4d90d01b6809a3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/cpp/types/libcoercetostring/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcoercetostring.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/ets/coercetostringability/CoercetostringAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/ets/coercetostringability/CoercetostringAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..ffbafce1af57adff0d83a2e88f94a811e20aa721 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/ets/coercetostringability/CoercetostringAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CoercetostringAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..ce2d63125e8724a62557b48c5c4a14e9dc9e247e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcoercetostring.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..10e89833e3e22a0b4bb1395c3590b42068bd922a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetostring", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CoercetostringAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CoercetostringAbility", + "srcEntry": "./ets/coercetostringability/CoercetostringAbility.ets", + "description": "$string:CoercetostringAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CoercetostringAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..bd1a49726a5776c88fe29f462547c7ce7930c8d1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CoercetostringAbility_desc", + "value": "description" + }, + { + "name": "CoercetostringAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..6d19394ce5c8866b545c05579f62f518487e25e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCoerceToString', 0, async (done: Function) => { + console.log('uitest: TestCoerceToString begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CoercetostringAbility' + } + 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('CoercetostringAbility'); + + 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: TestCoerceToString end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6b5ba124ce76d2f4797bc01aee2c350ed286b7e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/coercetostring/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "coercetostring_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3654c8deca14f73efd7ff9432e99c34463b13a73 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getboolean", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetboolean.so": "file:./src/main/cpp/types/libgetboolean" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5434154e397ff44ee2f80f089f5f277f5effbf96 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getboolean SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getboolean PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a5542c07999b5981b510e9468d1272cab56d6fc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/hello.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_boolean] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_boolean] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_boolean] + +// OH_JSVM_GetBoolean的样例方法 +static JSVM_Value GetBoolean(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 传入两个参数并解析 + size_t argc = 2; + JSVM_Value argv[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + int32_t paramData = 0; + OH_JSVM_GetValueInt32(env, argv[0], ¶mData); + int32_t paramValue = 0; + OH_JSVM_GetValueInt32(env, argv[1], ¶mValue); + JSVM_Value returnValue = nullptr; + bool type = false; + if (paramData == paramValue) { + OH_LOG_INFO(LOG_APP, "JSVM resultType equal"); + type = true; + } + JSVM_Status status = OH_JSVM_GetBoolean(env, type, &returnValue); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToNumber fail"); + } else { + bool result = false; + OH_JSVM_GetValueBool(env, returnValue, &result); + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToNumber success:%{public}d", result); + } + // 返回结果 + return returnValue; +} +// GetBoolean注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetBoolean}, +}; +static JSVM_CallbackStruct *method = param; +// GetBoolean方法别名,供JS调用 +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"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getboolean", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/types/libgetboolean/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/types/libgetboolean/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/types/libgetboolean/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/types/libgetboolean/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/types/libgetboolean/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c62d32f5c35555e3f255703bf3d7e55d83461781 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/cpp/types/libgetboolean/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetboolean.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/ets/getbooleanability/GetbooleanAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/ets/getbooleanability/GetbooleanAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..261b6b314dac068c8ab123f5636b9208b2dacb99 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/ets/getbooleanability/GetbooleanAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetbooleanAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..43b51fe15c6cfde2f77f0f601d8d733119c11118 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetboolean.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..65f18d7732e73c47bd030fb12c7bffee13c0f180 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getboolean", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetbooleanAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetbooleanAbility", + "srcEntry": "./ets/getbooleanability/GetbooleanAbility.ets", + "description": "$string:GetbooleanAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetbooleanAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b154bf6f562df9d22e74aaf87be79d04f1315aab --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetbooleanAbility_desc", + "value": "description" + }, + { + "name": "GetbooleanAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ca2482aa3ce45421d46c0516c41cb24af097520e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetBoolean', 0, async (done: Function) => { + console.log('uitest: TestGetBoolean begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetbooleanAbility' + } + 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('GetbooleanAbility'); + + 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: TestGetBoolean end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fd95e84ec930648d8169974e4757d78d88cc126e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getboolean/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getboolean_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7b5841f36f7d2531e0e34d9ac994ab6fbf2e32b4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getglobal", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetglobal.so": "file:./src/main/cpp/types/libgetglobal" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..17a118953061efd24fb0f890d0f12a38d311d889 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getglobal SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getglobal PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..337416b4d75166f21795681730942ea9392f8087 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/hello.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_global] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_global] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_global] + +// OH_JSVM_GetGlobal的样例方法 +static JSVM_Value GetGlobal(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取全局对象 + JSVM_Value value = nullptr; + JSVM_Value global = nullptr; + OH_JSVM_CreateInt32(env, 1, &value); + JSVM_Status status = OH_JSVM_GetGlobal(env, &global); + OH_JSVM_SetNamedProperty(env, global, "Row", value); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetGlobal fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetGlobal success"); + } + return global; +} +// GetGlobal注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetGlobal}, +}; +static JSVM_CallbackStruct *method = param; +// GetGlobal方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getGlobal", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(getGlobal())JS"; +// [End oh_jsvm_get_global] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getglobal", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/types/libgetglobal/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/types/libgetglobal/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/types/libgetglobal/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/types/libgetglobal/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/types/libgetglobal/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..88e8f331acbee420605e3be711a97ab75fb02d95 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/cpp/types/libgetglobal/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetglobal.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/ets/getglobalability/GetglobalAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/ets/getglobalability/GetglobalAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..c61f2cc27a334f5ef125952027f6e23288d74bcc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/ets/getglobalability/GetglobalAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetglobalAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7d0605383bdf64a3f723ca3b2b3070c8b928e516 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetglobal.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b5e64105ccd8616bcc89119cb9a8c6e04e6d2604 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getglobal", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetglobalAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetglobalAbility", + "srcEntry": "./ets/getglobalability/GetglobalAbility.ets", + "description": "$string:GetglobalAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetglobalAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..cdd44b9aa06645fb7ec24a28a0f2301d9cbc7f6e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetglobalAbility_desc", + "value": "description" + }, + { + "name": "GetglobalAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1068e965314ccc054879dd8b693561fc147ca25f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetGlobal', 0, async (done: Function) => { + console.log('uitest: TestGetGlobal begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetglobalAbility' + } + 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('GetglobalAbility'); + + 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: TestGetGlobal end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6fe66615a4a021864db9ff8e4bbf0a157929e870 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getglobal/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getglobal_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..aa2c7261d98b11eb436890f45111da6e3f524354 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getnull", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetnull.so": "file:./src/main/cpp/types/libgetnull" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b4921e4229aca3512db6d1513958f7822ea72f52 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getnull SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getnull PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36b5c80ed86842ad07fafe3f73387546e1017dda --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/hello.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_null] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_null] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_null] + +// OH_JSVM_GetNull的样例方法 +static JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value nullValue = nullptr; + JSVM_Status status = OH_JSVM_GetNull(env, &nullValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetNull fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetNull success"); + } + 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"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getnull", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/types/libgetnull/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/types/libgetnull/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/types/libgetnull/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/types/libgetnull/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/types/libgetnull/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5dfd54979da4cc954d4e4e753dc2e873753790c5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/cpp/types/libgetnull/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetnull.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/ets/getnullability/GetnullAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/ets/getnullability/GetnullAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..3b1db3c5dce0af822b6f88a3e79c10f68c7a2825 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/ets/getnullability/GetnullAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetnullAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..09ba916ba710749f1fec139db3fd688baeff6fc3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetnull.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..086197ee7b17c1dc84a54594724032932907856a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getnull", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetnullAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetnullAbility", + "srcEntry": "./ets/getnullability/GetnullAbility.ets", + "description": "$string:GetnullAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetnullAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..335bb4cf1648882a53584ef0f70ee84e3d81f2ee --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetnullAbility_desc", + "value": "description" + }, + { + "name": "GetnullAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f203e4a5c87f4e48e9bfa9399dfcc249c8343737 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetNull', 0, async (done: Function) => { + console.log('uitest: TestGetNull begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetnullAbility' + } + 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('GetnullAbility'); + + 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: TestGetNull end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3ebc4f295442faf1e6b287f17d6d3e77770643ba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getnull/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getnull_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2704b7a1c24bf0ab485c75cf0d5e12db0be0d8d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getundefined", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetundefined.so": "file:./src/main/cpp/types/libgetundefined" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..62d2397b1276b549d08dad89fa475951b1932f60 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getundefined SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getundefined PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5948f1f0b635c331fd34282c57d79982a88e03a4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_undefined] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_undefined] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_undefined] + +// OH_JSVM_GetUndefined的样例方法 +static JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取并解析传进的参数 + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 创建一个undefined值 + JSVM_Value value = nullptr; + JSVM_Status status = OH_JSVM_GetUndefined(env, &value); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetUndefined failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetUndefined success"); + } + 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"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getundefined", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/types/libgetundefined/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/types/libgetundefined/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/types/libgetundefined/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/types/libgetundefined/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/types/libgetundefined/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5390bebd13757f9fb09f62d3a741bd6ca086f145 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/cpp/types/libgetundefined/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetundefined.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/ets/getundefinedability/GetundefinedAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/ets/getundefinedability/GetundefinedAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..1dcce0364278e82d31393c0dc4c75a1c39ae5705 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/ets/getundefinedability/GetundefinedAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetundefinedAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0b300a0a348c5d2748eef6f02ac55b4ab279bc6a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetundefined.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..80aaf8561ea8cc8a10e63821ffc83256a84fec58 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getundefined", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetundefinedAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetundefinedAbility", + "srcEntry": "./ets/getundefinedability/GetundefinedAbility.ets", + "description": "$string:GetundefinedAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetundefinedAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..fc76dbea40a94722adb0222bcb2490d5760a1c37 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetundefinedAbility_desc", + "value": "description" + }, + { + "name": "GetundefinedAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..4c92820a1e256a7395ad3a94cdd25363ed42aba7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetUndefined', 0, async (done: Function) => { + console.log('uitest: TestGetUndefined begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetundefinedAbility' + } + 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('GetundefinedAbility'); + + 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: TestGetUndefined end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fca546749f1c8ea4b14016cb48e82387bd2a6391 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getundefined/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getundefined_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cac566efdba566922fdad8e81e1ad2d5d367c0e6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluebool", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluebool.so": "file:./src/main/cpp/types/libgetvaluebool" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6e30c606abfacecca6c83465bdce6a6a97800d13 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JSVMDemo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvaluebool SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluebool PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b62ccc71a72f90426e9f43d1a02501a6c30face5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_bool] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_value_bool] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_bool] + +// OH_JSVM_GetValueBool的样例方法 +static JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + bool result = false; + JSVM_Status status = OH_JSVM_GetValueBool(env, args[0], &result); + if (status == JSVM_BOOLEAN_EXPECTED || status != JSVM_OK) { + // 如果OH_JSVM_GetValueBool成功会返回JSVM_OK,如果传入一个非布尔值则会返回JSVM_BOOLEAN_EXPECTED + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetValueBool fail:%{public}d", status); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetValueBool success:%{public}d", result); + } + JSVM_Value boolJv = nullptr; + 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"; +// const char *SRC_CALL_NATIVE = R"JS(getValueBool(true))JS"; +// const char *SRC_CALL_NATIVE = R"JS(getValueBool(false))JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluebool", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/types/libgetvaluebool/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/types/libgetvaluebool/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/types/libgetvaluebool/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/types/libgetvaluebool/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/types/libgetvaluebool/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4d7135bafaa50e4a65dfa39a139e3d7c2dd91073 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/cpp/types/libgetvaluebool/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluebool.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/ets/getvalueboolability/GetvalueboolAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/ets/getvalueboolability/GetvalueboolAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..db1edcc600efcc237b437d0b7b4de03c4228f810 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/ets/getvalueboolability/GetvalueboolAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetvalueboolAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..839aea8595991df33f6a36e4b2ef472c43d451bd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluebool.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c53c24198f1a1a2bb21a535c1320536b71182cae --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebool", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetvalueboolAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetvalueboolAbility", + "srcEntry": "./ets/getvalueboolability/GetvalueboolAbility.ets", + "description": "$string:GetvalueboolAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetvalueboolAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8b441c740250d3504d82d114f0169da0aeccb67b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetvalueboolAbility_desc", + "value": "description" + }, + { + "name": "GetvalueboolAbility_label", + "value": "JsvmAboutPrimitive" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..e81c81c336eb43be0bf7072e3b7f54e235f08329 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueBool', 0, async (done: Function) => { + console.log('uitest: TestGetValueBool begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetvalueboolAbility' + } + 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('GetvalueboolAbility'); + + 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: TestGetValueBool end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6bbc2c38a34c09f37ed6a1d0e7b6df8bcfde7978 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/getvaluebool/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluebool_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..55b42fcca911e345ee7141dce598971ce4fbfd06 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/ohosTest.md @@ -0,0 +1,16 @@ +# JsvmAboutPrimitive 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 将目标值转换为Boolean类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 将目标值转换为Number类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 将目标值转换为Object类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 将目标值转换为String类型对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取JavaScript单例对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取当前环境中的全局global对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取JavaScript null | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取JavaScript undefined | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript Boolean的C布尔基础类型值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/screenshots/JsvmAboutPrimitive_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/screenshots/JsvmAboutPrimitive_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/screenshots/JsvmAboutPrimitive_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/screenshots/JsvmAboutPrimitive_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/screenshots/JsvmAboutPrimitive_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPrimitive/screenshots/JsvmAboutPrimitive_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..71b860edb4a68bee69990406803a34e9e941af4a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutpromise", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a5110316462c3cd08ca48bc5c489a70d4ba7cbfc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutPromise" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/README.md new file mode 100644 index 0000000000000000000000000000000000000000..187c00e674a9d50c53c4388aa5a80c8716ad20b3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/README.md @@ -0,0 +1,92 @@ +# ArkTS使用JSVM-API接口处理异步操作 + +### 介绍 + +使用JSVM-API接口处理异步操作。异步操作是指需要一定时间才能完成的操作,例如从网络下载数据或读取大型文件。与同步操作不同,异步操作不会阻塞主线程,而是会在后台执行。当异步操作完成后,事件循环将把它放入任务队列中,等待主线程空闲时执行。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口处理异步操作](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-promise.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :------------------------------------------------------------------: | :------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +ispromise/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +resolvereject/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libresolvereject + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── resolverejectability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ec066f93e07151a9484c3d7d54776e4a046a346d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/build-profile.json5 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "ispromise", + "srcPath": "./ispromise", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "resolvereject", + "srcPath": "./resolvereject", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..bcc8778338ccd731ac0e51f2bdf3e8c84d9d1485 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "ispromise", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libispromise.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e647ce8e15d09a54a3a12666f1311fe39aacefc1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPromise) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(ispromise SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(ispromise PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f945813dfa4f721a1550a92d85eb2b7119610825 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/hello.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_ispromise] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [EndExclude oh_jsvm_ispromise] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_ispromise] + +// OH_JSVM_IsPromise的样例方法 +static JSVM_Value IsPromise(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + bool isPromise = false; + JSVM_Status status = OH_JSVM_IsPromise(env, args[0], &isPromise); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_IsPromise fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_IsPromise success:%{public}d", isPromise); + } + JSVM_Value result = nullptr; + OH_JSVM_GetBoolean(env, isPromise, &result); + return result; +} +// IsPromise注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsPromise}, +}; +static JSVM_CallbackStruct *method = param; +// IsPromise方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isPromise", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(isPromise())JS"; +// [End oh_jsvm_ispromise] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "ispromise", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..707408e357296ba0f66dfe1cf27499b3b0202b2c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libispromise.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..6b05ea2e7e2859cec0713ea7ad73f4b6ddfe6f15 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libispromise.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..71b05a2821f709b7c21afb01c7fc08c3bc779b4c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "ispromise", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c1bed36d6ff3a162a03c47153d591630fa1542c4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutPromise" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ab03a77d002ae54282537aa96e5b82da83e85cb5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsPromise', 0, async (done: Function) => { + console.log('uitest: TestIsPromise 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: TestIsPromise end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fff774cdd338fe5c8d6a4f6fa8755bebc09a45e0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ispromise/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "ispromise_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..55a080857346281ad06a712a3e942064ad952205 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/ohosTest.md @@ -0,0 +1,9 @@ +# JsvmAboutDate 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------- | ------------ | ------------------------ | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 查询Promise是否为Promise对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 通过与之关联的延迟对象来解析JavaScript promise | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b3562ec8fe355ea6be4469d92d4109d0214cd045 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "resolvereject", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libresolvereject.so": "file:./src/main/cpp/types/libresolvereject" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..5d08cc789ed1300d8f38176a7582dfce1cd36cb7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPromise) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(resolvereject SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(resolvereject PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fae2725f78d59c77714d2da1a2ea8cf1c8f568bf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/hello.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_resolvedeferred_and_rejectdeferred] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_resolvedeferred_and_rejectdeferred] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_resolvedeferred_and_rejectdeferred] + +// OH_JSVM_CreatePromise、OH_JSVM_ResolveDeferred、OH_JSVM_RejectDeferred的样例方法 +static JSVM_Value CreatePromise(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Deferred defer = nullptr; + JSVM_Value promise = nullptr; + JSVM_Status status = OH_JSVM_CreatePromise(env, &defer, &promise); + bool isPromise = false; + JSVM_Value returnIsPromise = nullptr; + OH_JSVM_IsPromise(env, promise, &isPromise); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreatePromise fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreatePromise success:%{public}d", isPromise); + } + // 将布尔值转为可以返回的JSVM_Value + OH_JSVM_GetBoolean(env, isPromise, &returnIsPromise); + return returnIsPromise; +} + +static JSVM_Value ResolveRejectDeferred(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获得并解析参数 + size_t argc = 3; + JSVM_Value args[3] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 第一个参数为向resolve传入的信息,第二个参数为向reject传入的信息,第三个参数为Promise的状态 + bool status = false; + constexpr size_t PROMISE_STATUS_ARG_INDEX = 2; + OH_JSVM_GetValueBool(env, args[PROMISE_STATUS_ARG_INDEX], &status); + // 创建Promise对象 + JSVM_Deferred deferred = nullptr; + JSVM_Value promise = nullptr; + JSVM_Status createStatus = OH_JSVM_CreatePromise(env, &deferred, &promise); + if (createStatus != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "Create promise failed"); + return nullptr; + } + // 根据第三个参数设置resolve或reject + if (status) { + OH_JSVM_ResolveDeferred(env, deferred, args[0]); + OH_LOG_INFO(LOG_APP, "OH_JSVM_ResolveDeferred resolve"); + } else { + OH_JSVM_RejectDeferred(env, deferred, args[1]); + OH_LOG_INFO(LOG_APP, "OH_JSVM_RejectDeferred reject"); + } + JSVM_Value result = nullptr; + OH_JSVM_GetBoolean(env, true, &result); + return result; +} +// CreatePromise,ResolveRejectDeferred注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreatePromise}, + {.data = nullptr, .callback = ResolveRejectDeferred}, +}; +static JSVM_CallbackStruct *method = param; +// CreatePromise,ResolveRejectDeferred方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createPromise", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + {"resolveRejectDeferred", nullptr, method+1, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试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"; +// [End oh_jsvm_resolvedeferred_and_rejectdeferred] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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_CREATE_PROMISE, JSVM_AUTO_LENGTH, &jsSrc)); + CHECK_RET(OH_JSVM_CreateStringUtf8(env, SRC_CALL_NATIVE_RESOLVE_REJECT_DEFERRED1, JSVM_AUTO_LENGTH, &jsSrc)); + CHECK_RET(OH_JSVM_CreateStringUtf8(env, SRC_CALL_NATIVE_RESOLVE_REJECT_DEFERRED2, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "resolvereject", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/types/libresolvereject/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/types/libresolvereject/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/types/libresolvereject/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/types/libresolvereject/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/types/libresolvereject/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7d2e60a9d671fd0303863bfe5e43fe38d43e068e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/cpp/types/libresolvereject/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libresolvereject.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..88be5d10c014ec407c3e88106abe24ea32700f10 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libresolvereject.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/ets/resolverejectability/ResolverejectAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/ets/resolverejectability/ResolverejectAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..ab6c16a445d02d9e91baf61403628522ff2161fe --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/ets/resolverejectability/ResolverejectAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ResolverejectAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4ca7fde00d542389fdc9cdd7fe7f4f55eb475a31 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "resolvereject", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ResolverejectAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ResolverejectAbility", + "srcEntry": "./ets/resolverejectability/ResolverejectAbility.ets", + "description": "$string:ResolverejectAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ResolverejectAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..fbeb05689ef8c530a803b8b781c07e192b9fc49b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ResolverejectAbility_desc", + "value": "description" + }, + { + "name": "ResolverejectAbility_label", + "value": "JsvmAboutPromise" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f263aadacf623aecdeb319d45ed1172a69a7eb57 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testResolveReject', 0, async (done: Function) => { + console.log('uitest: TestResolveReject begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ResolverejectAbility' + } + 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('ResolverejectAbility'); + + 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: TestResolveReject end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..76eb1ad89d26e4a2dfeb456faed39cb817368fe0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/resolvereject/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "resolvereject_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/screenshots/JsvmAboutPromise_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/screenshots/JsvmAboutPromise_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/screenshots/JsvmAboutPromise_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/screenshots/JsvmAboutPromise_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/screenshots/JsvmAboutPromise_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutPromise/screenshots/JsvmAboutPromise_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0c4891ad144e5c3d9a9c08cd66146fa9ece4aaf2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutproperty", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..64d18ea938ce3945542ac6ce460d164571e9b212 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutProperty" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a67136eabe485f77c359cebe861a82da0c6c8955 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/README.md @@ -0,0 +1,263 @@ +# ArkTs使用JSVM-API接口设置JavaScript对象的属性 + +### 介绍 + +使用JSVM-API接口获取和设置JavaScript对象的属性。通过合理使用这些函数,实现更复杂的功能和逻辑。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口设置JavaScript对象的属性](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-property.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-------------------------------------------------------------------: | :-------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +defineproperties/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libdefineproperties + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── definepropertiesability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +deleteproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libdeleteproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── deletepropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getallpropertynames/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetallpropertynames + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getallpropertynamesability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getnamedproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetnamedproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getnamedpropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getpropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getpropertynames/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +hasnamedproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libhasnamedproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── hasnamedpropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +hasownproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libhasownproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── hasownpropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +hasproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libhasproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── haspropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +setnamedproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libsetnamedproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── setnamedpropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +setproperty/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libsetproperty + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── setpropertyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8814ba9883bf464f95fb159ea287689170ee40ba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/build-profile.json5 @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getpropertynames", + "srcPath": "./getpropertynames", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getproperty", + "srcPath": "./getproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "deleteproperty", + "srcPath": "./deleteproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getnamedproperty", + "srcPath": "./getnamedproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "defineproperties", + "srcPath": "./defineproperties", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getallpropertynames", + "srcPath": "./getallpropertynames", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "setproperty", + "srcPath": "./setproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "hasproperty", + "srcPath": "./hasproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "hasownproperty", + "srcPath": "./hasownproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "setnamedproperty", + "srcPath": "./setnamedproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "hasnamedproperty", + "srcPath": "./hasnamedproperty", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..25cd3b53558e03890f999d6db503aaba13589b48 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "defineproperties", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libdefineproperties.so": "file:./src/main/cpp/types/libdefineproperties" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6dab316b05832d38a6d52fe12f6e9860d6c66599 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(defineproperties SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(defineproperties PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0816302d41e0f9b22ad1734aeae73151793a05a4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/hello.cpp @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +#include + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" +#define MAX_MALLOC_SIZE 0x800000 + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_define_properties] +// 属性描述符列表中defineMethodPropertiesExample属性的回调函数 +static JSVM_Value DefineMethodPropertiesExample(JSVM_Env env, JSVM_CallbackInfo info) +{ + int32_t propValue = 26; + JSVM_Value returnValue; + OH_JSVM_CreateInt32(env, propValue, &returnValue); + return returnValue; +} +// 属性描述符列表中getterCallback属性的回调函数 +static JSVM_Value GetterCallback(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value result; + const char *str = "Hello world!"; + size_t length = strlen(str); + // 创建属性的值 + OH_JSVM_CreateStringUtf8(env, str, length, &result); + return result; +} + +// 执行JavaScript字符串的函数 +static JSVM_Value RunScriptAndLogResult(JSVM_Env env, const std::string &srcCode) +{ + JSVM_Value sourceCodeValue; + OH_JSVM_CreateStringUtf8(env, srcCode.c_str(), srcCode.size(), &sourceCodeValue); + JSVM_Script script; + // 编译JavaScript代码字符串并返回编译后的脚本 + OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script); + JSVM_Value jsVmResult; + // 执行JavaScript代码字符串 + OH_JSVM_RunScript(env, script, &jsVmResult); + return jsVmResult; +} + +// OH_JSVM_DefineProperties的样例方法 +static JSVM_Value DefineProperties(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接受一个JavaScript侧传入的空object + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + // 创建一个string类型的属性值 + JSVM_Value stringValue; + OH_JSVM_CreateStringUtf8(env, "Hello!", JSVM_AUTO_LENGTH, &stringValue); + // 创建属性描述符对应的回调函数列表 + JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = DefineMethodPropertiesExample}, + {.data = nullptr, .callback = GetterCallback}, + + }; + // 创建属性描述符列表,不同类型属性值添加位置参考JSVM_PropertyDescriptor定义 + JSVM_PropertyDescriptor descriptor[] = { + // 定义method类型的属性值 + {"defineMethodPropertiesExample", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT}, + // 定义string类型的属性值 + {"defineStringPropertiesExample", nullptr, nullptr, nullptr, nullptr, stringValue, JSVM_DEFAULT}, + // 定义getter类型的属性值 + {"getterCallback", nullptr, nullptr, ¶m[1], nullptr, nullptr, JSVM_DEFAULT}}; + // 根据属性描述符列表为obj对象创建属性 + JSVM_Status statusProperty = OH_JSVM_DefineProperties(env, + *argv, + sizeof(descriptor) / sizeof(descriptor[0]), + descriptor); + if (statusProperty != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM DefineProperties fail"); + return nullptr; + } + // 调用obj对象中添加的属性 + // 运行obj.defineMethodPropertiesExample()并将结果返回给JavaScript + std::string srcMethod = R"JS(obj.defineMethodPropertiesExample();)JS"; + JSVM_Value jsVmResult = RunScriptAndLogResult(env, srcMethod); + if (jsVmResult != nullptr) { + int32_t number; + OH_JSVM_GetValueInt32(env, jsVmResult, &number); + OH_LOG_INFO(LOG_APP, "JSVM DefineMethodPropertiesExample success:%{public}d", number); + } + // 运行obj.defineStringPropertiesExample()并将结果返回给JavaScript + static std::string srcString = R"JS(obj.defineStringPropertiesExample;)JS"; + JSVM_Value jsVmResult1 = RunScriptAndLogResult(env, srcString); + if (jsVmResult1 != nullptr) { + size_t length = 0; + OH_JSVM_GetValueStringUtf8(env, jsVmResult1, nullptr, 0, &length); + if (length == 0 || length > MAX_MALLOC_SIZE) { + OH_LOG_ERROR(LOG_APP, "Invalid string length: %{public}zu", length); + return jsVmResult; + } + char *buf = (char *)malloc(length + 1); + OH_JSVM_GetValueStringUtf8(env, jsVmResult1, buf, length + 1, &length); + OH_LOG_INFO(LOG_APP, "JSVM defineStringPropertiesExample success:%{public}s", buf); + free(buf); + } + // 调用obj的getterCallback()并将结果字符串返回给JavaScript + static std::string srcGetter = R"JS(obj.getterCallback;)JS"; + JSVM_Value jsVmResult2 = RunScriptAndLogResult(env, srcGetter); + if (jsVmResult2 != nullptr) { + size_t length = 0; + OH_JSVM_GetValueStringUtf8(env, jsVmResult2, nullptr, 0, &length); + OH_JSVM_GetValueStringUtf8(env, jsVmResult1, nullptr, 0, &length); + if (length == 0 || length > MAX_MALLOC_SIZE) { + OH_LOG_ERROR(LOG_APP, "Invalid string length: %{public}zu", length); + return jsVmResult; + } + char *buf = (char *)malloc(length + 1); + OH_JSVM_GetValueStringUtf8(env, jsVmResult2, buf, length + 1, &length); + OH_LOG_INFO(LOG_APP, "JSVM getterCallback success:%{public}s", buf); + free(buf); + } + return jsVmResult; +} + +// DefineProperties注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = DefineProperties}, +}; +static JSVM_CallbackStruct *method = param; +// DefineProperties方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"defineProperties", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = {}; + defineProperties(obj) +)JS"; +// [End oh_jsvm_define_properties] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "defineproperties", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/types/libdefineproperties/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/types/libdefineproperties/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/types/libdefineproperties/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/types/libdefineproperties/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/types/libdefineproperties/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..710ab15309650a8cea37526fcc4d27cd4620a9de --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/cpp/types/libdefineproperties/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libdefineproperties.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/ets/definepropertiesability/DefinepropertiesAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/ets/definepropertiesability/DefinepropertiesAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..749d8a72ea436f34f645eee84c1e680f4815cf82 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/ets/definepropertiesability/DefinepropertiesAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class DefinepropertiesAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..852bf54d05b6db3f667570e75fd10ab4b72fb429 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libdefineproperties.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a83b27d31234e7ff997725821a71ee680a414ba9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "defineproperties", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "DefinepropertiesAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "DefinepropertiesAbility", + "srcEntry": "./ets/definepropertiesability/DefinepropertiesAbility.ets", + "description": "$string:DefinepropertiesAbility_desc", + "icon": "$media:layered_image", + "label": "$string:DefinepropertiesAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f18a7b148c64f2507c5f8ae0be57389f2c993429 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "DefinepropertiesAbility_desc", + "value": "description" + }, + { + "name": "DefinepropertiesAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d008fe49316e44ac62ef1f907552150e5a58c7ef --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testDefineProperties', 0, async (done: Function) => { + console.log('uitest: TestDefineProperties begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'DefinepropertiesAbility' + } + 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('DefinepropertiesAbility'); + + 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: TestDefineProperties end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7f0304213fd029544b34175970ebeb1c443bf015 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/defineproperties/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "defineproperties_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7fb26b79ca413ee4636b80f3524f30cd77e6504 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "deleteproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libdeleteproperty.so": "file:./src/main/cpp/types/libdeleteproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ae8663537b8bd8811d237d88bafb31d69e5bda6c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(deleteproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(deleteproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d375545847f355cca3092a76f1193c79be49aac --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/hello.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_delete_property] +// OH_JSVM_DeleteProperty的样例方法 +static JSVM_Value DeleteProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_ValueType valueType; + OH_JSVM_Typeof(env, args[0], &valueType); + if (valueType != JSVM_OBJECT) { + OH_JSVM_ThrowError(env, nullptr, "Expects an object as argument."); + return nullptr; + } + // 从传入的Object对象中删除指定属性,返回是否删除成功的bool结果值 + bool result = false; + JSVM_Status status = OH_JSVM_DeleteProperty(env, args[0], args[1], &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_DeleteProperty failed"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_DeleteProperty success:%{public}d", result); + } + // 将bool结果转换为JSVM_value并返回 + JSVM_Value ret; + OH_JSVM_GetBoolean(env, result, &ret); + return ret; +} +// DeleteProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = DeleteProperty}, +}; +static JSVM_CallbackStruct *method = param; +// DeleteProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"deleteProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + deleteProperty(obj, "message") +)JS"; +// [End oh_jsvm_delete_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "deleteproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/types/libdeleteproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/types/libdeleteproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/types/libdeleteproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/types/libdeleteproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/types/libdeleteproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c7abd6bc79771f585ea06c8ab65284eecb2e9ee0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/cpp/types/libdeleteproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libdeleteproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/ets/deletepropertyability/DeletepropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/ets/deletepropertyability/DeletepropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..d8c5eb971639b23199cc83b59eaef8ae6bfd32b3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/ets/deletepropertyability/DeletepropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class DeletepropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b94ebea74230f957085188bb5679ed827b5ca538 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libdeleteproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f7ba360423f48f1fe4577f2c637b6c5fc88d89b0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "deleteproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "DeletepropertyAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "DeletepropertyAbility", + "srcEntry": "./ets/deletepropertyability/DeletepropertyAbility.ets", + "description": "$string:DeletepropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:DeletepropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..8465ed8521db075ec583c8076aa6a5d7241c6b61 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "DeletepropertyAbility_desc", + "value": "description" + }, + { + "name": "DeletepropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..54839fe37e9e2ae98482821ae33185fbffc4c7cb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testDeleteProperty', 0, async (done: Function) => { + console.log('uitest: TestDeleteProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'DeletepropertyAbility' + } + 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('DeletepropertyAbility'); + + 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: TestDeleteProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d2f9fb4e8d7a26713fc3958f1377ba7b5b7aca29 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/deleteproperty/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "deleteproperty_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..42c4974bcb397675bb910b4bbb5540ba651b91cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getallpropertynames", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetallpropertynames.so": "file:./src/main/cpp/types/libgetallpropertynames" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..dddd5f5e2026c9307c176214d471781551e1c881 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getallpropertynames SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getallpropertynames PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69e11fe1263de7657be8637dbf0745f08e94e4ec --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/hello.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_all_property_names] +// OH_JSVM_GetAllPropertyNames的样例方法 +static JSVM_Value GetAllPropertyNames(JSVM_Env env, JSVM_CallbackInfo info) +{ + // // 获取js侧传入的一个参数 + size_t argc = 1; + JSVM_Value args[1]; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取给定对象的所有属性名称(自有属性) + JSVM_Value result; + JSVM_Status status = OH_JSVM_GetAllPropertyNames(env, args[0], + JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY, + JSVM_KeyFilter::JSVM_KEY_WRITABLE, + JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetAllPropertyNames success"); + } + return result; +} +// GetAllPropertyNames注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetAllPropertyNames}, +}; +static JSVM_CallbackStruct *method = param; +// GetAllPropertyNames方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getAllPropertyNames", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = '{ data: 0, message: "hello world", 50: 1}'; + let script = getAllPropertyNames(obj); +)JS"; +// [End oh_jsvm_get_all_property_names] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getallpropertynames", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/types/libgetallpropertynames/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/types/libgetallpropertynames/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/types/libgetallpropertynames/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/types/libgetallpropertynames/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/types/libgetallpropertynames/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..bf650222f63023c8d241ec0cc69d591e4ed12ddf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/cpp/types/libgetallpropertynames/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetallpropertynames.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/getallpropertynamesability/GetallpropertynamesAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/getallpropertynamesability/GetallpropertynamesAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..12dcb64dcc33babd53c80a443a5f8f884391ae52 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/getallpropertynamesability/GetallpropertynamesAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetallpropertynamesAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..ed6c97303b32a0dcee8946234e7ec6d9302352b2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetallpropertynames.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b93bf158ee7561d3e44d88ba2cdac14f5ddcff48 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getallpropertynames", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetallpropertynamesAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetallpropertynamesAbility", + "srcEntry": "./ets/getallpropertynamesability/GetallpropertynamesAbility.ets", + "description": "$string:GetallpropertynamesAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetallpropertynamesAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..5312574924688346fb3acacc71d6b7afa06d7c8a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetallpropertynamesAbility_desc", + "value": "description" + }, + { + "name": "GetallpropertynamesAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..568b414f12aafad3484cd0bef381bc09a5181d70 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetAllPropertyNames', 0, async (done: Function) => { + console.log('uitest: TestGetAllPropertyNames begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetallpropertynamesAbility' + } + 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('GetallpropertynamesAbility'); + + 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: TestGetAllPropertyNames end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..74e9dc18bc10eb2532a228ea6ae10fd5514ffb4b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getallpropertynames/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getallpropertynames_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d9a8c8ec95cdff1d3be0e46f7d830a8debc48ddd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getnamedproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetnamedproperty.so": "file:./src/main/cpp/types/libgetnamedproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..56df704efeae570392c065e17094402ec2fca036 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getnamedproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getnamedproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27e398df1f077fb186b41582eec1d4214f928fdc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/hello.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_named_property] +// OH_JSVM_GetNamedProperty的样例方法 +const size_t MAX_KEY_LENGTH = 32; +static JSVM_Value GetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + char strKey[MAX_KEY_LENGTH] = ""; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取要获取的属性名 + size_t keyLength; + OH_JSVM_GetValueStringUtf8(env, args[1], strKey, MAX_KEY_LENGTH, &keyLength); + // 获取指定属性的值并存储在result中 + JSVM_Value result; + JSVM_Status status = OH_JSVM_GetNamedProperty(env, args[0], strKey, &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetNamedProperty failed"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetNamedProperty success"); + } + return result; +} +// GetNamedProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetNamedProperty}, +}; +static JSVM_CallbackStruct *method = param; +// GetNamedProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getNamedProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + getNamedProperty(obj, "message") +)JS"; +// [End oh_jsvm_get_named_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getnamedproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/types/libgetnamedproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/types/libgetnamedproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/types/libgetnamedproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/types/libgetnamedproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/types/libgetnamedproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..86fbcae9a1416992e1ec6c34235000d8b05b9c13 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/cpp/types/libgetnamedproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetnamedproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/ets/getnamedpropertyability/GetnamedpropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/ets/getnamedpropertyability/GetnamedpropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..da89e6399305b705021ed4ce153eebb371580179 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/ets/getnamedpropertyability/GetnamedpropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetnamedpropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..6e6d8624a9ecc33f58561ae808d2edcdc451f694 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetnamedproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5ee6d3e7e38eb040d39a92f82b6f6a1c6011f64b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getnamedproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetnamedpropertyAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetnamedpropertyAbility", + "srcEntry": "./ets/getnamedpropertyability/GetnamedpropertyAbility.ets", + "description": "$string:GetnamedpropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetnamedpropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d26d5177167c0b2a2654664f9424d43e3d094573 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetnamedpropertyAbility_desc", + "value": "description" + }, + { + "name": "GetnamedpropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..653253a107d94ec9c1da43ba1f28c33565d0fabc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetNamedProperty', 0, async (done: Function) => { + console.log('uitest: TestGetNamedProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetnamedpropertyAbility' + } + 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('GetnamedpropertyAbility'); + + 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: TestGetNamedProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b3e3350a842df43bc8e4d5330facb7ba1a6281cd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getnamedproperty/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getnamedproperty_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e92939fc442913bab8ca2f39640fe1f1a6d54662 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetproperty.so": "file:./src/main/cpp/types/libgetproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..dc0ca19546bc0921776d867bf8b2296d37de0005 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPropertyOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c4a2052105293182acdf03694118d2ebfb5bd34 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_property] +// OH_JSVM_GetProperty的样例方法 +static JSVM_Value GetProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接收两个js传来的参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 传入的第一个参数为要检测的object,第二个对象为要检测的属性,通过调用OH_JSVM_GetProperty接口获取对应的值 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetProperty(env, args[0], args[1], &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetProperty fail"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetProperty success"); + } + return result; +} +// GetProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetProperty}, +}; +static JSVM_CallbackStruct *method = param; +// GetProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + getProperty(obj, "message") +)JS"; +// [End oh_jsvm_get_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/types/libgetproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/types/libgetproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/types/libgetproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/types/libgetproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/types/libgetproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7d7e24664065e6522a70c1fd550efc0b22077a8f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/cpp/types/libgetproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/ets/getpropertyability/GetpropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/ets/getpropertyability/GetpropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..ee69dcb61d7fce0b1c4226b73b0a618e45849c10 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/ets/getpropertyability/GetpropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetpropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b32ce360e2b6e5c0e1b3c360fb61dd24fdde1ca3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..06ee603a1bc3cfb080834a5133f04c45e3ecd745 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetpropertyAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetpropertyAbility", + "srcEntry": "./ets/getpropertyability/GetpropertyAbility.ets", + "description": "$string:GetpropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetpropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c402a0e57513ba5ab54ef60989d4e7aeb92c18e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetpropertyAbility_desc", + "value": "description" + }, + { + "name": "GetpropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c5af891cc641ba6515b56ad772baa578cd597854 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetProperty', 0, async (done: Function) => { + console.log('uitest: TestGetProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetpropertyAbility' + } + 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('GetpropertyAbility'); + + 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: TestGetProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2bc62a8afd027b6accd811d1a1d0e3ce9bc7b600 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getproperty/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getproperty_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d8ac2accaf5c81eb8794f2490b0c9b2da23d2353 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getpropertynames", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetpropertynames.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4546d46454ba2375aa4c83606ebc602b1e47b314 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPropertyOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getpropertynames SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getpropertynames PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbf79e1814442f39913543ad3a9dc86341b4f450 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_property_names] +// OH_JSVM_GetPropertyNames的样例方法 +static JSVM_Value GetPropertyNames(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 将obj作为参数传入 + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 以字符串数组的形式获取对象的可枚举属性的名称,以result传出 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetPropertyNames(env, args[0], &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "Failed to get propertynames"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetPropertyNames success"); + } + return result; +} +// GetPropertyNames注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetPropertyNames}, +}; +static JSVM_CallbackStruct *method = param; +// GetPropertyNames方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getPropertyNames", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = '{ data: 0, message: "hello world"}'; + let script = getPropertyNames(obj); +)JS"; +// [End oh_jsvm_get_property_names] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getpropertynames", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a55ca35c9e174941b7c70c4564d033202b63cf22 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetpropertynames.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b9f77ef80afe9b61cc5f8dc6ae2707a9828db563 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetpropertynames.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..778f1d8bd1e948d0260215a4dcafac9383f647fb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getpropertynames", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1fb1f941578b22e364953323a32ac7b001b6d733 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..668e57697d7c1de1bf2ff11e40633f224ba67bba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetPropertyNames', 0, async (done: Function) => { + console.log('uitest: TestGetPropertyNames 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: TestGetPropertyNames end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..08449f7be7094146d6a4c4812b518798bbe9eb5c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/getpropertynames/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getpropertynames_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cf1fafa36b910633acec8d147e3839b0a17ac733 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "hasnamedproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libhasnamedproperty.so": "file:./src/main/cpp/types/libhasnamedproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..395b46e09c4a169c5dc2693635a3e7b0887d183f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(hasnamedproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(hasnamedproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d75ad1e0ba37aa0a817e2bc40513c38df9e4d767 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/hello.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_has_named_property] +// OH_JSVM_HasNamedProperty的样例方法 +const size_t STR_KEY_LEN = 32; +static JSVM_Value HasNamedProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + char strKey[STR_KEY_LEN] = ""; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 获取要检查的属性名 + size_t keyLength; + OH_JSVM_GetValueStringUtf8(env, args[1], strKey, STR_KEY_LEN, &keyLength); + // 检查对象是否具有指定命名的属性,并将结果存储在hasProperty中 + bool hasProperty = false; + JSVM_Status status = OH_JSVM_HasNamedProperty(env, args[0], strKey, &hasProperty); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasNamedProperty failed"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_HasNamedProperty success:%{public}d", hasProperty); + } + // 将bool结果转换为JSVM_Value并返回 + JSVM_Value result; + OH_JSVM_GetBoolean(env, hasProperty, &result); + return result; +} +// HasNamedProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = HasNamedProperty}, +}; +static JSVM_CallbackStruct *method = param; +// HasNamedProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"hasNamedProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + hasNamedProperty(obj, "message") +)JS"; +// [End oh_jsvm_has_named_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "hasnamedproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/types/libhasnamedproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/types/libhasnamedproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/types/libhasnamedproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/types/libhasnamedproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/types/libhasnamedproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cec57929cdb14006c0766a8365dc8167d704d5b8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/cpp/types/libhasnamedproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libhasnamedproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/ets/hasnamedpropertyability/HasnamedpropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/ets/hasnamedpropertyability/HasnamedpropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..f0a3a72706651fa3ec7a9375a57dd10c9302f108 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/ets/hasnamedpropertyability/HasnamedpropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class HasnamedpropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..f700231cbecdb0495c08d32210f9de9a3c754015 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libhasnamedproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5ce858194fea57278ac9679f0bbf352df5317aa1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "hasnamedproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "HasnamedpropertyAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "HasnamedpropertyAbility", + "srcEntry": "./ets/hasnamedpropertyability/HasnamedpropertyAbility.ets", + "description": "$string:HasnamedpropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:HasnamedpropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a5607b54b5eb3d10df277f010ae0993eabee9812 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "HasnamedpropertyAbility_desc", + "value": "description" + }, + { + "name": "HasnamedpropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..584048aa60bdf42f6d7c556fedbccd814ad41b32 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testHasNamedProperty', 0, async (done: Function) => { + console.log('uitest: TestHasNamedProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'HasnamedpropertyAbility' + } + 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('HasnamedpropertyAbility'); + + 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: TestHasNamedProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ce195d251d6f2b936b89f08c151a19724693bdf1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasnamedproperty/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "hasnamedproperty_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a276285db6df023ad6ad880be2ef6b5311299757 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "hasownproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libhasownproperty.so": "file:./src/main/cpp/types/libhasownproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..945aa4eaab769ca88f45715343a9c8b7a0fe7e34 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(hasownproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(hasownproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45d47c53644228f5f32a415dc9b3105017650d76 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/hello.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_has_own_property] +// OH_JSVM_HasOwnProperty的样例方法 +static JSVM_Value HasOwnProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的两个参数 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 检查第一个参数是否为对象 + JSVM_ValueType valueType1; + OH_JSVM_Typeof(env, args[0], &valueType1); + if (valueType1 != JSVM_OBJECT) { + OH_JSVM_ThrowError(env, nullptr, "First argument must be an object."); + return nullptr; + } + // 检查第二个参数是否为string + JSVM_ValueType valuetype2; + OH_JSVM_Typeof(env, args[1], &valuetype2); + if (valuetype2 != JSVM_STRING) { + OH_JSVM_ThrowError(env, nullptr, "Second argument must be a string."); + return nullptr; + } + // 检查对象是否具有指定属性,结果存储在hasProperty中 + bool hasProperty; + JSVM_Status status = OH_JSVM_HasOwnProperty(env, args[0], args[1], &hasProperty); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasOwnProperty failed"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_HasOwnProperty success:%{public}d", hasProperty); + } + // 将bool结果转换为JSVM_Value并返回 + JSVM_Value result; + OH_JSVM_GetBoolean(env, hasProperty, &result); + return result; +} +// HasOwnProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = HasOwnProperty}, +}; +static JSVM_CallbackStruct *method = param; +// HasOwnProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"hasOwnProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + hasOwnProperty(obj, "message") + hasOwnProperty(obj, "__defineGetter__") +)JS"; +// [End oh_jsvm_has_own_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "hasownproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/types/libhasownproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/types/libhasownproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/types/libhasownproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/types/libhasownproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/types/libhasownproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b759b773e775174ae9abd261fd629d4d1f4dedf5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/cpp/types/libhasownproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libhasownproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/ets/hasownpropertyability/HasownpropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/ets/hasownpropertyability/HasownpropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..cd36fd79e31870b544ddadb0a8c417e9dc78a4dd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/ets/hasownpropertyability/HasownpropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class HasownpropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..199480e7dca7ed7c6e64e8c7824dc9caa477ca09 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libhasownproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..39f936321adcf422c62fa5b226bcb87a3661363d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "hasownproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "HasownpropertyAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "HasownpropertyAbility", + "srcEntry": "./ets/hasownpropertyability/HasownpropertyAbility.ets", + "description": "$string:HasownpropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:HasownpropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b96f1cf66c1a79243695bb7e26bf6c8de95473d6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "HasownpropertyAbility_desc", + "value": "description" + }, + { + "name": "HasownpropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c219ee83170cde2558b51aaa5bdc5a86b6cf5245 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testHasOwnProperty', 0, async (done: Function) => { + console.log('uitest: TestHasOwnProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'HasownpropertyAbility' + } + 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('HasownpropertyAbility'); + + 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: TestHasOwnProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0112adaed88ae70e13326dd38d85091eab5f4f2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasownproperty/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "hasownproperty_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..516888132a73ffaedce5839c6a61c0dfb3760204 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "hasproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libhasproperty.so": "file:./src/main/cpp/types/libhasproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e6f672a727efb1645fb2ef3cf29a2afa055b4065 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPropertyOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(hasproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(hasproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8106190d3a9ebf48005179da53a6f01a8c9b1ee --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/hello.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_has_property] +// OH_JSVM_HasProperty的样例方法 +static JSVM_Value HasProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 从js侧传入两个参数:第一个参数为要检验的对象,第二个参数为要检测是否存在对象的属性 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 将参数传入OH_JSVM_HasProperty方法中,若接口调用成功则将结果转化为JSVM_Value类型抛出,否则抛出错误 + bool result; + JSVM_Status status = OH_JSVM_HasProperty(env, args[0], args[1], &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasProperty fail"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_HasProperty success:%{public}d", result); + } + // 若传入属性存在传入对象中,则输出true将结果转化为JSVM_Value类型抛出 + JSVM_Value returnReslut = nullptr; + OH_JSVM_GetBoolean(env, result, &returnReslut); + return returnReslut; +} +// HasProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = HasProperty}, +}; +static JSVM_CallbackStruct *method = param; +// HasProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"hasProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + hasProperty(obj, "data") + hasProperty(obj, 0) +)JS"; +// [End oh_jsvm_has_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "hasproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/types/libhasproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/types/libhasproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/types/libhasproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/types/libhasproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/types/libhasproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b0b7775d9c39f79c4f04c1f14ae7dff72ddea889 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/cpp/types/libhasproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libhasproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/ets/haspropertyability/HaspropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/ets/haspropertyability/HaspropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..3702f7f57bb46d045c55c67b17142d6ff02711cc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/ets/haspropertyability/HaspropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class HaspropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..e36318dd0e2c7a6a3b105533327694977ea072a4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libhasproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9333bc5583c9f92899e789c535988b4b1074f9db --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "hasproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "HaspropertyAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "HaspropertyAbility", + "srcEntry": "./ets/haspropertyability/HaspropertyAbility.ets", + "description": "$string:HaspropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:HaspropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1703bf5a8cc7fb6be1e2c51a4ad447509b613e72 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "HaspropertyAbility_desc", + "value": "description" + }, + { + "name": "HaspropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..89128f7bb361b82c1e2cd28e2564eb426b8d7cbf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testHasProperty', 0, async (done: Function) => { + console.log('uitest: TestHasProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'HaspropertyAbility' + } + 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('HaspropertyAbility'); + + 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: TestHasProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3f1c5ae200a64d5c3476454edca1dd5152413267 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hasproperty/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "hasproperty_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..9a9dbf391e5460b5dc511ce3840ff0c92d4da147 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/ohosTest.md @@ -0,0 +1,18 @@ +# JsvmAboutProperty 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 批量的向给定对象中定义属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 用给定的属性的名称,删除目标对象属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定对象的所有可用属性名称 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 用给定的属性的名称,检索目标对象的属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 用给定的属性的名称,检索目标对象的属性,此方法等效于调用Hello World | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定对象的所有可枚举属性名称 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 用给定的属性的名称,查询目标对象是否有此属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断给定Object中是否有名为key的own property | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 用给定的属性的名称,查询目标对象是否有此属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 为给定对象设置一个属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 用给定的属性的名称为目标对象设置属性 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/screenshots/JsvmAboutProperty_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/screenshots/JsvmAboutProperty_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/screenshots/JsvmAboutProperty_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/screenshots/JsvmAboutProperty_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/screenshots/JsvmAboutProperty_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/screenshots/JsvmAboutProperty_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4b22f0c7b5a85f022a38be857196ac51e75c48f3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "setnamedproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libsetnamedproperty.so": "file:./src/main/cpp/types/libsetnamedproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b1aaa08d4eede213300cd4470c68e7e3ea5ce559 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutProperty) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(setnamedproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(setnamedproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b9eef9575bf2632222008830ee194df07b40bbb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/hello.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_set_named_property] +// OH_JSVM_SetNamedProperty的样例方法 +const size_t STR_KEY_LEN = 32; +static JSVM_Value SetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取js侧传入的一个参数 + size_t argc = 1; + JSVM_Value str; + char strKey[STR_KEY_LEN] = ""; + OH_JSVM_GetCbInfo(env, info, &argc, &str, nullptr, nullptr); + // 获取传入参数字符串并存储在strKey中 + size_t keyLength; + OH_JSVM_GetValueStringUtf8(env, str, strKey, STR_KEY_LEN, &keyLength); + // 创建一个新对象 + JSVM_Value newObj; + OH_JSVM_CreateObject(env, &newObj); + // 设置整数值1234为属性值 + int32_t value = 1234; + JSVM_Value numValue; + OH_JSVM_CreateInt32(env, value, &numValue); + // 将整数值与指定属性名关联 + JSVM_Status status = OH_JSVM_SetNamedProperty(env, newObj, strKey, numValue); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetNamedProperty failed"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_SetNamedProperty success"); + } + // 返回新创建并设置命名属性的对象 + return newObj; +} +// SetNamedProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = SetNamedProperty}, +}; +static JSVM_CallbackStruct *method = param; +// SetNamedProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"setNamedProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + setNamedProperty("message") +)JS"; +// [End oh_jsvm_set_named_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "setnamedproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/types/libsetnamedproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/types/libsetnamedproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/types/libsetnamedproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/types/libsetnamedproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/types/libsetnamedproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4993f42700ac41b320d73ad2227f58da20519b00 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/cpp/types/libsetnamedproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libsetnamedproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..4f83d35959226804786d1e01f4c5445a4920df3d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libsetnamedproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/ets/setnamedpropertyability/SetnamedpropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/ets/setnamedpropertyability/SetnamedpropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..f3a650e5614339cc2feb4ee3992633aa501e79d3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/ets/setnamedpropertyability/SetnamedpropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class SetnamedpropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..be0003bbefc70a90679f1ba3a8f3c59b22e18630 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "setnamedproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "SetnamedpropertyAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "SetnamedpropertyAbility", + "srcEntry": "./ets/setnamedpropertyability/SetnamedpropertyAbility.ets", + "description": "$string:SetnamedpropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:SetnamedpropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..00edf98c8a41b1b0a9b400867f17f10c9024b735 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "SetnamedpropertyAbility_desc", + "value": "description" + }, + { + "name": "SetnamedpropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..01c28a2092ce8a071c5ffac0fc517405947027ba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testSetNamedProperty', 0, async (done: Function) => { + console.log('uitest: TestSetNamedProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'SetnamedpropertyAbility' + } + 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('SetnamedpropertyAbility'); + + 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: TestSetNamedProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e494eccaee7de5f224651acda68ce49527372a6a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setnamedproperty/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "setnamedproperty_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..84d963dd856ece2cce1752a294145b826402220b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "setproperty", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libsetproperty.so": "file:./src/main/cpp/types/libsetproperty" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4bf4840ac37514bfa696da9303aa44c2d682d4fc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutPropertyOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(setproperty SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(setproperty PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81f83071f82526e96e1536e8c9351a3267ad89e0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/hello.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_set_property] +// OH_JSVM_SetProperty的样例方法 +static JSVM_Value SetProperty(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接收js侧传入的三个参数:第一个参数为想要设置的object,第二个参数为属性,第三个参数为属性对应的值 + size_t argc = 3; + JSVM_Value args[3] = {nullptr}; + JSVM_Status status = OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetCbInfo fail"); + return nullptr; + } + // 通过调用OH_JSVM_SetProperty接口将属性与值设置入object如果失败,直接抛出错误 + constexpr size_t TARGET_OBJECT_INDEX = 0; + constexpr size_t PROPERTY_NAME_INDEX = 1; + constexpr size_t PROPERTY_VALUE_INDEX = 2; + status = OH_JSVM_SetProperty(env, args[TARGET_OBJECT_INDEX], args[PROPERTY_NAME_INDEX], args[PROPERTY_VALUE_INDEX]); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetProperty fail"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_SetProperty success"); + } + // 将设置成功后的object返回出去 + return args[TARGET_OBJECT_INDEX]; +} +// SetProperty注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = SetProperty}, +}; +static JSVM_CallbackStruct *method = param; +// SetProperty方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"setProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let obj = { data: 0, message: "hello world", 50: 1}; + setProperty(obj, "code", "hi") +)JS"; +// [End oh_jsvm_set_property] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "setproperty", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/types/libsetproperty/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/types/libsetproperty/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/types/libsetproperty/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/types/libsetproperty/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/types/libsetproperty/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..57e01ed73ad76ed4796ae378a07362a336ae56f4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/cpp/types/libsetproperty/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libsetproperty.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b5547fa7cacb6af5f879a888c7d37d3ca5554867 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libsetproperty.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/ets/setpropertyability/SetpropertyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/ets/setpropertyability/SetpropertyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..210466c53c3c78fdb12508871b43d1f7dc39c39e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/ets/setpropertyability/SetpropertyAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class SetpropertyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f3e301cdbecdc024b2085cf61540c8a290d28110 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "setproperty", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "SetpropertyAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "SetpropertyAbility", + "srcEntry": "./ets/setpropertyability/SetpropertyAbility.ets", + "description": "$string:SetpropertyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:SetpropertyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b44128d457e2683d0533e2c676ba18b099eb9dad --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "SetpropertyAbility_desc", + "value": "description" + }, + { + "name": "SetpropertyAbility_label", + "value": "JsvmAboutProperty" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..fe705bf04e86c7652a22a45fbf765eebb08bb96d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testSetProperty', 0, async (done: Function) => { + console.log('uitest: TestSetProperty begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'SetpropertyAbility' + } + 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('SetpropertyAbility'); + + 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: TestSetProperty end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..17ccc65377c137675412a5cf8d98f5874c90c31c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutProperty/setproperty/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "setproperty_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01ae5288a135b74b3719798f21df73c3a3a62aa0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmaboutstring", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1e583037fa45535edde72a032c9c0971761cfcfb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmAboutString" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f3944759025d5a5adaa836b3decf893a05085f95 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/README.md @@ -0,0 +1,168 @@ +# ArkTs使用JSVM-API接口创建和获取string值 + +### 介绍 + +使用JSVM-API接口获取和设置JavaScript对象的属性。通过合理使用这些函数,实现更复杂的功能和逻辑。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口创建和获取string值](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-string.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-----------------------------------------------------------------: | :-----------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createstringlatin1/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatestringlatin1 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── libcreatestringlatin1 + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createstringutf8/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatestringutf8 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createstringutf8ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createstringutf16/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatestringutf16 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createstringutf16ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluestringlatin1/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvaluestringlatin1 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvaluestringlatin1ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluestringutf8/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluestringutf16/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvaluestringutf16 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvaluestringutf16ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..55527286065291c19cea0e858b1f205ddd24f909 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/build-profile.json5 @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getvaluestringutf8", + "srcPath": "./getvaluestringutf8", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createstringutf8", + "srcPath": "./createstringutf8", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvaluestringutf16", + "srcPath": "./getvaluestringutf16", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createstringutf16", + "srcPath": "./createstringutf16", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvaluestringlatin1", + "srcPath": "./getvaluestringlatin1", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createstringlatin1", + "srcPath": "./createstringlatin1", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fb21c08f458e988814e8625d3c861ea50ef8d994 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createstringlatin1", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatestringlatin1.so": "file:./src/main/cpp/types/libcreatestringlatin1" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..981566719f45047d709b6c202414c81ad57d826f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutString) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createstringlatin1 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createstringlatin1 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acd0db361b4045feb9c8f085e012e4a4846b6b2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/hello.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_string_latin1] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +// [StartExclude oh_jsvm_create_string_latin1] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_string_latin1] + +// CreateStringLatin1注册回调 +// 定义字符串缓冲区的最大长度 +static const int MAX_BUFFER_SIZE = 128; +// OH_JSVM_CreateStringLatin1的样例方法 +static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) +{ + const char *str = "Hello, World! éçñ, successes to create Latin1 string!"; + size_t length = JSVM_AUTO_LENGTH; + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateStringLatin1(env, str, length, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateStringLatin1 fail"); + } else { + char buf[MAX_BUFFER_SIZE]; + 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); + } + return result; +} +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateStringLatin1}, +}; +static JSVM_CallbackStruct *method = param; +// CreateStringLatin1方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let script = createStringLatin1(); +)JS"; +// [End oh_jsvm_create_string_latin1] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createstringlatin1", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/types/libcreatestringlatin1/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/types/libcreatestringlatin1/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/types/libcreatestringlatin1/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/types/libcreatestringlatin1/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/types/libcreatestringlatin1/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8ae5b27af50d2208c9c74a8b8a7976b7c4b7ad33 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/cpp/types/libcreatestringlatin1/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatestringlatin1.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/ets/createstringlatin1ability/Createstringlatin1Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/ets/createstringlatin1ability/Createstringlatin1Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..7018945a85d72df08459bd82989d504cc43a9e01 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/ets/createstringlatin1ability/Createstringlatin1Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Createstringlatin1Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..5effaba83d83fdfb4cf569978a99fbc02691b876 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatestringlatin1.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..db113bbf9729cb65e8cbf422324202cffa262f66 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createstringlatin1", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createstringlatin1Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createstringlatin1Ability", + "srcEntry": "./ets/createstringlatin1ability/Createstringlatin1Ability.ets", + "description": "$string:Createstringlatin1Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createstringlatin1Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..9e79474145c64f53f5c247cc03163c2b44cd46d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createstringlatin1Ability_desc", + "value": "description" + }, + { + "name": "Createstringlatin1Ability_label", + "value": "JsvmAboutString" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0d89fd15b43061200a343ed9c64997e8322234a7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateStringLatin1', 0, async (done: Function) => { + console.log('uitest: TestCreateStringLatin1 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createstringlatin1Ability' + } + 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('Createstringlatin1Ability'); + + 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: TestCreateStringLatin1 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c4bb7f225410839e35893937a2c1ececc74c4995 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringlatin1/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createstringlatin1_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4db4fec0aab02675ecf35b8d654b67be87ba31b8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createstringutf16", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatestringutf16.so": "file:./src/main/cpp/types/libcreatestringutf16" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c27fcc77efa77536aace2220be59aebf35954ca8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutString) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createstringutf16 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createstringutf16 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..119ab953c9818ca89effa4880b7237964f7af8de --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/hello.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_string_utf16] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +#include +#include +// [StartExclude oh_jsvm_create_string_utf16] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_string_utf16] + +// OH_JSVM_CreateStringUtf16的样例方法 +static JSVM_Value CreateStringUtf16(JSVM_Env env, JSVM_CallbackInfo info) +{ + const char16_t *str = u"你好, World!, successes to create UTF-16 string!"; + std::u16string ustr(str); + size_t length = ustr.length(); + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateStringUtf16(env, str, length, &result); + std::u16string u16str = {str}; + // 将 std::u16string 转换为 std::string + std::wstring_convert, char16_t> converter; + std::string strResult = converter.to_bytes(u16str); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateStringUtf16 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf16 success: %{public}s", strResult.c_str()); + } + return result; +} +// CreateStringUtf16注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateStringUtf16}, +}; +static JSVM_CallbackStruct *method = param; +// CreateStringUtf16方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let script = createStringUtf16(); +)JS"; +// [End oh_jsvm_create_string_utf16] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createstringutf16", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/types/libcreatestringutf16/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/types/libcreatestringutf16/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/types/libcreatestringutf16/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/types/libcreatestringutf16/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/types/libcreatestringutf16/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e78b723bbb6313156a33b24d7000f36d15fdb260 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/cpp/types/libcreatestringutf16/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatestringutf16.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/ets/createstringutf16ability/Createstringutf16Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/ets/createstringutf16ability/Createstringutf16Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..f55281a97387e1ee3a2bc7e1c56730293a26f2d3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/ets/createstringutf16ability/Createstringutf16Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Createstringutf16Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a5d7bf8bf264dd3bce69af2a1d6c740268853158 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatestringutf16.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..67d9698b622e7bc816251538753e5372bbe3b635 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createstringutf16", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createstringutf16Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createstringutf16Ability", + "srcEntry": "./ets/createstringutf16ability/Createstringutf16Ability.ets", + "description": "$string:Createstringutf16Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createstringutf16Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1890388e764eea5aa8c34ce3983f26b4b8cbbd2c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createstringutf16Ability_desc", + "value": "description" + }, + { + "name": "Createstringutf16Ability_label", + "value": "JsvmAboutString" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..92b42b3c86b5d6ef7f3386294f60e4ac88d0eed4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateStringUtf16', 0, async (done: Function) => { + console.log('uitest: TestCreateStringUtf16 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createstringutf16Ability' + } + 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('Createstringutf16Ability'); + + 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: TestCreateStringUtf16 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c67774b7b32d66034e3417b4bdc95989c1157368 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf16/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createstringutf16_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ed9c77f4d3a3362220ce9ca0d95728e5df753bfa --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createstringutf8", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatestringutf8.so": "file:./src/main/cpp/types/libcreatestringutf8" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2a5952976ca2d84232ecf1b8045fe89445d51d86 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutString) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createstringutf8 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createstringutf8 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f3abfc46f946be3546b20451c35089ac4c70a33 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/hello.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_string_utf8] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +// [StartExclude oh_jsvm_create_string_utf8] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_string_utf8] + +// OH_JSVM_CreateStringUtf8的样例方法 +static JSVM_Value CreateStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) +{ + const char *str = u8"你好, World!, successes to create UTF-8 string!"; + size_t length = strlen(str); + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, length, &result); + if (status != JSVM_OK) { + OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-8 string"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM CreateStringUtf8 success: %{public}s", str); + } + return result; +} +// CreateStringUtf8注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateStringUtf8}, +}; +static JSVM_CallbackStruct *method = param; +// CreateStringUtf8方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let script = createStringUtf8(); +)JS"; +// [End oh_jsvm_create_string_utf8] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createstringutf8", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/types/libcreatestringutf8/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/types/libcreatestringutf8/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/types/libcreatestringutf8/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/types/libcreatestringutf8/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/types/libcreatestringutf8/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01e8bf5dfcc9610a96f2e40607502986bf75525e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/cpp/types/libcreatestringutf8/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatestringutf8.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/ets/createstringutf8ability/Createstringutf8Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/ets/createstringutf8ability/Createstringutf8Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..33326532fe7b23263d54a426a146390791219893 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/ets/createstringutf8ability/Createstringutf8Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Createstringutf8Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7ef1c87772d60ba3701c0b04cc8178001300aa73 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatestringutf8.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..beb50ecab63be08f424eb5bfc5dca9610487562a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createstringutf8", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createstringutf8Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createstringutf8Ability", + "srcEntry": "./ets/createstringutf8ability/Createstringutf8Ability.ets", + "description": "$string:Createstringutf8Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createstringutf8Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e36fc0a2bc016d63a6b88abb0503724afd7ca95c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createstringutf8Ability_desc", + "value": "description" + }, + { + "name": "Createstringutf8Ability_label", + "value": "JsvmAboutString" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bcf5447dcc2fe275550265fdae2592835feda6f0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateStringUtf8', 0, async (done: Function) => { + console.log('uitest: TestCreateStringUtf8 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createstringutf8Ability' + } + 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('Createstringutf8Ability'); + + 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: TestCreateStringUtf8 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0413d85c14f339db0956a8ca6228740be5c2bd65 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/createstringutf8/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createstringutf8_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..660b85226f4b6983e503e924cf5003bcfa705846 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluestringlatin1", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluestringlatin1.so": "file:./src/main/cpp/types/libgetvaluestringlatin1" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c2cd6e19ea3de6fe398d6822a34e8b515954541d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutString) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvaluestringlatin1 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluestringlatin1 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f4217397b5392b4e53683bb3725d8c99249cd18 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/hello.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_string_latin1] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +// [StartExclude oh_jsvm_get_value_string_latin1] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_string_latin1] + +// OH_JSVM_GetValueStringLatin1的样例方法 +// 定义字符串缓冲区的最大长度 +static const int MAX_BUFFER_SIZE = 128; +static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + char buf[MAX_BUFFER_SIZE]; + 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) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringLatin1 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueStringLatin1 success: %{public}s", buf); + } + OH_JSVM_CreateStringLatin1(env, buf, length, &jsvmRes); + return jsvmRes; +} +// GetValueStringLatin1注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueStringLatin1}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueStringLatin1方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueStringLatin1", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let data = "中文"; + let script = getValueStringLatin1(data); +)JS"; +// [End oh_jsvm_get_value_string_latin1] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluestringlatin1", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/types/libgetvaluestringlatin1/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/types/libgetvaluestringlatin1/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/types/libgetvaluestringlatin1/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/types/libgetvaluestringlatin1/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/types/libgetvaluestringlatin1/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..501d4bb9f1405c45889a74c63c3b736bd4a6d2df --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/cpp/types/libgetvaluestringlatin1/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluestringlatin1.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/ets/getvaluestringlatin1ability/Getvaluestringlatin1Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/ets/getvaluestringlatin1ability/Getvaluestringlatin1Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..3f4cf52e7f6898c3f733e59480dc3d9920b119aa --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/ets/getvaluestringlatin1ability/Getvaluestringlatin1Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Getvaluestringlatin1Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..99242bf94b108429ed2ed351880f57fc77ede1ee --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluestringlatin1.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ef501871330ec46126b6ee1881643e79b3c5ed46 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluestringlatin1", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Getvaluestringlatin1Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvaluestringlatin1Ability", + "srcEntry": "./ets/getvaluestringlatin1ability/Getvaluestringlatin1Ability.ets", + "description": "$string:Getvaluestringlatin1Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvaluestringlatin1Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c6341d8d9ae01f759ae57a186e2b2d850856664f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvaluestringlatin1Ability_desc", + "value": "description" + }, + { + "name": "Getvaluestringlatin1Ability_label", + "value": "JsvmAboutString" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c4a92abdc0caac216b7559042908c7b49431c337 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueStringLatin1', 0, async (done: Function) => { + console.log('uitest: TestGetValueStringLatin1 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvaluestringlatin1Ability' + } + 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('Getvaluestringlatin1Ability'); + + 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: TestGetValueStringLatin1 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f7c28910206f9f5ca17aa8be56137f1b1ed886ee --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringlatin1/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluestringlatin1_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8c3af9b1484431bbdc64dce31787ea9ada4f9e8b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluestringutf16", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluestringutf16.so": "file:./src/main/cpp/types/libgetvaluestringutf16" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0617721e65d0cd0005d0f09e5f2fe706672a242c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutString) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvaluestringutf16 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluestringutf16 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00f7732f0b88cb925105a1931a3fb7c75637eec9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/hello.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_string_utf16] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +#include +#include +// [StartExclude oh_jsvm_get_value_string_utf16] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_string_utf16] + +// OH_JSVM_GetValueStringUtf16的样例方法 +// 定义字符串缓冲区的最大长度 +static const int MAX_BUFFER_SIZE = 128; +static JSVM_Value GetValueStringUtf16(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + JSVM_Value result = nullptr; + size_t length = 0; + char16_t buffer[MAX_BUFFER_SIZE] = {0}; + // 字符串的缓冲区大小 + size_t bufferSize = MAX_BUFFER_SIZE; + JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], buffer, bufferSize, &length); + // 将 char16_t 转换为 std::u16string + std::u16string u16str = {buffer}; + // 将 std::u16string 转换为 std::string + std::wstring_convert, char16_t> converter; + std::string str = converter.to_bytes(u16str); + // 获取字符串返回结果 + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf16 fail"); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf16 success: %{public}s", str.c_str()); + } + return result; +} +// GetValueStringUtf16注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueStringUtf16}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueStringUtf16方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueStringUtf16", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let data = "ahello。"; + let script = getValueStringUtf16(data); +)JS"; +// [End oh_jsvm_get_value_string_utf16] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluestringutf16", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/types/libgetvaluestringutf16/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/types/libgetvaluestringutf16/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/types/libgetvaluestringutf16/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/types/libgetvaluestringutf16/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/types/libgetvaluestringutf16/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..31443c0527e0d2a072c3f20b946700673fec4974 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/cpp/types/libgetvaluestringutf16/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluestringutf16.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/ets/getvaluestringutf16ability/Getvaluestringutf16Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/ets/getvaluestringutf16ability/Getvaluestringutf16Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..441c2bd280e3dd72a1597d43cdeda93743499195 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/ets/getvaluestringutf16ability/Getvaluestringutf16Ability.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class Getvaluestringutf16Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1fbf0916d6ae2cede656dc8013a0ec4da7bac2f3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluestringutf16.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..80dce385f33a045183300e74ee2b2de24d8a9d9d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluestringutf16", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Getvaluestringutf16Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvaluestringutf16Ability", + "srcEntry": "./ets/getvaluestringutf16ability/Getvaluestringutf16Ability.ets", + "description": "$string:Getvaluestringutf16Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvaluestringutf16Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a4979b2c4b9b52dfb07d610e4c63f16d18082d89 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvaluestringutf16Ability_desc", + "value": "description" + }, + { + "name": "Getvaluestringutf16Ability_label", + "value": "JsvmAboutString" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..133fc2bb86c8068d0b707b717e64c6b802dd1894 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueStringUtf16', 0, async (done: Function) => { + console.log('uitest: TestGetValueStringUtf16 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvaluestringutf16Ability' + } + 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('Getvaluestringutf16Ability'); + + 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: TestGetValueStringUtf16 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ef448862ddbd26d9711bb2cdee9a969526110340 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf16/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluestringutf16_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f2c7b354dc2d05e3efb908c814f797f5b9be3151 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluestringutf8", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluestringutf8.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..7250a3ad6acea238eff0281d47e43c5b134a0193 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmAboutString) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvaluestringutf8 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluestringutf8 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f10a76c55db117829386a14bd4ae74da42cb3ab --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/hello.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_string_utf8] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +#include +// [StartExclude oh_jsvm_get_value_string_utf8] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" +#define MAX_MALLOC_SIZE 0x800000 + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_string_utf8] + +// OH_JSVM_GetValueStringUtf8的样例方法 +static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + size_t length = 0; + 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 nullptr; + } + char *buf = (char *)malloc(length + 1); + status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueStringUtf8 fail"); + free(buf); + return nullptr; + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueStringUtf8 success: %{public}s", buf); + } + JSVM_Value result = nullptr; + OH_JSVM_CreateStringUtf8(env, buf, length, &result); + free(buf); + return result; +} +// GetValueStringUtf8注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueStringUtf8}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueStringUtf8方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueStringUtf8", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS( + let data = "aaBC+-$%^你好123"; + let script = getValueStringUtf8(data); +)JS"; +// [End oh_jsvm_get_value_string_utf8] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluestringutf8", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a4238e7158575c4ad8e0e6148cd6f125b76edc79 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluestringutf8.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..090a5c31a8a7e2a94aa0453ab82428e53c22b91d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluestringutf8.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a57554c5fa0d8519ce1db20a1583e6be7d88e672 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluestringutf8", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3c9edea49abd4287a9bc26a3a1901d94d881ddce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmAboutString" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c627e81499d50cbe603bc171b07060ff2bacd183 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueStringUtf8', 0, async (done: Function) => { + console.log('uitest: TestGetValueStringUtf8 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: TestGetValueStringUtf8 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a0a873074ee86f43327e175ab32f59b783b7e7c3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/getvaluestringutf8/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluestringutf8_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..079f6083c788e180c320162ce8b303a18d9019fe --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/ohosTest.md @@ -0,0 +1,13 @@ +# JsvmAboutString 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 获取给定JavaScript string对象的Utf8编码字符串 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据Utf8编码的字符串创建一个JavaScript string对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript string对象的Utf16编码字符串 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据Utf16编码的字符串数据创建JavaScript string对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript string对象的Latin1编码字符串 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据Latin-1编码的字符串创建一个JavaScript string对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/screenshots/JsvmAboutString_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/screenshots/JsvmAboutString_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/screenshots/JsvmAboutString_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/screenshots/JsvmAboutString_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/screenshots/JsvmAboutString_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmAboutString/screenshots/JsvmAboutString_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fdbab31b67ea5eac241a399dec4c09424f4a581c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmbasicdatatypes", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0717d1aaf402b6f50787907b8bfc57100ba0bfb7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmBasicDataTypes" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e590d76ac6afc4eafbb43e37df813a582968ee77 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/README.md @@ -0,0 +1,206 @@ +# ArkTS使用JSVM-API接口创建基本数据类型 + +### 介绍 + +在JavaScript中,整数类型是一种基本数据类型,用于表示没有小数部分的数值。double类型用于表示有小数的数值。JavaScript的数值存储方式会导致在某些情况下无法准确表示非常大或非常小的数值,在这种情况下,需要使用BigInt对应的JSVM-API接口来处理更大范围的整数。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口创建基本数据类型](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-basic-data-types.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :--------------------------------------------------------------------: | :--------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World ,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createdouble/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatedouble + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createdoubleability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createint32/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreateint32 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createint32ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createint64/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreateint64 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createint64ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createuint32/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreateuint32 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createuint32ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvaluedouble/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvaluedouble + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvaluedoubleability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvalueint32/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvalueint32 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvalueint32ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvalueint64/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvalueint64 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvalueint64ability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvalueuint32/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetvalueuint32 + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getvalueuint32ability + │ │ ├── getvalueuint32backupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e9b2efb5186d365624e13fe4b75bc341fe061718 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/build-profile.json5 @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getvalueuint32", + "srcPath": "./getvalueuint32", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvalueint32", + "srcPath": "./getvalueint32", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvalueint64", + "srcPath": "./getvalueint64", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getvaluedouble", + "srcPath": "./getvaluedouble", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createint32", + "srcPath": "./createint32", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createuint32", + "srcPath": "./createuint32", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createint64", + "srcPath": "./createint64", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createdouble", + "srcPath": "./createdouble", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ed05653cca31b61d64cf6471529eaf50d4f70709 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/code-linter.json5 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c6d0dac73b2a62826e12610d92b647b3af64b6a7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createdouble", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatedouble.so": "file:./src/main/cpp/types/libcreatedouble" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..364ccbf29e58974306af53d82c93a518afca2554 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createdouble SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createdouble PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a822e9807e28495b22e142249168d025e75747d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/hello.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_double] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_double] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_double] + +// OH_JSVM_CreateDouble的样例方法 +static JSVM_Value CreateDouble(JSVM_Env env, JSVM_CallbackInfo info) +{ + double value = 1.234; + // 创建JavaScript中的double数字 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateDouble(env, value, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateDouble fail"); + } else { + double number = 0; + OH_JSVM_GetValueDouble(env, result, &number); + OH_LOG_INFO(LOG_APP, "JSVM CreateDouble success: %{public}f", number); + } + return result; +} + +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateDouble}, +}; +static JSVM_CallbackStruct *method = param; +// CreateDouble方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createDouble", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createDouble())JS"; +// [End oh_jsvm_create_double] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createdouble", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/types/libcreatedouble/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/types/libcreatedouble/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/types/libcreatedouble/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/types/libcreatedouble/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/types/libcreatedouble/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..07112ef05dd7115781e3bba7de9001cb53e52cfc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/cpp/types/libcreatedouble/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatedouble.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/ets/createdoubleability/CreatedoubleAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/ets/createdoubleability/CreatedoubleAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6a9c2cbcc4cc4abe34d4194ab38317c5f29b1de0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/ets/createdoubleability/CreatedoubleAbility.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class CreatedoubleAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a8e8874ef07d6371ebfd14497a3a52bf99ca8043 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatedouble.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9b58e4ac831192d483a8456ddb604f345c475d4f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createdouble", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatedoubleAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatedoubleAbility", + "srcEntry": "./ets/createdoubleability/CreatedoubleAbility.ets", + "description": "$string:CreatedoubleAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatedoubleAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e1275bfb9201a3f0ac8c48d4b8277cbe60ccdd75 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatedoubleAbility_desc", + "value": "description" + }, + { + "name": "CreatedoubleAbility_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..884353874ac6627a5298b9918c47f9d8368ecd2a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateDouble', 0, async (done: Function) => { + console.log('uitest: TestCreateDouble begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatedoubleAbility' + } + 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('CreatedoubleAbility'); + + 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: TestCreateDouble end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7b241d74d31922efec9234c67c23673e675287ae --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createdouble/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createdouble_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4507a2ecd45fde3a0fb3ec8a910524f5f7853d1f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createint32", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreateint32.so": "file:./src/main/cpp/types/libcreateint32" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f9bdf9271d3d70ec7ade48c0fb1ac1992f026893 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createint32 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createint32 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f671f5eadcd9165dad3fb6fa908267a12d04d81 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/hello.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_int32] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_int32] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_int32] + +// OH_JSVM_CreateInt32的样例方法 +static JSVM_Value CreateInt32(JSVM_Env env, JSVM_CallbackInfo info) +{ + int32_t value = -20; + // 创建JavaScript中的int32数字 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateInt32(env, value, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateInt32 fail"); + } else { + int32_t number = 0; + OH_JSVM_GetValueInt32(env, result, &number); + OH_LOG_INFO(LOG_APP, "JSVM CreateInt32 success: %{public}d", number); + } + return result; +} + +// CreateInt32注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateInt32}, +}; +static JSVM_CallbackStruct *method = param; +// CreateInt32方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createInt32", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createInt32())JS"; +// [End oh_jsvm_create_int32] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createint32", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/types/libcreateint32/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/types/libcreateint32/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/types/libcreateint32/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/types/libcreateint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/types/libcreateint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..37d87a3282436a82a46f2ab0f2d2bfe80f0d0715 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/cpp/types/libcreateint32/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreateint32.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/ets/createint32ability/Createint32Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/ets/createint32ability/Createint32Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..d76b52b1c97eb594fff49e5f27d83a9ede2f417b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/ets/createint32ability/Createint32Ability.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Createint32Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..6bb339635e7766bd3c95c284c80369be898ac5a3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreateint32.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a684705ce0a7a4e79f0590bec9f0d04149629a37 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createint32", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createint32Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createint32Ability", + "srcEntry": "./ets/createint32ability/Createint32Ability.ets", + "description": "$string:Createint32Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createint32Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..19b814ed2a2c9fce357062e488f034f1085ffe0c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createint32Ability_desc", + "value": "description" + }, + { + "name": "Createint32Ability_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f4230efd09ffd5c4eaf34741d3f27cb380aceaaa --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateInt32', 0, async (done: Function) => { + console.log('uitest: TestCreateInt32 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createint32Ability' + } + 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('Createint32Ability'); + + 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: TestCreateInt32 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..67190c9e5c7b327097bde18d61ec9de5a5e9ed74 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint32/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createint32_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0ca706152ac79490f7def1fe934fd25cc9d5ae58 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createint64", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreateint64.so": "file:./src/main/cpp/types/libcreateint64" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0c59239f03840727822e3a236549d7babbccd127 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createint64 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createint64 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..241ad92c3e4ce81adc860b4baa589269e42147a4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/hello.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_int64] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_int64] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_int64] + +// OH_JSVM_CreateInt64的样例方法 +static JSVM_Value CreateInt64(JSVM_Env env, JSVM_CallbackInfo info) +{ + // int64是有符号的64位整数类型,可以表示范围从-2^63到2^63 - 1的整数,即 -9223372036854775808到9223372036854775807 + // 要表示的整数值 + int64_t value = 2147483648; + // 创建JavaScript中的int64数字 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateInt64(env, value, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateInt64 fail"); + } else { + int64_t number = 0; + OH_JSVM_GetValueInt64(env, result, &number); + OH_LOG_INFO(LOG_APP, "JSVM CreateInt64 success: %{public}ld", number); + } + return result; +} + +// CreateInt64注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateInt64}, +}; +static JSVM_CallbackStruct *method = param; +// CreateInt64方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createInt64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createInt64())JS"; +// [End oh_jsvm_create_int64] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createint64", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/types/libcreateint64/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/types/libcreateint64/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/types/libcreateint64/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/types/libcreateint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/types/libcreateint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..82ec4846629896cb53c22b7ca7b9c435de78142c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/cpp/types/libcreateint64/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreateint64.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/ets/createint64ability/Createint64Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/ets/createint64ability/Createint64Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..f65ac599b5d30a72196155a8e80661e48e77e937 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/ets/createint64ability/Createint64Ability.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Createint64Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..d84dc3c2e9c1cee52c9f15fa2d0f50b3ebaa2972 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreateint64.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..aff59ba55039252522fa86a68a3b3a7526a0b4bc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createint64", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createint64Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createint64Ability", + "srcEntry": "./ets/createint64ability/Createint64Ability.ets", + "description": "$string:Createint64Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createint64Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1e3a4cefa2ec46ad5037ca39adf96562562a7491 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createint64Ability_desc", + "value": "description" + }, + { + "name": "Createint64Ability_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c266475a275e462bb464a28bcc87fa3e3ec884df --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateInt64', 0, async (done: Function) => { + console.log('uitest: TestCreateInt64 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createint64Ability' + } + 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('Createint64Ability'); + + 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: TestCreateInt64 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..61457fd3f6d6399b81bba1fcda3508a6c52d8b8e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createint64/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createint64_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..daa6d3f06a74c5cbc88c0109112807c53158214f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createuint32", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreateuint32.so": "file:./src/main/cpp/types/libcreateuint32" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..184deeb980903f436030be54d42ec21c2bdce0bb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(createuint32 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createuint32 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09e64074d6da80fdcb18925799008f1d10d20c44 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/hello.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_uint32] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_create_uint32] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_create_uint32] + +// OH_JSVM_CreateUint32的样例方法 +static JSVM_Value CreateUInt32(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 如果使用 + // uint32_t类型来定义-26,会发生溢出,溢出时会对结果进行模运算,将负数的二进制补码转换为相应的正数。-26输出4294967270 + // uint32_t是无符号的32位整数类型,只能表示非负整数。它的范围是从0到2 ^32 - 1,即0到4294967295 + // 要表示的整数值 + uint32_t value = 26; + // 创建JavaScript中的uint32数字 + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateUint32(env, value, &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM CreateUInt32 fail"); + } else { + uint32_t number = 0; + OH_JSVM_GetValueUint32(env, result, &number); + OH_LOG_INFO(LOG_APP, "JSVM CreateUInt32 success: %{public}u", number); + } + return result; +} + +// CreateUInt32注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = CreateUInt32}, +}; +static JSVM_CallbackStruct *method = param; +// CreateUInt32方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"createUInt32", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(createUInt32())JS"; +// [End oh_jsvm_create_uint32] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createuint32", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/types/libcreateuint32/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/types/libcreateuint32/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/types/libcreateuint32/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/types/libcreateuint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/types/libcreateuint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6203cb2eab75e7764b965f4ea5f52a8c683efd7d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/cpp/types/libcreateuint32/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreateuint32.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/ets/createuint32ability/Createuint32Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/ets/createuint32ability/Createuint32Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..32d30a9079fd3ef06bc98e6bdb2e2a0047da5627 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/ets/createuint32ability/Createuint32Ability.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Createuint32Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..dbbbbfde9af6a78aadf4f3f10589137df69d236f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreateuint32.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6136e1848e85e6d5d153fd24402be8ddfd4374c2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createuint32", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Createuint32Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Createuint32Ability", + "srcEntry": "./ets/createuint32ability/Createuint32Ability.ets", + "description": "$string:Createuint32Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Createuint32Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..45dd30e382140fb937578b3fc1cad3547d41af3b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Createuint32Ability_desc", + "value": "description" + }, + { + "name": "Createuint32Ability_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..cb8fb3e4fec28977f35b09afa4139888656a426b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateUint32', 0, async (done: Function) => { + console.log('uitest: TestCreateUint32 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Createuint32Ability' + } + 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('Createuint32Ability'); + + 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: TestCreateUint32 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b1a963b6af744813767c2535e6c1a384bd15d212 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/createuint32/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createuint32_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4adebc3b57c689f6643fc8ea1e0986480b11766b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvaluedouble", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvaluedouble.so": "file:./src/main/cpp/types/libgetvaluedouble" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6e49d0fbcde3c2210c362bb6c02c23f3f60b8653 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getvaluedouble SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvaluedouble PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb5c0b063467cd0a4b5692910f1e18b679e73562 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/hello.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_double] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_value_double] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_double] + +// OH_JSVM_GetValueDouble的样例方法 +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 = 0; + JSVM_Status status = OH_JSVM_GetValueDouble(env, args[0], &value); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetDouble fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetDouble success: %{public}f", value); + } + return args[0]; +} + +// GetDouble注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetDouble}, +}; +static JSVM_CallbackStruct *method = param; +// GetDouble方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getDouble", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getDouble(-110.0456))JS"; +// [EndExclude oh_jsvm_get_value_double] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvaluedouble", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/types/libgetvaluedouble/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/types/libgetvaluedouble/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/types/libgetvaluedouble/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/types/libgetvaluedouble/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/types/libgetvaluedouble/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1cba47eb3223d20ca1c537fd9949af61014b7bbd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/cpp/types/libgetvaluedouble/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvaluedouble.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/ets/getvaluedoubleability/GetvaluedoubleAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/ets/getvaluedoubleability/GetvaluedoubleAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..cfbdba57fe2fa0f9fc10f44d960ac879dcb87da8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/ets/getvaluedoubleability/GetvaluedoubleAbility.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class GetvaluedoubleAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..6ef860f004f2f85283fbb4f12bb68705198a490f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvaluedouble.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c62508cb0e63c61baa795c9fc6058a673e912904 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluedouble", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetvaluedoubleAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetvaluedoubleAbility", + "srcEntry": "./ets/getvaluedoubleability/GetvaluedoubleAbility.ets", + "description": "$string:GetvaluedoubleAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetvaluedoubleAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0ee636b385f3c97b03cda7171db946b523272328 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetvaluedoubleAbility_desc", + "value": "description" + }, + { + "name": "GetvaluedoubleAbility_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a59ff4a6e199d56b5dcf088c057329eb6afbea08 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueDouble', 0, async (done: Function) => { + console.log('uitest: TestGetValueDouble begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetvaluedoubleAbility' + } + 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('GetvaluedoubleAbility'); + + 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: TestGetValueDouble end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..081f77bc27e2b1ca05c2352603d034d2c60de620 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvaluedouble/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvaluedouble_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..559beb0fe7cb1fa24f48b7f35899deb36206e246 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvalueint32", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvalueint32.so": "file:./src/main/cpp/types/libgetvalueint32" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6eebe42c373a57fd3aad8a7af7e565cb5ec4ecf4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getvalueint32 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvalueint32 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cf74aea089e32d9c58b1ea07d51e8412defcf1f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/hello.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_int32] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_value_int32] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_int32] + +// OH_JSVM_GetValueInt32的样例方法 +static JSVM_Value GetValueInt32(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + int32_t result32 = 0; + // 解析传递的参数 + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 将前端传过来的参数转为JSVM模块的int32类型 + JSVM_Status status = OH_JSVM_GetValueInt32(env, args[0], &result32); + if (status != JSVM_OK) { + return nullptr; + } + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueInt32 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueInt32 success: %{public}d", result32); + } + return args[0]; +} + +// GetValueInt32注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueInt32}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueInt32方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueInt32", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getValueInt32(-123))JS"; +// [EndExclude oh_jsvm_get_value_int32] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvalueint32", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/types/libgetvalueint32/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/types/libgetvalueint32/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/types/libgetvalueint32/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/types/libgetvalueint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/types/libgetvalueint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b16c22eb9756fefb2b156b5e5f1e7b00cc05062a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/cpp/types/libgetvalueint32/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvalueint32.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/ets/getvalueint32ability/Getvalueint32Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/ets/getvalueint32ability/Getvalueint32Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..95250ef0343610c196924a1336d69e68dee83c19 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/ets/getvalueint32ability/Getvalueint32Ability.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Getvalueint32Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7805ff25158d3b67d5e80f29e6032964e3e543cb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvalueint32.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..102c5ee189282b9bb05d7c4c317322698d36c82e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueint32", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Getvalueint32Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvalueint32Ability", + "srcEntry": "./ets/getvalueint32ability/Getvalueint32Ability.ets", + "description": "$string:Getvalueint32Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvalueint32Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..74660f7434fd9e7104a158f5bab0e81d18305402 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvalueint32Ability_desc", + "value": "description" + }, + { + "name": "Getvalueint32Ability_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..51cfa9704003d9c776e75a607368880c2c5589a9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueInt32', 0, async (done: Function) => { + console.log('uitest: TestGetValueInt32 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvalueint32Ability' + } + 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('Getvalueint32Ability'); + + 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: TestGetValueInt32 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3cf8a9cf57aa9a0af8882e3233aee6d0af726ca4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint32/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueint32_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3a300d3190d7d9d2b6fd5d4d8e546a1564ecae94 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvalueint64", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvalueint64.so": "file:./src/main/cpp/types/libgetvalueint64" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..de7a3ca78888729b7dea34d83e79fe585b51e57b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getvalueint64 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvalueint64 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b2f7c5d3d5c71da6de091970dae53019f15dd33 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/hello.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_int64] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_value_int64] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_int64] + +// OH_JSVM_GetValueInt64的样例方法 +static JSVM_Value GetValueInt64(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + int64_t result64 = 0; + // 解析传递的值 + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 将前端传过来的参数分别转为JSVM模块的int64类型 + JSVM_Status status = OH_JSVM_GetValueInt64(env, args[0], &result64); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueInt64 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueInt64 success: %{public}d", result64); + } + return args[0]; +} + +// GetValueInt64注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueInt64}, +}; +static JSVM_CallbackStruct *method = param; +// GetValueInt64方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueInt64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getValueInt64(-123))JS"; +// [End oh_jsvm_get_value_int64] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvalueint64", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/types/libgetvalueint64/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/types/libgetvalueint64/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/types/libgetvalueint64/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/types/libgetvalueint64/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/types/libgetvalueint64/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..675da76204fb0e03fc979d91ac3fe4fbb740bbf6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/cpp/types/libgetvalueint64/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvalueint64.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/ets/getvalueint64ability/Getvalueint64Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/ets/getvalueint64ability/Getvalueint64Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..6f8befe4242603704a6b8be88a2dba3504e133a3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/ets/getvalueint64ability/Getvalueint64Ability.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Getvalueint64Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..eb9f302146f51edc82e1d2f2832b08a58b970544 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvalueint64.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2ca98d368aacc292e265cdb2eaa83978ac59c19b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueint64", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "Getvalueint64Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvalueint64Ability", + "srcEntry": "./ets/getvalueint64ability/Getvalueint64Ability.ets", + "description": "$string:Getvalueint64Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvalueint64Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..7c45a2953a835a86ed9b457ee207d8b08e286956 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvalueint64Ability_desc", + "value": "description" + }, + { + "name": "Getvalueint64Ability_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0fca3488df0bab0630d436de603ac72bd36d2dae --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueInt64', 0, async (done: Function) => { + console.log('uitest: TestGetValueInt64 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvalueint64Ability' + } + 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('Getvalueint64Ability'); + + 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: TestGetValueInt64 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2aedd60cbf574fa077adb9917acb8e094bf6afe2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueint64/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueint64_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a988e9ec7e3e67a8c756f26f07b95be60470302b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvalueuint32", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvalueuint32.so": "file:./src/main/cpp/types/libgetvalueuint32" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..67c7b089564d9e99494bb614245fd5f2b7a4db2a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(GetValueUint32) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(getvalueuint32 SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvalueuint32 PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb130e8f4a4e9585dfa845314076d8b3855cc11f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/hello.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_value_uint32] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" + +// [StartExclude oh_jsvm_get_value_uint32] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_value_uint32] + +// OH_JSVM_GetValueUint32的样例方法 +static JSVM_Value GetValueUint32(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取传入的数字类型参数 + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + // 解析传入的参数 + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + uint32_t number = 0; + // 获取传入参数的值中的无符号32位整数 + JSVM_Status status = OH_JSVM_GetValueUint32(env, argv[0], &number); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM GetValueUint32 fail"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM GetValueUint32 success: %{public}u", number); + } + return argv[0]; +} + +// GetValueUint32注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetValueUint32}, +}; +static JSVM_CallbackStruct *method = param; + +// GetValueUint32方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getValueUint32", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(getValueUint32(123))JS"; +// [End oh_jsvm_get_value_uint32] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvalueuint32", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/types/libgetvalueuint32/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/types/libgetvalueuint32/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/types/libgetvalueuint32/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/types/libgetvalueuint32/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/types/libgetvalueuint32/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1892cf3f4c75381cc20ae814a589ef7958b4696b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/cpp/types/libgetvalueuint32/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvalueuint32.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/getvalueuint32ability/Getvalueuint32Ability.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/getvalueuint32ability/Getvalueuint32Ability.ets new file mode 100644 index 0000000000000000000000000000000000000000..d7c77ef51df1e1eec37ab78f58dabf95aa7035bc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/getvalueuint32ability/Getvalueuint32Ability.ets @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class Getvalueuint32Ability extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/getvalueuint32backupability/Getvalueuint32BackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/getvalueuint32backupability/Getvalueuint32BackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..3e2a11e2beee9f0ab3dd3d7adfb8f34545d76736 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/getvalueuint32backupability/Getvalueuint32BackupAbility.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class Getvalueuint32BackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7407f72a435ed8fef4ab0a87fb71cc8e8a1d0201 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvalueuint32.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ab01538bd622d8efe23a796b9cbdd9620cedc576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueuint32", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "Getvalueuint32Ability", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "Getvalueuint32Ability", + "srcEntry": "./ets/getvalueuint32ability/Getvalueuint32Ability.ets", + "description": "$string:Getvalueuint32Ability_desc", + "icon": "$media:layered_image", + "label": "$string:Getvalueuint32Ability_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "Getvalueuint32BackupAbility", + "srcEntry": "./ets/getvalueuint32backupability/Getvalueuint32BackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..010d3b35e85ab47d8b917cd7d016facd7933de39 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "Getvalueuint32Ability_desc", + "value": "description" + }, + { + "name": "Getvalueuint32Ability_label", + "value": "JsvmBasicDataTypes" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..21603bee159ced57e6c1b9bfed71c56caa658c0f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetValueUint32', 0, async (done: Function) => { + console.log('uitest: TestGetValueUint32 begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'Getvalueuint32Ability' + } + 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('Getvalueuint32Ability'); + + 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: TestGetValueUint32 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..705f3fe838ebc48948bb79b9fbcbfa32e3431e8d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/getvalueuint32/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvalueuint32_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0bbbb33b0bd997de3ffe32ac6228075c5a4b1f0e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.2", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3b555a4269faeadc6caab93d696e194c30862899 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.2", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.21", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..2fd61feb9d41f297dd3f835af52ae281ce0e2885 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/ohosTest.md @@ -0,0 +1,15 @@ +# JsvmBasicDataTypes 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| --------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 根据Double类型对象创建JavaScript number对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据Int32_t类型对象创建JavaScript number对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据Int64_t类型对象创建JavaScript number对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 根据Uint32_t类型对象创建JavaScript number对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript number的Double基础类型值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript number的Int32基础类型值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript number的Int64基础类型值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取给定JavaScript number的Uint32基础类型值 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/screenshots/JsvmBasicDataTypes_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/screenshots/JsvmBasicDataTypes_1.png new file mode 100644 index 0000000000000000000000000000000000000000..8f4e60551bd803d01b94165c572da288627c981a Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/screenshots/JsvmBasicDataTypes_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/screenshots/JsvmBasicDataTypes_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/screenshots/JsvmBasicDataTypes_2.png new file mode 100644 index 0000000000000000000000000000000000000000..09093b971c79d669af42fd272b7dc5e680d1d2bc Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmBasicDataTypes/screenshots/JsvmBasicDataTypes_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..185fc255b029bdbcac6eb9940f8ebc070bb09a78 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmerror", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4fd0266eeb44c0a8c0439aa670081fd11dd7dc7e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmError" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9ed3ef1af24df53b226717ae1041a8119f71659f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/README.md @@ -0,0 +1,283 @@ +# ArkTs使用JSVM-API接口进行错误处理开发 + +### 介绍 + +使用JSVM-API接口进行错误处理开发,使得在JSVM-API模块中能够更好地管理和响应错误情况。通过合理使用这些函数,可以提高模块的稳定性和可靠性。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行错误处理开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-error.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :-----------------------------------------------------------: | :-----------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +createerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createrangeerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreaterangeerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── createrangeerrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createsyntaxerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatesyntaxerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createsyntaxerrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createtypeerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libcreatetypeerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── createtypeerrorability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getandclearlastexception/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetandclearlastexception + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getandclearlastexceptionability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getlasterrorinfo/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetlasterrorinfo + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getlasterrorinfoability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +iserror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libiserror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── iserrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +isexceptionpending/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libisexceptionpending + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── isexceptionpendingability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +throwerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libthrowerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── throwerrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +throwrangeerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libthrowrangeerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── throwrangeerrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +throwsyntaxerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libthrowsyntaxerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── throwsyntaxerrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +throwtypeerror/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libthrowtypeerror + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── throwtypeerrorability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6cbed5049222352a03a385e427eadd7a9efd82d5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/build-profile.json5 @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "createerror", + "srcPath": "./createerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "throwerror", + "srcPath": "./throwerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "throwtypeerror", + "srcPath": "./throwtypeerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "throwrangeerror", + "srcPath": "./throwrangeerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "throwsyntaxerror", + "srcPath": "./throwsyntaxerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "iserror", + "srcPath": "./iserror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createtypeerror", + "srcPath": "./createtypeerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createrangeerror", + "srcPath": "./createrangeerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "createsyntaxerror", + "srcPath": "./createsyntaxerror", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getandclearlastexception", + "srcPath": "./getandclearlastexception", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "isexceptionpending", + "srcPath": "./isexceptionpending", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getlasterrorinfo", + "srcPath": "./getlasterrorinfo", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..45fc020f213fd0625ea76343dbbf4812a8038631 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreateerror.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..646502a8b6d574b61ad16a550affc88da5f0f314 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4724bf8d7f83bc15f2cc245a366b74a9f8349df --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/hello.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_create_error] +// 捕获清除并打印错误,该函数作为公共函数,在本文档后续样例中不再声明和定义 +static void GetLastErrorAndClean(JSVM_Env env) +{ + // 调用OH_JSVM_GetAndClearLastException接口获取并清除最后一个未处理的异常。即使存在挂起的JavaScript异常,也可以调用此API + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result); + // 打印错误信息 + JSVM_Value message; + JSVM_Value errorCode; + OH_JSVM_GetNamedProperty((env), result, "message", &message); + OH_JSVM_GetNamedProperty((env), result, "code", &errorCode); + const int maxMessageLength = 256; + const int maxCodeLength = 256; + char messagestr[maxMessageLength]; + char codeStr[maxCodeLength]; + OH_JSVM_GetValueStringUtf8(env, message, messagestr, maxMessageLength, nullptr); + OH_JSVM_GetValueStringUtf8(env, errorCode, codeStr, maxCodeLength, nullptr); + OH_LOG_INFO(LOG_APP, "JSVM error message: %{public}s, error code: %{public}s", messagestr, codeStr); +} + +// OH_JSVM_CreateError的样例方法 +static JSVM_Value JsVmCreateThrowError(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 在JSVM环境中创建一个字符串,并将其存储在errorCode变量中 + JSVM_Value errorCode = nullptr; + OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode); + // 在JSVM环境中创建一个字符串,并将其存储在errorMessage变量中 + JSVM_Value errorMessage = nullptr; + OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage); + // 创建一个JavaScript对象error + JSVM_Value error = nullptr; + OH_JSVM_CreateError(env, errorCode, errorMessage, &error); + // 通过OH_JSVM_Throw接口将对象抛出 + OH_JSVM_Throw(env, error); + GetLastErrorAndClean(env); + return nullptr; +} + +// JsVmThrow注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmCreateThrowError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmThrow方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmCreateThrowError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmCreateThrowError();)JS"; +// [End oh_jsvm_create_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..08ccb34006f06bf86f549b1269179705b2e39605 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreateerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0adc1a4d4033ffc1119c91a1e23ea4de3193005f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreateerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1afc6e120aaad5a0fa714e89a2ef30d49f36f8e6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createerror", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..05c747fe963a7c53e44928c6e79f4675ca367e39 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7dd6267a0d6ba940a7b935e2cb31e08990009f45 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateError', 0, async (done: Function) => { + console.log('uitest: TestCreateError 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: TestCreateError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4afacccb72cad7b1254e870172735868b3d165a0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createerror/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createerror_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..794eed302f6a87b7484b39681f04cd33962a6580 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createrangeerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreaterangeerror.so": "file:./src/main/cpp/types/libcreaterangeerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..d408997169682709598785b4675c3ce71557ce12 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createrangeerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createrangeerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b63e5932bbba81e06f802dfc706f834f4693e455 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_create_range_error] +// OH_JSVM_CreateRangeError的样例方法 +static JSVM_Value JsVmCreateRangeError(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 在JSVM环境中创建一个字符串,并将其存储在errorCode变量中 + JSVM_Value errorCode = nullptr; + OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode); + // 在JSVM环境中创建一个字符串,并将其存储在errorMessage变量中 + JSVM_Value errorMessage = nullptr; + OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage); + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateRangeError(env, errorCode, errorMessage, &result); + if (status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM API CreateRangeError SUCCESS"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM API CreateRangeError FAILED"); + } + return result; +} +// JsVmCreateRangeError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmCreateRangeError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmCreateRangeError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmCreateRangeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmCreateRangeError();)JS"; +// [End oh_jsvm_create_range_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createrangeerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/types/libcreaterangeerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/types/libcreaterangeerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/types/libcreaterangeerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/types/libcreaterangeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/types/libcreaterangeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f4ae2861bcfce16c00897e27904c7640db616d03 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/cpp/types/libcreaterangeerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreaterangeerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/ets/createrangeerrorability/CreaterangeerrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/ets/createrangeerrorability/CreaterangeerrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..c22cc9d9060adb37f78580ee6f893d2993b98ca0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/ets/createrangeerrorability/CreaterangeerrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreaterangeerrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..32026a9b1f3a6e69ed68b110ce052175cea6013a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreaterangeerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0c27fd2f396854016993c03755046e7edffe4a28 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createrangeerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreaterangeerrorAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreaterangeerrorAbility", + "srcEntry": "./ets/createrangeerrorability/CreaterangeerrorAbility.ets", + "description": "$string:CreaterangeerrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreaterangeerrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..cb358fc78c5766a9d136ae26da31bfcaf21c8323 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreaterangeerrorAbility_desc", + "value": "description" + }, + { + "name": "CreaterangeerrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7a80e0145e4547c87dc6ed463dca617ddc975644 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateRangeError', 0, async (done: Function) => { + console.log('uitest: TestCreateRangeError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreaterangeerrorAbility' + } + 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('CreaterangeerrorAbility'); + + 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: TestCreateRangeError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..efa99ca79b1097e8c16607b41949accd47bd3426 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createrangeerror/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createrangeerror_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cc5e28cfd281cfe543d65978630c9e317a415993 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createsyntaxerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatesyntaxerror.so": "file:./src/main/cpp/types/libcreatesyntaxerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..69e1f39691657128493a26d4b0b9e7c098d445d4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createsyntaxerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createsyntaxerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a42ca8818fa6fae99f53f670a194d6c44f0a0797 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_create_syntax_error] +// OH_JSVM_CreateSyntaxError的样例方法 +static JSVM_Value JsVmCreateSyntaxError(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 在JSVM环境中创建一个字符串,并将其存储在errorCode变量中 + JSVM_Value errorCode = nullptr; + OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode); + // 在JSVM环境中创建一个字符串,并将其存储在errorMessage变量中 + JSVM_Value errorMessage = nullptr; + OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage); + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateSyntaxError(env, errorCode, errorMessage, &result); + if (status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM API CreateSyntaxError SUCCESS"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM API CreateSyntaxError FAILED"); + } + return result; +} +// JsVmCreateSyntaxError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmCreateSyntaxError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmThrow方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmCreateSyntaxError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmCreateSyntaxError();)JS"; +// [End oh_jsvm_create_syntax_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createsyntaxerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/types/libcreatesyntaxerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/types/libcreatesyntaxerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/types/libcreatesyntaxerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/types/libcreatesyntaxerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/types/libcreatesyntaxerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1bae7049ef4933b413e35f0c9e13e39c76e71ecd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/cpp/types/libcreatesyntaxerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatesyntaxerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/ets/createsyntaxerrorability/CreatesyntaxerrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/ets/createsyntaxerrorability/CreatesyntaxerrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b9fa1783748a233a288ae6d0db0f8821c0b282cd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/ets/createsyntaxerrorability/CreatesyntaxerrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreatesyntaxerrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8680f3871c4cca741105498d16559fb98a7ee7f1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatesyntaxerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..751d118ee6efc97785542b0af824450172afb369 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createsyntaxerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreatesyntaxerrorAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreatesyntaxerrorAbility", + "srcEntry": "./ets/createsyntaxerrorability/CreatesyntaxerrorAbility.ets", + "description": "$string:CreatesyntaxerrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreatesyntaxerrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..f1a5b4372d1768a34b7f8ca5999fac7ebaa6e56d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreatesyntaxerrorAbility_desc", + "value": "description" + }, + { + "name": "CreatesyntaxerrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..724e339a2a7ec957716fa8f35cbaa0c54f03d307 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateSyntaxError', 0, async (done: Function) => { + console.log('uitest: TestCreateSyntaxError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreatesyntaxerrorAbility' + } + 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('CreatesyntaxerrorAbility'); + + 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: TestCreateSyntaxError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..17a8fd92c1e27ba0c770eb95e45856d6df01fff4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createsyntaxerror/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createsyntaxerror_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d3699fb5510cd3042e2d15787b9e46358706fbbb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createtypeerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatetypeerror.so": "file:./src/main/cpp/types/libcreatetypeerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..fd081f46ea46ccee823487ad276cc8595ae45d04 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createtypeerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createtypeerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b914e402bbc50f57e563127de3d09f0a973401fb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_create_type_error] +// OH_JSVM_CreateTypeError的样例方法 +static JSVM_Value JsVmCreateTypeError(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 在JSVM环境中创建一个字符串,并将其存储在errorCode变量中 + JSVM_Value errorCode = nullptr; + OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode); + // 在JSVM环境中创建一个字符串,并将其存储在errorMessage变量中 + JSVM_Value errorMessage = nullptr; + OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage); + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_CreateTypeError(env, errorCode, errorMessage, &result); + if (status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM API Create TypeError SUCCESS"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM API Create TypeError FAILED"); + } + return result; +} +// JsVmCreateTypeError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmCreateTypeError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmCreateTypeError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmCreateTypeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmCreateTypeError();)JS"; +// [End oh_jsvm_create_type_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createtypeerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/types/libcreatetypeerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/types/libcreatetypeerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/types/libcreatetypeerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/types/libcreatetypeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/types/libcreatetypeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fddd88934e49acba7e4d0186cd3887c824758bd7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/cpp/types/libcreatetypeerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatetypeerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/ets/createtypeerrorability/CreateTypeErrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/ets/createtypeerrorability/CreateTypeErrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..60e7992c8f914757d3bbcb42eccae0af8c063c49 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/ets/createtypeerrorability/CreateTypeErrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class CreateTypeErrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..5561ff42c3ba1d1e2773b450af7b319d0c5c405d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatetypeerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..0635f758165d9c65bb27b88c740fbecbbbee9c20 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createtypeerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "CreateTypeErrorAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "CreateTypeErrorAbility", + "srcEntry": "./ets/createtypeerrorability/CreateTypeErrorAbility.ets", + "description": "$string:CreateTypeErrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:CreateTypeErrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1b0fb6f6669cf1e6a591a489d4e346ff18fa7bd9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "CreateTypeErrorAbility_desc", + "value": "description" + }, + { + "name": "CreateTypeErrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..23b7bc2384d6f8ddf1779b4ea8405451ec351ab4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testCreateTypeError', 0, async (done: Function) => { + console.log('uitest: TestCreateTypeError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'CreateTypeErrorAbility' + } + 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('CreateTypeErrorAbility'); + + 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: TestCreateTypeError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..47c64e9faf1dc8ab7c4fa414fb9e619f37ddba78 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/createtypeerror/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createtypeerror_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ce9fb1f628d737667d32b52f1f1964b5047bc45f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getandclearlastexception", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetandclearlastexception.so": "file:./src/main/cpp/types/libgetandclearlastexception" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1f3a62ea9111f4d8d67bdf8728942e26ff9356f0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getandclearlastexception SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getandclearlastexception PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a64b404d11f0935de8c1dcb70547190099719d9d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/hello.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_and_clear_last_exception] +// OH_JSVM_GetAndClearLastException的样例方法 +static JSVM_Value JsVmGetAndClearLastException(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 抛出异常,创造异常情况 + OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError errorCode", "OH_JSVM_ThrowError errorMessage"); + // 调用OH_JSVM_GetAndClearLastException接口获取并清除最后一个未处理的异常。即使存在挂起的JavaScript异常,也可以调用此API + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result); + if (status != JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_GetAndClearLastException FAILED"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_GetAndClearLastException SUCCESS"); + } + return result; +} +// JsVmGetAndClearLastException注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmGetAndClearLastException}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmGetAndClearLastException方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmGetAndClearLastException", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmGetAndClearLastException();)JS"; +// [End oh_jsvm_get_and_clear_last_exception] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getandclearlastexception", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/types/libgetandclearlastexception/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/types/libgetandclearlastexception/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/types/libgetandclearlastexception/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/types/libgetandclearlastexception/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/types/libgetandclearlastexception/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4ff65b223c56c15626197f64f70c846369ade636 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/cpp/types/libgetandclearlastexception/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetandclearlastexception.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/ets/getandclearlastexceptionability/GetandclearlastexceptionAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/ets/getandclearlastexceptionability/GetandclearlastexceptionAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..e23d25077c5408c2e1149af19212c74716b482fe --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/ets/getandclearlastexceptionability/GetandclearlastexceptionAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetandclearlastexceptionAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..cd32c826799b8f8253e5f53fef97c74c7ba1822a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetandclearlastexception.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4891dbbfdebeae1cfaa72d34fe5d2c5e319e3280 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getandclearlastexception", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetandclearlastexceptionAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetandclearlastexceptionAbility", + "srcEntry": "./ets/getandclearlastexceptionability/GetandclearlastexceptionAbility.ets", + "description": "$string:GetandclearlastexceptionAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetandclearlastexceptionAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..c7016b49b9da4821d09c14c694b69fbfd002dc7b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetandclearlastexceptionAbility_desc", + "value": "description" + }, + { + "name": "GetandclearlastexceptionAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..ae265192b6a53bedf436bd13b93aba8b5b029251 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetAndClearLastException', 0, async (done: Function) => { + console.log('uitest: TestGetAndClearLastException begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetandclearlastexceptionAbility' + } + 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('GetandclearlastexceptionAbility'); + + 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: TestGetAndClearLastException end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e8a5853c79815a1a389736df48c2057aa2af0fd0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getandclearlastexception/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getandclearlastexception_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..027afee4c3683a076fe3f60ecf4292cdee8e3abd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getlasterrorinfo", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetlasterrorinfo.so": "file:./src/main/cpp/types/libgetlasterrorinfo" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..89a61c4de9837c4c4c365c4c28902596710a4feb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getlasterrorinfo SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getlasterrorinfo PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f7b3f1237c36b2099ae033e866258124dea5431 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/hello.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_last_error_info] +// OH_JSVM_GetLastErrorInfo的样例方法 +static JSVM_Value JsVmGetLastErrorInfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取输入参数(这里以字符串message作为参数传入) + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 将传入的字符串参数以OH_JSVM_GetValueInt32取出,主动制造错误 + int32_t value = 0; + OH_JSVM_GetValueInt32(env, args[0], &value); + // 调用接口OH_JSVM_GetLastErrorInfo获取最后一次错误信息 + const JSVM_ExtendedErrorInfo *errorInfo; + OH_JSVM_GetLastErrorInfo(env, &errorInfo); + + // 取出错误消息作为返回值带出去打印 + JSVM_Value result = nullptr; + OH_LOG_INFO(LOG_APP, + "JSVM API OH_JSVM_GetLastErrorInfo: SUCCESS, error message is %{public}s, error code is %{public}d", + errorInfo->errorMessage, errorInfo->errorCode); + // 对异常进行处理,防止程序由于抛异常而退出 + JSVM_Value result1 = nullptr; + OH_JSVM_GetAndClearLastException(env, &result1); + OH_JSVM_CreateInt32(env, errorInfo->errorCode, &result); + return result; +} +// JsVmGetLastErrorInfo注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmGetLastErrorInfo}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmGetLastErrorInfo方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmGetLastErrorInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmGetLastErrorInfo();)JS"; +// [End oh_jsvm_get_last_error_info] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getlasterrorinfo", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/types/libgetlasterrorinfo/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/types/libgetlasterrorinfo/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/types/libgetlasterrorinfo/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/types/libgetlasterrorinfo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/types/libgetlasterrorinfo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cd07d9e1ff30f0e79831dfed9f802bd08db50074 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/cpp/types/libgetlasterrorinfo/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetlasterrorinfo.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/ets/getlasterrorinfoability/GetlasterrorInfoAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/ets/getlasterrorinfoability/GetlasterrorInfoAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..70352007a6595c22c15e0f633d84b2afea30999f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/ets/getlasterrorinfoability/GetlasterrorInfoAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetlasterrorInfoAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e488f42860517829b932c22fbc0ddd9b1b716c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetlasterrorinfo.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a4923980f38b52633ee73e7994bbb2f5ef44715c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getlasterrorinfo", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetlasterrorInfoAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetlasterrorInfoAbility", + "srcEntry": "./ets/getlasterrorinfoability/GetlasterrorInfoAbility.ets", + "description": "$string:GetlasterrorInfoAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetlasterrorInfoAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..16097cf65589d1faa951244e44bb4435e14b04f4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetlasterrorInfoAbility_desc", + "value": "description" + }, + { + "name": "GetlasterrorInfoAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..79afcc5677db20ebf9d6b99dd9089e4b243ec010 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetLastErrorInfo', 0, async (done: Function) => { + console.log('uitest: TestGetLastErrorInfo begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetlasterrorInfoAbility' + } + 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('GetlasterrorInfoAbility'); + + 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: TestGetLastErrorInfo end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c8353f702747ee19204bd7aa4c7219cc8eb2df3a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/getlasterrorinfo/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getlasterrorinfo_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7e807ee8cdf0b9e5320439088b3012418f4f3d86 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "iserror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libiserror.so": "file:./src/main/cpp/types/libiserror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..0688691815f5f2ed3de3c442c8b94ea8110da221 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(iserror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(iserror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf50296c12e2a6872a7ddf8384c5806a2ee20e2e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/hello.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_is_error] +// OH_JSVM_IsError的样例方法 +static JSVM_Value JsVmIsError(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 调用接口OH_JSVM_IsError判断入参是否为一个error对象 + bool result = false; + // 如果JSVM_Value为一个error对象,则设置result为true的布尔值,否则设置为false + JSVM_Status status = OH_JSVM_IsError(env, args[0], &result); + if (status == JSVM_OK) { + OH_LOG_INFO(LOG_APP, "JSVM API call OH_JSVM_IsError success, result is %{public}d", result); + } else { + OH_LOG_INFO(LOG_APP, "JSVM API call OH_JSVM_IsError failed"); + } + // 取出result通过OH_JSVM_GetBoolean接口将取出的bool值转换为JSVM_Value类型的值返回出去 + JSVM_Value returnValue = nullptr; + OH_JSVM_GetBoolean(env, result, &returnValue); + return returnValue; +} +// JsVmIsError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmIsError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmIsError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmIsError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmIsError(Error()))JS"; +// [End oh_jsvm_is_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "iserror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/types/libiserror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/types/libiserror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/types/libiserror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/types/libiserror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/types/libiserror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..926afa128701ef325532483e5a43e18c05f13b9e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/cpp/types/libiserror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libiserror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/ets/iserrorability/IserrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/ets/iserrorability/IserrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..fef24b4d9e03f2d1a02f00a37dc2aed381922e44 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/ets/iserrorability/IserrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IserrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..59cc37cf1168d61d5d1fc6d3f18899e12d592916 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libiserror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..54300ca15bdfc24bb15879470b555a3c530d0df1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "iserror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IserrorAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IserrorAbility", + "srcEntry": "./ets/iserrorability/IserrorAbility.ets", + "description": "$string:IserrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IserrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..6b8f8fce3d4987ce83046755318c47379fc8291b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IserrorAbility_desc", + "value": "description" + }, + { + "name": "IserrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..09dd8628c07a7aac060caab554cf46447a2a1ce1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsError', 0, async (done: Function) => { + console.log('uitest: TestIsError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IserrorAbility' + } + 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('IserrorAbility'); + + 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: TestIsError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..378102b3e71973c25080c5264ed8744628c399b2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/iserror/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "iserror_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..52141205c9f0f549c425080b96b2e0ede72b8a71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "isexceptionpending", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libisexceptionpending.so": "file:./src/main/cpp/types/libisexceptionpending" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..c31f3153cc3664b52b87f2451de3121d6aa67750 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(isexceptionpending SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(isexceptionpending PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15530e8edf04d62031e6383142283241be595ef9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/hello.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_is_exception_pending] +// OH_JSVM_GetAndClearLastException的样例方法 +static JSVM_Value JsVmIsExceptionPending(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Status status; + bool isExceptionPending = false; + // 在执行一些可能引发异常的操作后 + OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError errorCode", "OH_JSVM_ThrowError errorMessage"); + // 检查当前环境中是否有异常挂起 + status = OH_JSVM_IsExceptionPending(env, &isExceptionPending); + if (status != JSVM_OK) { + return nullptr; + } + if (isExceptionPending) { + OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_IsExceptionPending: SUCCESS"); + // 处理异常挂起的情况 + JSVM_Value result = nullptr; + status = OH_JSVM_GetAndClearLastException(env, &result); + if (status != JSVM_OK) { + return nullptr; + } + // 将处理的异常返回出去 + return result; + } else { + OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_IsExceptionPending: FAILED"); + } + return nullptr; +} +// JsVmIsExceptionPending注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmIsExceptionPending}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmIsExceptionPending方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmIsExceptionPending", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmIsExceptionPending();)JS"; +// [End oh_jsvm_is_exception_pending] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "isexceptionpending", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/types/libisexceptionpending/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/types/libisexceptionpending/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/types/libisexceptionpending/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/types/libisexceptionpending/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/types/libisexceptionpending/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..93b0b1126a1faf89ac68a6231d954c977c6ef16b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/cpp/types/libisexceptionpending/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libisexceptionpending.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/ets/isexceptionpendingability/IsexceptionpendingAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/ets/isexceptionpendingability/IsexceptionpendingAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..257fee01bddbc70a98d7935c78d90eae9b2d7ba1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/ets/isexceptionpendingability/IsexceptionpendingAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class IsexceptionpendingAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8898b91ea2f14c47ee64cb24b87e179ddc7560eb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libisexceptionpending.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7a33aebf396e2f7a635e60f176ec27900e08565b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isexceptionpending", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "IsexceptionpendingAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "IsexceptionpendingAbility", + "srcEntry": "./ets/isexceptionpendingability/IsexceptionpendingAbility.ets", + "description": "$string:IsexceptionpendingAbility_desc", + "icon": "$media:layered_image", + "label": "$string:IsexceptionpendingAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3399f5a2fa01d721498b0ebd3443df2fc00bec6a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "IsexceptionpendingAbility_desc", + "value": "description" + }, + { + "name": "IsexceptionpendingAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..eb34bd84017ca874f6f403c3bb1b170a7941fe35 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testIsExceptionPending', 0, async (done: Function) => { + console.log('uitest: TestIsExceptionPending begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'IsexceptionpendingAbility' + } + 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('IsexceptionpendingAbility'); + + 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: TestIsExceptionPending end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c86b2c326c2d5de59f68448561d2c18f202e2fcb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/isexceptionpending/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "isexceptionpending_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..8e25fa8cc7a8710c82ccf19b0ee16751dc36175a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/ohosTest.md @@ -0,0 +1,20 @@ +# JsvmError 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ------------------------------------------------ | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript Error | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript RangeError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript SyntaxError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript TypeError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取并清除最近一次出现的异常 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 获取调用JSVM接口最后一次发生的错误信息 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断给定的JSVM_Value是否表示一个error对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断是否出现了异常 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 抛出一个带文本信息的JS Error | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript RangeError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript SyntaxError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript SyntaxError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 创建并获取一个带文本信息的JavaScript TypeError | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/screenshots/JsvmError_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/screenshots/JsvmError_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/screenshots/JsvmError_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/screenshots/JsvmError_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/screenshots/JsvmError_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/screenshots/JsvmError_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..aba6265ae563ef6525c3ce27128b3ef9ecccb1f1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "throwerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libthrowerror.so": "file:./src/main/cpp/types/libthrowerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..561b1fafd25ffac96c4eaf22aef112dc5ce6d6a7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(throwerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(throwerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1ae595fa19b8859d605fd41b45b94d7472c465a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/hello.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +static void GetLastErrorAndClean(JSVM_Env env) +{ + // 调用OH_JSVM_GetAndClearLastException接口获取并清除最后一个未处理的异常。即使存在挂起的JavaScript异常,也可以调用此API + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result); + // 打印错误信息 + JSVM_Value message; + JSVM_Value errorCode; + OH_JSVM_GetNamedProperty((env), result, "message", &message); + OH_JSVM_GetNamedProperty((env), result, "code", &errorCode); + const int maxMessageLength = 256; + const int maxCodeLength = 256; + char messagestr[maxMessageLength]; + char codeStr[maxCodeLength]; + OH_JSVM_GetValueStringUtf8(env, message, messagestr, maxMessageLength, nullptr); + OH_JSVM_GetValueStringUtf8(env, errorCode, codeStr, maxCodeLength, nullptr); + OH_LOG_INFO(LOG_APP, "JSVM error message: %{public}s, error code: %{public}s", messagestr, codeStr); +} + +// [Start oh_jsvm_throw_error] +// OH_JSVM_ThrowError的样例方法 +static JSVM_Value JsVmThrowError(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + if (argc == 0) { + // 如果没有传递参数,直接抛出错误 + OH_JSVM_ThrowError(env, "-1", "has Error"); + } else if (argc == 1) { + size_t length; + // 通过入参获取到javaScript侧传入的字符串长度。 + OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length); + char *buffer = new char[length + 1]; + // 获取入参的字符串内容。 + OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr); + // 作为error信息填入到OH_JSVM_ThrowError接口中。 + OH_JSVM_ThrowError(env, "self defined error code", buffer); + delete[] buffer; + } + GetLastErrorAndClean(env); + return nullptr; +} +// JsVmThrowError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmThrowError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmThrowError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmThrowError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmThrowError();jsVmThrowError("self defined error message");)JS"; +// [End oh_jsvm_throw_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "throwerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/types/libthrowerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/types/libthrowerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/types/libthrowerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/types/libthrowerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/types/libthrowerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e44e55f9001c789bd21cef11b0fe77c661bd2bfc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/cpp/types/libthrowerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libthrowerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb4f2484d26bdb1a90cd24c40d515bcc3654259b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libthrowerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/ets/throwerrorability/ThrowerrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/ets/throwerrorability/ThrowerrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..ec0453fc4da29bc16ccf674ff3d07279d434a675 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/ets/throwerrorability/ThrowerrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ThrowerrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..bb657a31b515a4b300027e097c24b7433e104301 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ThrowerrorAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ThrowerrorAbility", + "srcEntry": "./ets/throwerrorability/ThrowerrorAbility.ets", + "description": "$string:ThrowerrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ThrowerrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d7fcd5ad6f3e963a9e590e6824bc8c7c4429969b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ThrowerrorAbility_desc", + "value": "description" + }, + { + "name": "ThrowerrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..56ee26520593f14ff573c7960387ec1df1ad1caf --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testThrowError', 0, async (done: Function) => { + console.log('uitest: TestThrowError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ThrowerrorAbility' + } + 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('ThrowerrorAbility'); + + 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: TestThrowError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8d6e408d5fcc995a6a1a70646670189d936c4c81 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwerror/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwerror_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6366a341fe91fdeb8b263b57a1b4188b22836a6f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "throwrangeerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libthrowrangeerror.so": "file:./src/main/cpp/types/libthrowrangeerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..bb0d5f352bfbf3cc9b9c6d20b28a09dc1ca9f7a8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(throwrangeerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(throwrangeerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4269865e3ad7b5d6b28a133af621d0eaeaf73a6a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/hello.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// 捕获清除并打印错误,该函数作为公共函数,在本文档后续样例中不再声明和定义 +static void GetLastErrorAndClean(JSVM_Env env) +{ + // 调用OH_JSVM_GetAndClearLastException接口获取并清除最后一个未处理的异常。即使存在挂起的JavaScript异常,也可以调用此API + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result); + // 打印错误信息 + JSVM_Value message; + JSVM_Value errorCode; + OH_JSVM_GetNamedProperty((env), result, "message", &message); + OH_JSVM_GetNamedProperty((env), result, "code", &errorCode); + const int maxMessageLength = 256; + const int maxCodeLength = 256; + char messagestr[maxMessageLength]; + char codeStr[maxCodeLength]; + OH_JSVM_GetValueStringUtf8(env, message, messagestr, maxMessageLength, nullptr); + OH_JSVM_GetValueStringUtf8(env, errorCode, codeStr, maxCodeLength, nullptr); + OH_LOG_INFO(LOG_APP, "JSVM error message: %{public}s, error code: %{public}s", messagestr, codeStr); +} + +// [Start oh_jsvm_throw_range_error] +// OH_JSVM_ThrowRangeError的样例方法 +static JSVM_Value JsVmThrowRangeError(JSVM_Env env, JSVM_CallbackInfo info) +{ + // js侧传入两个参数 + size_t argc = 2; + JSVM_Value argv[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + // 如果传入参数个数不为2 + if (argc != 2) { + // 这里抛出一个RangeError + OH_JSVM_ThrowRangeError(env, "OH_JSVM_ThrowRangeError", "Expected two numbers as arguments"); + GetLastErrorAndClean(env); + return nullptr; + } + JSVM_Value result = nullptr; + OH_JSVM_GetBoolean(env, true, &result); + return result; +} +// JsVmThrowRangeError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmThrowRangeError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmThrowRangeError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmThrowRangeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmThrowRangeError(1);)JS"; +// [End oh_jsvm_throw_range_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "throwrangeerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/types/libthrowrangeerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/types/libthrowrangeerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/types/libthrowrangeerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/types/libthrowrangeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/types/libthrowrangeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fc45dc27dc2057f4a3989531be3a1f9350b3b0b5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/cpp/types/libthrowrangeerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libthrowrangeerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..51c64d091a4dee4a28c915e36a87313c4f73eea1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libthrowrangeerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/ets/throwrangeerrorability/ThrowrangeerrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/ets/throwrangeerrorability/ThrowrangeerrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..3d02deea2786329ba329edd24dc145510172d51d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/ets/throwrangeerrorability/ThrowrangeerrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ThrowrangeerrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..19f876193ac297ccb7f1b2ac5f33368b0851d870 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwrangeerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ThrowrangeerrorAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ThrowrangeerrorAbility", + "srcEntry": "./ets/throwrangeerrorability/ThrowrangeerrorAbility.ets", + "description": "$string:ThrowrangeerrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ThrowrangeerrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..136d4395f271512d170448ccfdff250a70a0010f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ThrowrangeerrorAbility_desc", + "value": "description" + }, + { + "name": "ThrowrangeerrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..424cae4a7ffbd909b4554628d8310ce8a67e4ce5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testThrowRangeError', 0, async (done: Function) => { + console.log('uitest: TestThrowRangeError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ThrowrangeerrorAbility' + } + 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('ThrowrangeerrorAbility'); + + 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: TestThrowRangeError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..20b06c3ea787fab1b5ba98a84a56ed12cc26a8d4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwrangeerror/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwrangeerror_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..53f860259f381a8b67766fd5e64e638fd1b73003 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "throwsyntaxerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libthrowsyntaxerror.so": "file:./src/main/cpp/types/libthrowsyntaxerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..680ef809be974477c9186a5a68173bc2a3cd5804 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(throwsyntaxerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(throwsyntaxerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba54e1427004ef429513c4e7415e863efc0f3717 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/hello.cpp @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// 捕获清除并打印错误,该函数作为公共函数,在本文档后续样例中不再声明和定义 +static void GetLastErrorAndClean(JSVM_Env env) +{ + // 调用OH_JSVM_GetAndClearLastException接口获取并清除最后一个未处理的异常。即使存在挂起的JavaScript异常,也可以调用此API + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result); + // 打印错误信息 + JSVM_Value message; + JSVM_Value errorCode; + OH_JSVM_GetNamedProperty((env), result, "message", &message); + OH_JSVM_GetNamedProperty((env), result, "code", &errorCode); + const int maxMessageLength = 256; + const int maxCodeLength = 256; + char messagestr[maxMessageLength]; + char codeStr[maxCodeLength]; + OH_JSVM_GetValueStringUtf8(env, message, messagestr, maxMessageLength, nullptr); + OH_JSVM_GetValueStringUtf8(env, errorCode, codeStr, maxCodeLength, nullptr); + OH_LOG_INFO(LOG_APP, "JSVM error message: %{public}s, error code: %{public}s", messagestr, codeStr); +} + +// [Start oh_jsvm_throw_syntax_error] +// OH_JSVM_ThrowSyntaxError的样例方法 +static JSVM_Value JsVmThrowSyntaxError(JSVM_Env env, JSVM_CallbackInfo info) +{ + // JS侧传入运行的JS代码 + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + JSVM_Script script = nullptr; + // 通过OH_JSVM_CompileScript接口编译JS代码 + OH_JSVM_CompileScript(env, argv[0], nullptr, 0, true, nullptr, &script); + JSVM_Value scriptResult = nullptr; + // 通过OH_JSVM_RunScript接口运行JS代码 + JSVM_Status status = OH_JSVM_RunScript(env, script, &scriptResult); + if (status != JSVM_OK) { + // 如果JSVM_RunScript接口返回状态不为JSVM_OK,则抛出一个SyntaxError + OH_JSVM_ThrowSyntaxError(env, "JsVmThrowSyntaxError", "throw syntax error"); + GetLastErrorAndClean(env); + return nullptr; + } + JSVM_Value result = nullptr; + OH_JSVM_GetBoolean(env, true, &result); + return result; +} +// JsVmThrowSyntaxError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmThrowSyntaxError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmThrowSyntaxError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmThrowSyntaxError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmThrowSyntaxError();)JS"; +// [End oh_jsvm_throw_syntax_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "throwsyntaxerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/types/libthrowsyntaxerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/types/libthrowsyntaxerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/types/libthrowsyntaxerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/types/libthrowsyntaxerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/types/libthrowsyntaxerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..da7cb3b77c82de216915498955d2aac2c954ecc6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/cpp/types/libthrowsyntaxerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libthrowsyntaxerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..b527e27d96c6a0b4bcce69add10aa3ab21aff30e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libthrowsyntaxerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/ets/throwsyntaxerrorability/ThrowsyntaxerrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/ets/throwsyntaxerrorability/ThrowsyntaxerrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..1e16c210f1678ad3d7c51cfca0fbfb0f1f488e4f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/ets/throwsyntaxerrorability/ThrowsyntaxerrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ThrowsyntaxerrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a9c59db7c7abf605e529c269d9913c10f611b303 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwsyntaxerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ThrowsyntaxerrorAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ThrowsyntaxerrorAbility", + "srcEntry": "./ets/throwsyntaxerrorability/ThrowsyntaxerrorAbility.ets", + "description": "$string:ThrowsyntaxerrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ThrowsyntaxerrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1cb0b10b447f02acc3d69ad1263a38584d41dd38 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ThrowsyntaxerrorAbility_desc", + "value": "description" + }, + { + "name": "ThrowsyntaxerrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..3bf8f3e6d0e45e518600f3e951d740c3217dbd2d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testThrowSyntaxError', 0, async (done: Function) => { + console.log('uitest: TestThrowSyntaxError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ThrowsyntaxerrorAbility' + } + 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('ThrowsyntaxerrorAbility'); + + 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: TestThrowSyntaxError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..88f620e8499fdbdb17459561f453d0847e9c4e6d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwsyntaxerror/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwsyntaxerror_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e0ee682a348217166df7cb7acef906fd1ef73fa5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "throwtypeerror", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libthrowtypeerror.so": "file:./src/main/cpp/types/libthrowtypeerror" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..48f97e21c2c5766353b414d2f4e4be3ca77b9012 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmError) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(throwtypeerror SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(throwtypeerror PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f29e4fba0099d370c9b5e43a6b37f2adc3ad8d31 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/hello.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +static void GetLastErrorAndClean(JSVM_Env env) +{ + // 调用OH_JSVM_GetAndClearLastException接口获取并清除最后一个未处理的异常。即使存在挂起的JavaScript异常,也可以调用此API + JSVM_Value result = nullptr; + JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result); + // 打印错误信息 + JSVM_Value message; + JSVM_Value errorCode; + OH_JSVM_GetNamedProperty((env), result, "message", &message); + OH_JSVM_GetNamedProperty((env), result, "code", &errorCode); + const int maxMessageLength = 256; + const int maxCodeLength = 256; + char messagestr[maxMessageLength]; + char codeStr[maxCodeLength]; + OH_JSVM_GetValueStringUtf8(env, message, messagestr, maxMessageLength, nullptr); + OH_JSVM_GetValueStringUtf8(env, errorCode, codeStr, maxCodeLength, nullptr); + OH_LOG_INFO(LOG_APP, "JSVM error message: %{public}s, error code: %{public}s", messagestr, codeStr); +} + +// [Start oh_jsvm_throw_type_error] +// OH_JSVM_ThrowTypeError的样例方法 +static JSVM_Value JsVmThrowTypeError(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); + if (argc == 0) { + // 如果没有传递参数,直接抛出错误 + OH_JSVM_ThrowTypeError(env, "-1", "throwing type error"); + } else if (argc == 1) { + size_t length = 0; + // 通过入参获取到javaScript侧传入的字符串长度 + OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length); + char *buffer = new char[length + 1]; + // 获取入参的字符串内容 + OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr); + // 作为error信息填入到OH_JSVM_ThrowError接口中 + OH_JSVM_ThrowTypeError(env, "self defined error code", buffer); + delete[] buffer; + } + GetLastErrorAndClean(env); + return nullptr; +} +// JsVmThrowTypeError注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsVmThrowTypeError}, +}; +static JSVM_CallbackStruct *method = param; +// JsVmThrowTypeError方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"jsVmThrowTypeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(jsVmThrowTypeError();jsVmThrowTypeError("self defined error message");)JS"; +// [End oh_jsvm_throw_type_error] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "throwtypeerror", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/types/libthrowtypeerror/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/types/libthrowtypeerror/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/types/libthrowtypeerror/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/types/libthrowtypeerror/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/types/libthrowtypeerror/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f14a6db9ce479d9512417b24e0fe8f2094f33224 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/cpp/types/libthrowtypeerror/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libthrowtypeerror.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a49adff02a87823a265b83cd4e8f6fa715dce6c2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libthrowtypeerror.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/ets/throwtypeerrorability/ThrowtypeerrorAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/ets/throwtypeerrorability/ThrowtypeerrorAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..2c1dab4730d58a1c018eb0b128b469d0048f57e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/ets/throwtypeerrorability/ThrowtypeerrorAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ThrowtypeerrorAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6a7f7e1f3c6c84781fa5f0009f0d5285f70642b2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/module.json5 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwtypeerror", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ThrowtypeerrorAbility", + "deviceTypes": [ + "default", + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ThrowtypeerrorAbility", + "srcEntry": "./ets/throwtypeerrorability/ThrowtypeerrorAbility.ets", + "description": "$string:ThrowtypeerrorAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ThrowtypeerrorAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..908e0f170b85598f786a81d6d158f7dc5d65bf7f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ThrowtypeerrorAbility_desc", + "value": "description" + }, + { + "name": "ThrowtypeerrorAbility_label", + "value": "JsvmError" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..5b234d6ecd865af6898f6943551dcd96c32f294c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testThrowTypeError', 0, async (done: Function) => { + console.log('uitest: TestThrowTypeError begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ThrowtypeerrorAbility' + } + 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('ThrowtypeerrorAbility'); + + 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: TestThrowTypeError end'); + done(); + }) + }) +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4d18ae367f49c69ca82cd9fb394d323dfa3bbada --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmError/throwtypeerror/src/ohosTest/module.json5 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "throwtypeerror_test", + "type": "feature", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..03f0b7aac0d4ce9f3184cf0201d4cdb0b5d7f4bd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.jsvmlifecycle", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..a81a963d2ca9ee54da5496e886e9a59e1f6112e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "JsvmLifeCycle" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fbc9deaac3d82b9278f9e3929c7c9e7b0f828c68 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/README.md @@ -0,0 +1,138 @@ +# ArkTs使用JSVM-API接口进行生命周期相关开发 + +### 介绍 + +在JSVM-API中,JSVM_Value是一个表示JavaScript值的抽象类型,它可以表示任何JavaScript值,包括基本类型(如数字、字符串、布尔值)和对象类型(如数组、函数、对象等)。 JSVM_Value的生命周期与其在JavaScript中的对应值的生命周期紧密相关。当JavaScript值被垃圾回收时,与之关联的JSVM_Value也将不再有效。重要的是不要在JavaScript值不再存在时尝试使用JSVM_Value。 + +框架层的scope通常用于管理JSVM_Value的生命周期。在JSVM-API中,可以使用OH_JSVM_OpenHandleScope和OH_JSVM_CloseHandleScope函数来创建和销毁scope。通过在scope内创建JSVM_Value,可以确保在scope结束时自动释放JSVM_Value,避免内存泄漏。 + +JSVM_Ref是一个JSVM-API类型,用于管理JSVM_Value的生命周期。JSVM_Ref允许您在JSVM_Value的生命周期内保持对其的引用,即使它已经超出了其原始上下文的范围。这使得您可以在不同的上下文中共享JSVM_Value,并确保在不再需要时正确释放其内存。 + +合理使用OH_JSVM_OpenHandleScope和OH_JSVM_CloseHandleScope管理JSVM_Value的生命周期,做到生命周期最小化,避免发生内存泄漏问题。 + +每个JSVM_Value属于特定的HandleScope,HandleScope通过OH_JSVM_OpenHandleScope和OH_JSVM_CloseHandleScope来建立和关闭,HandleScope关闭后,所属的JSVM_Value就会自动释放。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行生命周期相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-life-cycle.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :---------------------------------------------------------------: | :---------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +addfinalizer/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libaddfinalizer + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── addfinalizerability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +openescapablehandlescope/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libopenescapablehandlescope + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── openescapablehandlescopeability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +openhandlescope/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +referenceref/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libreferenceref + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── referencerefability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..8f7a117b0c0ec70ae8a442979fecae40d15334b3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "addfinalizer", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libaddfinalizer.so": "file:./src/main/cpp/types/libaddfinalizer" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6b5f8e481338c0d3863d44d94c6d775c05dbc685 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmLifeCycle) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(addfinalizer SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(addfinalizer PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1475353eef3c3302c291401f9f4b46b7fd9d306 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/hello.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_add_finalizer] +static int AddFinalizer(JSVM_VM vm, JSVM_Env env) +{ + // 打开 handlescope + JSVM_HandleScope handleScope; + CHECK_RET(OH_JSVM_OpenHandleScope(env, &handleScope)); + // 创建 object 并设置回调 + JSVM_Value obj; + CHECK_RET(OH_JSVM_CreateObject(env, &obj)); + CHECK_RET(OH_JSVM_AddFinalizer( + env, obj, nullptr, + [](JSVM_Env env, void *data, void *hint) -> void { + // Finalizer 方法,可在该方法中清理 Native 对象 + OH_LOG_INFO(LOG_APP, "JSVM: finalizer called."); + }, + nullptr, nullptr)); + OH_LOG_INFO(LOG_APP, "JSVM: finalizer added."); + // 关闭 handlescope,触发 GC,GC 时 Finalizer 会被调用 + CHECK_RET(OH_JSVM_CloseHandleScope(env, handleScope)); + OH_LOG_INFO(LOG_APP, "JSVM: before call gc."); + CHECK_RET(OH_JSVM_MemoryPressureNotification(env, JSVM_MemoryPressureLevel::JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL)); + OH_LOG_INFO(LOG_APP, "JSVM: after call gc."); + + return 0; +} + +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 int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "addfinalizer", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/types/libaddfinalizer/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/types/libaddfinalizer/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/types/libaddfinalizer/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/types/libaddfinalizer/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/types/libaddfinalizer/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2c7b363db44fe3c00a262ea3b7c7883ccba2de84 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/cpp/types/libaddfinalizer/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libaddfinalizer.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/ets/addfinalizerability/AddfinalizerAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/ets/addfinalizerability/AddfinalizerAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..3f175ec34f970a29960520067d7753fde77f8a05 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/ets/addfinalizerability/AddfinalizerAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class AddfinalizerAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ecafb6e1aa76975408c5da11a28ed7f78f39021 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libaddfinalizer.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d7637b4badac4ab8f1b8805eeeec455cdd679fe7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "addfinalizer", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "AddfinalizerAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "AddfinalizerAbility", + "srcEntry": "./ets/addfinalizerability/AddfinalizerAbility.ets", + "description": "$string:AddfinalizerAbility_desc", + "icon": "$media:layered_image", + "label": "$string:AddfinalizerAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..07298009eb7fbbd1a103648eb17ece950b529893 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "AddfinalizerAbility_desc", + "value": "description" + }, + { + "name": "AddfinalizerAbility_label", + "value": "JsvmLifeCycle" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..856c055bab897cb88883814cd19ae866070c1f7f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testAddFinalizer', 0, async (done: Function) => { + console.log('uitest: TestAddFinalizer begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'AddfinalizerAbility' + } + 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('AddfinalizerAbility'); + + 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: TestAddFinalizer end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cbed882e355404e47694ff17ee8364ffba2842e6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/addfinalizer/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "addfinalizer_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..903f0b9d4338d25018d0e1ec6b920a966772ae88 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/build-profile.json5 @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "openhandlescope", + "srcPath": "./openhandlescope", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "openescapablehandlescope", + "srcPath": "./openescapablehandlescope", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "referenceref", + "srcPath": "./referenceref", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "addfinalizer", + "srcPath": "./addfinalizer", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1e473e424320d4e68b16737b289f5c851bb19d36 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..d07a4aa352b945a84dcc5ee046f0828d35769147 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/ohosTest.md @@ -0,0 +1,11 @@ +# JsvmLifeCycle 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| ---------------------------------------------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 为对象添加JSVM_Finalize回调,以便在JavaScript对象被垃圾回收时调用来释放原生对象 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 打开一个新的scope逃逸Handle scope,在关闭该scope之前创建的对象与父作用域有相同的生命周期 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 打开一个Handle scope,确保scope范围内的JSVM_Value不被GC回收 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 增加由Hello World创建的引用的引用计数,以确保对象在有引用时不会被提前释放 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f0cdca70796e03dcf71f2256a32affa04e2bf7bd --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "openescapablehandlescope", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libopenescapablehandlescope.so": "file:./src/main/cpp/types/libopenescapablehandlescope" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6009db002510ae53c0920c455c2604568d167b54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmLifeCycle) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(openescapablehandlescope SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(openescapablehandlescope PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbf119d706c1db74813cc572d9c63f1a613ca1af --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/hello.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_open_escapable_handle_scope_close_escapable_handle_scope_escape_handle] +// OH_JSVM_OpenEscapableHandleScope、OH_JSVM_CloseEscapableHandleScope、OH_JSVM_EscapeHandle的样例方法 +static JSVM_Value EscapableHandleScopeTest(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 创建一个可逃逸的句柄作用域 + JSVM_EscapableHandleScope scope = nullptr; + JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env, &scope); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_OpenEscapableHandleScope: failed"); + return nullptr; + } + // 在可逃逸的句柄作用域内创建一个obj + JSVM_Value obj; + OH_JSVM_CreateObject(env, &obj); + // 在对象中添加属性 + JSVM_Value value = nullptr; + OH_JSVM_CreateStringUtf8(env, "Test jsvm_escapable_handle_scope", JSVM_AUTO_LENGTH, &value); + OH_JSVM_SetNamedProperty(env, obj, "name", value); + // 调用OH_JSVM_EscapeHandle将对象逃逸到作用域之外 + JSVM_Value escapedObj = nullptr; + OH_JSVM_EscapeHandle(env, scope, obj, &escapedObj); + // 关闭可逃逸的句柄作用域,清理资源 + status = OH_JSVM_CloseEscapableHandleScope(env, scope); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CloseEscapableHandleScope: failed"); + return nullptr; + } + // 此时的escapedObj已逃逸,可以在作用域外继续使用escapedObj + bool result = false; + OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &value); + OH_JSVM_HasProperty(env, escapedObj, value, &result); + if (result) { + OH_LOG_INFO(LOG_APP, "JSVM EscapableHandleScopeTest: success"); + } + return escapedObj; +} + +// EscapableHandleScopeTest注册回调 +static JSVM_CallbackStruct param[] = { + {.callback = EscapableHandleScopeTest, .data = nullptr}, +}; +static JSVM_CallbackStruct *method = param; +// EscapableHandleScopeTest方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"escapableHandleScopeTest", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = "escapableHandleScopeTest()"; +// [End oh_jsvm_open_escapable_handle_scope_close_escapable_handle_scope_escape_handle] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "openescapablehandlescope", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/types/libopenescapablehandlescope/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/types/libopenescapablehandlescope/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/types/libopenescapablehandlescope/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/types/libopenescapablehandlescope/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/types/libopenescapablehandlescope/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..fdf21229621254e83368b7fcfb6e3eb104b7167a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/cpp/types/libopenescapablehandlescope/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libopenescapablehandlescope.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/ets/openescapablehandlescopeability/OpenescapablehandlescopeAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/ets/openescapablehandlescopeability/OpenescapablehandlescopeAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..6802668c1435816eab9873a79e2a22044680393c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/ets/openescapablehandlescopeability/OpenescapablehandlescopeAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class OpenescapablehandlescopeAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..25099de28d093494ad1d6e56be334073a01e81d9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libopenescapablehandlescope.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ac31360dee87bac2aa7308e8cb5f78a7db424473 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "openescapablehandlescope", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "OpenescapablehandlescopeAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "OpenescapablehandlescopeAbility", + "srcEntry": "./ets/openescapablehandlescopeability/OpenescapablehandlescopeAbility.ets", + "description": "$string:OpenescapablehandlescopeAbility_desc", + "icon": "$media:layered_image", + "label": "$string:OpenescapablehandlescopeAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..603926c5ce7ecd45a9ad8dfe90cb803fe59db77d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "OpenescapablehandlescopeAbility_desc", + "value": "description" + }, + { + "name": "OpenescapablehandlescopeAbility_label", + "value": "JsvmLifeCycle" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..a4b372d04bbdb8425ab59ea3cab9e467abc9dfea --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testOpenEscapableHandleScope', 0, async (done: Function) => { + console.log('uitest: TestOpenEscapableHandleScope begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'OpenescapablehandlescopeAbility' + } + 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('OpenescapablehandlescopeAbility'); + + 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: TestOpenEscapableHandleScope end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..99aacbc46ab44bec0db93d6282458998a85bfe8f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openescapablehandlescope/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "openescapablehandlescope_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7afe9b6f3693a69073444e48278e0f26f2bf0f7b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "openhandlescope", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libopenhandlescope.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..14b69294913f02091b3387579edd5549224bcbeb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmLifeCycle) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(openhandlescope SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(openhandlescope PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6cbf9a8b3b723940381423f133e066e973375f6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/hello.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_open_handle_scope_and_oh_jsvm_close_handle_scope] +// OH_JSVM_OpenHandleScope、OH_JSVM_CloseHandleScope的三种样例方法 +static JSVM_Value HandleScopeFor(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 在for循环中频繁调用JSVM接口创建js对象时,要加handle_scope及时释放不再使用的资源。 + // 下面例子中,每次循环结束局部变量res的生命周期已结束,因此加scope及时释放其持有的js对象,防止内存泄漏 + constexpr uint32_t DIFF_VALUE_HUNDRED_THOUSAND = 10000; + JSVM_Value checked = nullptr; + for (int i = 0; i < DIFF_VALUE_HUNDRED_THOUSAND; i++) { + JSVM_HandleScope scope = nullptr; + JSVM_Status status = OH_JSVM_OpenHandleScope(env, &scope); + if (status != JSVM_OK || scope == nullptr) { + OH_JSVM_GetBoolean(env, false, &checked); + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_OpenHandleScope: failed"); + return checked; + } + JSVM_Value res = nullptr; + OH_JSVM_CreateObject(env, &res); + status = OH_JSVM_CloseHandleScope(env, scope); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CloseHandleScope: failed"); + } + } + OH_JSVM_GetBoolean(env, true, &checked); + OH_LOG_INFO(LOG_APP, "JSVM HandleScopeFor: success"); + return checked; +} + +// HandleScopeFor注册回调 +static JSVM_CallbackStruct param[] = { + {.callback = HandleScopeFor, .data = nullptr}, +}; + +static JSVM_CallbackStruct *method = param; +// HandleScopeFor方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"HandleScopeFor", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = "HandleScopeFor()"; +// [End oh_jsvm_open_handle_scope_and_oh_jsvm_close_handle_scope] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "openhandlescope", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7d6e6febff745ee031374cee7d9d1ef37c19aa75 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libopenhandlescope.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8a8cd1c9b4a87ce6fe54b1b89603d9c2fcc4b9cb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libopenhandlescope.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1c9a28b2fa3543b486d1e1d0330e1a447ff9e431 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "openhandlescope", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..b6f5d69fa12fcc7a32bf475e7fe8942d3db231e0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "JsvmLifeCycle" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..da98ac03f9c082ea92839520fe88e2cea54a9243 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testOpenHandleScope', 0, async (done: Function) => { + console.log('uitest: TestOpenHandleScope 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: TestOpenHandleScope end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d34e9992399d1bd2dbf76975f16a43b207c7f656 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/openhandlescope/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "openhandlescope_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1b0a19f319251a416183feeca3e6f333ec27b2f1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "referenceref", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libreferenceref.so": "file:./src/main/cpp/types/libreferenceref" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..cfff8a97cd98b407a408103fcea54b5d07331464 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(JsvmLifeCycle) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(referenceref SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(referenceref PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09fa88cbb068fa1946ae98a8a05f73fc5a27eeb5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/hello.cpp @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_reference_ref_and_oh_jsvm_reference_unref] +static JSVM_Value UseReference(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 创建 JavaScript 对象 + JSVM_Value obj = nullptr; + OH_JSVM_CreateObject(env, &obj); + JSVM_Value value = nullptr; + OH_JSVM_CreateStringUtf8(env, "UseReference", JSVM_AUTO_LENGTH, &value); + OH_JSVM_SetNamedProperty(env, obj, "name", value); + + JSVM_Ref g_ref = nullptr; + // 创建对JavaScript对象的引用 + JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &g_ref); + if (status != JSVM_OK) { + return nullptr; + } + + // 增加传入引用的引用计数并返回生成的引用计数 + uint32_t result; + OH_JSVM_ReferenceRef(env, g_ref, &result); + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_ReferenceRef, count = %{public}d.", result); + const int resultValue = 2; + if (result != resultValue) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_ReferenceRef: failed"); + return nullptr; + } + + // 减少传入引用的引用计数并返回生成的引用计数 + uint32_t num; + OH_JSVM_ReferenceUnref(env, g_ref, &num); + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_ReferenceUnref, count = %{public}d.", num); + if (num != 1) { + return nullptr; + } + + JSVM_Value object = nullptr; + // 通过调用OH_JSVM_GetReferenceValue获取引用的JavaScript对象 + status = OH_JSVM_GetReferenceValue(env, g_ref, &object); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetReferenceValue: failed"); + return nullptr; + } + + // 不再使用引用,通过调用OH_JSVM_DeleteReference删除对JavaScript对象的引用 + status = OH_JSVM_DeleteReference(env, g_ref); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_DeleteReference: failed"); + return nullptr; + } + + // 将获取到的对象返回 + OH_LOG_INFO(LOG_APP, "JSVM UseReference success"); + return object; +} + +// CreateReference、UseReference、DeleteReference注册回调 +static JSVM_CallbackStruct param[] = { + {.callback = UseReference, .data = nullptr}, +}; +static JSVM_CallbackStruct *method = param; +// CreateReference、UseReference、DeleteReference方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"useReference", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = "useReference()"; +// [End oh_jsvm_reference_ref_and_oh_jsvm_reference_unref] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "referenceref", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/types/libreferenceref/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/types/libreferenceref/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/types/libreferenceref/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/types/libreferenceref/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/types/libreferenceref/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..00b60dcce9db302511492f32cedd7c9c3690a584 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/cpp/types/libreferenceref/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libreferenceref.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..19bc6dba180d233023fb9d9fe8c27ae3d5ced3aa --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libreferenceref.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/ets/referencerefability/ReferencerefAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/ets/referencerefability/ReferencerefAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..5d3dd16ceeef8ca18d5a4cad066fc84990f272d3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/ets/referencerefability/ReferencerefAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class ReferencerefAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ab9624e612fc693f3a1e0f9f3ae3ff341c99f44c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "referenceref", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "ReferencerefAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "ReferencerefAbility", + "srcEntry": "./ets/referencerefability/ReferencerefAbility.ets", + "description": "$string:ReferencerefAbility_desc", + "icon": "$media:layered_image", + "label": "$string:ReferencerefAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..de9920cf09653a3a17a58577c2610c932832e7e9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "ReferencerefAbility_desc", + "value": "description" + }, + { + "name": "ReferencerefAbility_label", + "value": "JsvmLifeCycle" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..b6cc3c556aaff9bcd248caf220e8ab47ffbadea7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testReferenceRef', 0, async (done: Function) => { + console.log('uitest: TestReferenceRef begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'ReferencerefAbility' + } + 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('ReferencerefAbility'); + + 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: TestReferenceRef end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..59eb234df77e795ae3994050a3e85697e24f1075 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/referenceref/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "referenceref_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/screenshots/JsvmLifeCycle_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/screenshots/JsvmLifeCycle_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/screenshots/JsvmLifeCycle_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/screenshots/JsvmLifeCycle_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/screenshots/JsvmLifeCycle_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle/screenshots/JsvmLifeCycle_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ad92f03cd682bb8e0f6ae555a5e0d3933009513d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.usageinstructionsone", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..16413e8d3183cd7bc0258cc04e1bf8e3318801ed --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "UsageInstructionsOne" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/README.md new file mode 100644 index 0000000000000000000000000000000000000000..127eeb9dcb73a418d695b83b0d183a8c4170e466 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/README.md @@ -0,0 +1,156 @@ +# ArkTS JSVM-API使用指导 + +### 介绍 + +使用JSVM-API实现跨语言交互,首先需要按照JSVM-API的机制实现模块的注册和加载等相关动作。 + +- ArkTS/JS侧:实现C++方法的调用。代码比较简单,import一个对应的so库后,即可调用C++方法。 +- Native侧:.cpp文件,实现模块的注册。需要提供注册lib库的名称,并在注册回调方法中定义接口的映射关系,即Native方法及对应的JS/ArkTS接口名称等。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口进行函数创建和调用](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-function-call.md) +- [使用JSVM-API接口进行虚拟机快照相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-create-snapshot.md) +- [使用JSVM-API接口进行JSON操作](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-JSON.md) +- [使用JSVM-API接口进行任务队列相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-execute_tasks.md) +- [使用JSVM-API接口进行WebAssembly模块相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-wasm.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :----------------------------------------------------------------------: | :----------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击hello world,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +aboutjson/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libaboutjson + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── aboutjsonability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +createsnapshot/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +functioncall/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libfunctioncall + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── functioncallability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +pumpmessageloop/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libpumpmessageloop + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── pumpmessageloopability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 + webassembly/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libwebassembly + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── webassemblyability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3eef1a4384628633d921b7851f4d61fd37f7cfa0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "aboutjson", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libaboutjson.so": "file:./src/main/cpp/types/libaboutjson" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..7e4c935ba66de48ceb9a96e74986e50f2adbfb56 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(aboutjson SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(aboutjson PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6403e34d678c31aa6b24dca1867c29b73d78931 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/hello.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include +#include + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_json_parse_and_json_stringify] +// 解析JSON数字 +static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 设置要解析的JSON数字 + std::string strNumber = "10.555"; + JSVM_Value jsonString; + JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strNumber.c_str(), strNumber.size(), &jsonString)); + JSVM_Value jsonObject; + // 调用OH_JSVM_JsonParse函数解析JSON数字,并将结果存储在JSON对象中 + JSVM_CALL(OH_JSVM_JsonParse(env, jsonString, &jsonObject)); + double number; + JSVM_CALL(OH_JSVM_GetValueDouble(env, jsonObject, &number)); + OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseNumber: %{public}f", number); + return nullptr; +} + +// 解析JSON字符串中的对象 +static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 设置要解析的JSON对象字符串 + std::string strObject = "{\"first\": \"one\", \"second\": \"two\", \"third\": \"three\"}"; + JSVM_Value strJson; + JSVM_CALL(OH_JSVM_CreateStringUtf8(env, strObject.c_str(), strObject.size(), &strJson)); + JSVM_Value jsonObject; + // 调用OH_JSVM_JsonParse函数解析JSON对象字符串,并将结果存储在JSON对象中 + JSVM_CALL(OH_JSVM_JsonParse(env, strJson, &jsonObject)); + JSVM_Value jsonString; + // 调用OH_JSVM_JsonStringify函数将对象转换为字符串格式,并将结果存储在JSVM字符串对象中 + JSVM_CALL(OH_JSVM_JsonStringify(env, jsonObject, &jsonString)); + size_t totalLen = 0; + JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, nullptr, 0, &totalLen)); + size_t needLen = totalLen + 1; + char* buff = new char[needLen]; + JSVM_CALL(OH_JSVM_GetValueStringUtf8(env, jsonString, buff, needLen, &totalLen)); + OH_LOG_INFO(LOG_APP, "Test JSVM jsonParseNumber: %{public}s", buff); + delete[] buff; + return nullptr; +} + +// JsonParse注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = JsonParseNumber}, + {.data = nullptr, .callback = JsonParseObject}, +}; + +static JSVM_CallbackStruct *method = param; + +JSVM_PropertyDescriptor descriptor[] = { + {"jsonParseNumber", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + {"jsonParseObject", nullptr, method + 1, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 待执行的js代码 +static const char *STR_TASK = R"JS(jsonParseNumber();jsonParseObject();)JS"; +// [End oh_jsvm_json_parse_and_json_stringify] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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, STR_TASK, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "aboutjson", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/types/libaboutjson/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/types/libaboutjson/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/types/libaboutjson/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/types/libaboutjson/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/types/libaboutjson/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..4186c9f0331a66e9b827a7826ee9888283c3fc33 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/cpp/types/libaboutjson/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libaboutjson.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/ets/aboutjsonability/AboutjsonAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/ets/aboutjsonability/AboutjsonAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..ac9499d7d33261087c18eb0ec49b3ab6359bc8ac --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/ets/aboutjsonability/AboutjsonAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class AboutjsonAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..4ba46b0d01055709619100b87d58773fa2bd5e91 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libaboutjson.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6cb67f9f71483ff5943e031a22e51252b557b31c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "aboutjson", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "AboutjsonAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "AboutjsonAbility", + "srcEntry": "./ets/aboutjsonability/AboutjsonAbility.ets", + "description": "$string:AboutjsonAbility_desc", + "icon": "$media:layered_image", + "label": "$string:AboutjsonAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e2fa38f441547155e11fc4541ea6c9c4b10d33a6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "AboutjsonAbility_desc", + "value": "description" + }, + { + "name": "AboutjsonAbility_label", + "value": "UsageInstructionsOne" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..e91a9e1ef884dd6f79d63d7506bde6fc47fadaa3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testAboutJSON', 0, async (done: Function) => { + console.log('uitest: TestAboutJSON begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'AboutjsonAbility' + } + 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('AboutjsonAbility'); + + 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: TestAboutJSON end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..5b74fd35ad14fff07f470cab9e6a564615b31534 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/aboutjson/src/ohosTest/module.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "aboutjson_test", + "type": "feature", + "deviceTypes": [ + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..20480d47b2b74ab87df9d37194e4acbb27264bf6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/build-profile.json5 @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "createsnapshot", + "srcPath": "./createsnapshot", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "functioncall", + "srcPath": "./functioncall", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "aboutjson", + "srcPath": "./aboutjson", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "pumpmessageloop", + "srcPath": "./pumpmessageloop", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "webassembly", + "srcPath": "./webassembly", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..23894ad544570fe9718dffd758b294301e11daec --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "createsnapshot", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libcreatesnapshot.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..d48847ce5945f278cdc95f7127d3ee0f45c6bf7a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(createsnapshot SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(createsnapshot PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..530b701adc33faf51bf00af404ccc2c1fb114fb1 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/hello.cpp @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_create_snapshot_and_create_env_from_snapshot] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include +#include + +#define LOG_DEMAIN 0x0202 +#define LOG_TAG "TEST_TAG" + +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; \ + } \ + } 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; \ + } \ + } 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; \ + } \ + } while (0) + +// JSVM_CALL_BASE的简化版本,返回nullptr +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +static const int MAX_BUFFER_SIZE = 128; +// CreateHelloString()函数需绑定到JSVM虚拟机, 用于OH_JSVM_CreateSnapshot虚拟机快照的正常创建 +static JSVM_Value CreateHelloString(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Value outPut; + OH_JSVM_CreateStringUtf8(env, "Hello world!", JSVM_AUTO_LENGTH, &outPut); + return outPut; +} +// 提供外部引用的方式以便JavaScript环境可以调用绑定的函数 +static JSVM_CallbackStruct helloCb = {CreateHelloString, nullptr}; + +static intptr_t g_externals[] = { + (intptr_t)&helloCb, + 0, +}; + +static JSVM_Value RunVMScript(JSVM_Env env, std::string &src) +{ + // 打开handleScope作用域 + JSVM_HandleScope handleScope; + OH_JSVM_OpenHandleScope(env, &handleScope); + JSVM_Value jsStr = nullptr; + OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsStr); + // 编译JavaScript代码 + JSVM_Script script; + OH_JSVM_CompileScript(env, jsStr, nullptr, 0, true, nullptr, &script); + // 执行JavaScript代码 + JSVM_Value result = nullptr; + OH_JSVM_RunScript(env, script, &result); + // 关闭handleScope作用域 + OH_JSVM_CloseHandleScope(env, handleScope); + return result; +} +// OH_JSVM_CreateSnapshot的样例方法 +static void CreateVMSnapshot() +{ + // 创建JavaScript虚拟机实例,打开虚拟机作用域 + JSVM_VM vm; + JSVM_CreateVMOptions vmOptions; + memset(&vmOptions, 0, sizeof(vmOptions)); + // isForSnapshotting设置该虚拟机是否用于创建快照 + vmOptions.isForSnapshotting = true; + OH_JSVM_CreateVM(&vmOptions, &vm); + JSVM_VMScope vmScope; + OH_JSVM_OpenVMScope(vm, &vmScope); + // 创建JavaScript环境,打开环境作用域 + JSVM_Env env; + // 将native函数注册成JavaScript可调用的方法 + JSVM_PropertyDescriptor descriptor[] = { + {"createHelloString", nullptr, &helloCb, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + }; + OH_JSVM_CreateEnv(vm, 1, descriptor, &env); + JSVM_EnvScope envScope; + OH_JSVM_OpenEnvScope(env, &envScope); + // 使用OH_JSVM_CreateSnapshot创建虚拟机的启动快照 + const char *blobData = nullptr; + size_t blobSize = 0; + JSVM_Env envs[1] = {env}; + OH_JSVM_CreateSnapshot(vm, 1, envs, &blobData, &blobSize); + // 将snapshot保存到文件中 + // 保存快照数据,/data/storage/el2/base/files/test_blob.bin为沙箱路径 + // 以包名为com.example.jsvm为例,实际文件会保存到/data/app/el2/100/base/com.example.jsvm/files/test_blob.bin + std::ofstream file("/data/storage/el2/base/files/test_blob.bin", + std::ios::out | std::ios::binary | std::ios::trunc); + file.write(blobData, blobSize); + file.close(); + // 关闭并销毁环境和虚拟机 + OH_JSVM_CloseEnvScope(env, envScope); + OH_JSVM_DestroyEnv(env); + OH_JSVM_CloseVMScope(vm, vmScope); + OH_JSVM_DestroyVM(vm); +} + +static void RunVMSnapshot() +{ + // blobData的生命周期不能短于vm的生命周期 + // 从文件中读取snapshot + std::vector blobData; + std::ifstream file("/data/storage/el2/base/files/test_blob.bin", std::ios::in | std::ios::binary | std::ios::ate); + size_t blobSize = file.tellg(); + blobData.resize(blobSize); + file.seekg(0, std::ios::beg); + file.read(blobData.data(), blobSize); + file.close(); + OH_LOG_INFO(LOG_APP, "Test JSVM RunVMSnapshot read file blobSize = : %{public}ld", blobSize); + // 使用快照数据创建虚拟机实例 + JSVM_VM vm; + JSVM_CreateVMOptions vmOptions; + memset(&vmOptions, 0, sizeof(vmOptions)); + vmOptions.snapshotBlobData = blobData.data(); + vmOptions.snapshotBlobSize = blobSize; + OH_JSVM_CreateVM(&vmOptions, &vm); + JSVM_VMScope vmScope; + OH_JSVM_OpenVMScope(vm, &vmScope); + // 从快照中创建环境env + JSVM_Env env; + OH_JSVM_CreateEnvFromSnapshot(vm, 0, &env); + JSVM_EnvScope envScope; + OH_JSVM_OpenEnvScope(env, &envScope); + // 执行js脚本,快照记录的env中定义了createHelloString() + std::string src = "createHelloString()"; + JSVM_Value result = RunVMScript(env, src); + // 环境关闭前检查脚本运行结果 + char str[MAX_BUFFER_SIZE]; + OH_JSVM_GetValueStringUtf8(env, result, str, MAX_BUFFER_SIZE, nullptr); + if (strcmp(str, "Hello world!") != 0) { + OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d", __FILE__, __LINE__); + } + // 关闭并销毁环境和虚拟机 + OH_JSVM_CloseEnvScope(env, envScope); + OH_JSVM_DestroyEnv(env); + OH_JSVM_CloseVMScope(vm, vmScope); + OH_JSVM_DestroyVM(vm); + return; +} + +static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 在创建虚拟机快照时,如果存在对外部的依赖,需要在OH_JSVM_Init时,将外部依赖注册到initOptions.externalReferences中 + // 创建虚拟机快照并将快照保存到文件中 + CreateVMSnapshot(); + // snapshot可以记录下特定的js执行环境,可以跨进程通过snapshot快速还原出js执行上下文环境 + RunVMSnapshot(); + JSVM_Value result = nullptr; + OH_JSVM_CreateInt32(env, 0, &result); + return result; +} + +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = AdjustExternalMemory}, +}; +static JSVM_CallbackStruct *method = param; +// AdjustExternalMemory方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"adjustExternalMemory", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试JS +const char *SRC_CALL_NATIVE = R"JS(adjustExternalMemory();)JS"; + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + JSVM_Env env = nullptr; + JSVM_VMScope vmScope; + JSVM_EnvScope envScope; + JSVM_HandleScope handleScope; + JSVM_Value result; + // 初始化JavaScript引擎实例 + if (g_aa == 0) { + g_aa++; + initOptions.externalReferences = g_externals; + int argc = 0; + char **argv = nullptr; + initOptions.argc = &argc; + initOptions.argv = argv; + 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; +} + +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_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "createsnapshot", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } +// [End oh_jsvm_create_snapshot_and_create_env_from_snapshot] diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..dfb4b188c2ae8961b9e7d00be177aefa1f17649a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libcreatesnapshot.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..0e181902f311b2f7dc314db5885f7ac693189476 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/ets/pages/Index.ets @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libcreatesnapshot.so'; + +// [Start create_snapshot_call_native] +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} +// [End create_snapshot_call_native] diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..769e893b6b882f896b1ff7a7dc47f2ffa53c133a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createsnapshot", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3b974782ef1cb4db4e7e515932f0def6cd89cf1d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "UsageInstructionsOne" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..4b108b49b4b3ef2e1759af90f7a963bd8cc0d5c3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testVirtualMachine', 0, async (done: Function) => { + console.log('uitest: TestVirtualMachine 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: TestVirtualMachine end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6350e0f5d33f3fee1ecba2213fe3a8bfeea726c9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/createsnapshot/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "createsnapshot_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d615c366197ba752d6b64ab0641242f50dab452d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "functioncall", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libfunctioncall.so": "file:./src/main/cpp/types/libfunctioncall" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a93514b1236fc59e6955dad5defc2435d8f02d42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(functioncall SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(functioncall PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecb51ba8931a82502b448463cbf7e9c17e938106 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/hello.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start jsvm_function_call] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +#define CHECK_RET(cond) \ + if ((cond)) { \ + 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(cond) \ + if (!(cond)) { \ + OH_LOG_ERROR(LOG_APP, "jsvm fail file: %{public}s line: %{public}d ret = %{public}d", __FILE__, __LINE__, \ + cond); \ + return -1; \ + } + +JSVM_Value NativeCreateFunctionTest(JSVM_Env env, JSVM_CallbackInfo info) +{ + void *data; + size_t argc = 1; + JSVM_Value argv[1] = {nullptr}; + JSVM_Value thisArg; + // 获取callback 参数信息 + JSVM_Status ret = OH_JSVM_GetCbInfo(env, info, &argc, &argv[0], &thisArg, &data); + if (ret != 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__, ret, info != nullptr ? info->errorMessage : ""); + return nullptr; + } + const int maxMessageLength = 256; + char message[256]; + OH_JSVM_GetValueStringLatin1(env, argv[0], message, maxMessageLength, nullptr); + if (data == nullptr) { + OH_LOG_ERROR(LOG_APP, "jsvm: %{public}s; callback data null", message); + } else { + OH_LOG_INFO(LOG_APP, "jsvm: %{public}s; %{public}s", message, (char *)data); + } + return nullptr; +} + +static int32_t TEST_FUNC() +{ + JSVM_InitOptions initOptions{}; + JSVM_VM vm; + JSVM_Env env = nullptr; + JSVM_VMScope vmScope; + JSVM_EnvScope envScope; + JSVM_HandleScope handleScope; + JSVM_Value result; + static bool isVMInit = false; + if (!isVMInit) { + isVMInit = true; + // 单个进程只用初始化一次 + OH_JSVM_Init(&initOptions); + } + CHECK_RET(OH_JSVM_CreateVM(nullptr, &vm)); + CHECK_RET(OH_JSVM_CreateEnv(vm, 0, nullptr, &env)); + CHECK_RET(OH_JSVM_OpenVMScope(vm, &vmScope)); + CHECK_RET(OH_JSVM_OpenEnvScope(env, &envScope)); + CHECK_RET(OH_JSVM_OpenHandleScope(env, &handleScope)); + + // 创建并检查函数 + char hello[] = "Hello World!"; + JSVM_CallbackStruct cb = {NativeCreateFunctionTest, (void *)hello}; + JSVM_Value func; + CHECK_RET(OH_JSVM_CreateFunction(env, "", JSVM_AUTO_LENGTH, &cb, &func)); + bool isFunction = false; + CHECK_RET(OH_JSVM_IsFunction(env, func, &isFunction)); + CHECK(isFunction); + + // 将函数设置到全局对象中 + JSVM_Value global; + CHECK_RET(OH_JSVM_GetGlobal(env, &global)); + JSVM_Value key; + CHECK_RET(OH_JSVM_CreateStringUtf8(env, "NativeFunc", JSVM_AUTO_LENGTH, &key)); + CHECK_RET(OH_JSVM_SetProperty(env, global, key, func)); + + // 通过call 接口调用函数 + JSVM_Value argv[1] = {nullptr}; + OH_JSVM_CreateStringUtf8(env, "jsvm api call function", JSVM_AUTO_LENGTH, &argv[0]); + CHECK_RET(OH_JSVM_CallFunction(env, global, func, 1, argv, &result)); + + // 通过script调用函数 + JSVM_Script script; + JSVM_Value jsSrc; + const char *srcCallNative = R"JS(NativeFunc('js source call function');)JS"; + CHECK_RET(OH_JSVM_CreateStringUtf8(env, srcCallNative, JSVM_AUTO_LENGTH, &jsSrc)); + CHECK_RET(OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script)); + CHECK_RET(OH_JSVM_RunScript(env, script, &result)); + + CHECK_RET(OH_JSVM_CloseHandleScope(env, handleScope)); + CHECK_RET(OH_JSVM_CloseEnvScope(env, envScope)); + CHECK_RET(OH_JSVM_CloseVMScope(vm, vmScope)); + CHECK_RET(OH_JSVM_DestroyEnv(env)); + CHECK_RET(OH_JSVM_DestroyVM(vm)); + return 0; +} +// [End jsvm_function_call] + +static napi_value RunTest(napi_env env, napi_callback_info info) +{ + TEST_FUNC(); + return nullptr; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "functioncall", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/types/libfunctioncall/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/types/libfunctioncall/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/types/libfunctioncall/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/types/libfunctioncall/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/types/libfunctioncall/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..701827d530db7ecd032f023e1b68cfe29bc0058c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/cpp/types/libfunctioncall/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libfunctioncall.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/ets/functioncallability/FunctioncallAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/ets/functioncallability/FunctioncallAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..308ebdac2150d848f334de5bad65ed57af18ea79 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/ets/functioncallability/FunctioncallAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class FunctioncallAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..13de3baa1d434ea842c033320f316901a6032847 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libfunctioncall.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..79be9255c54edaef0b5f5e164f54b4d0f60328e0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "functioncall", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "FunctioncallAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "FunctioncallAbility", + "srcEntry": "./ets/functioncallability/FunctioncallAbility.ets", + "description": "$string:FunctioncallAbility_desc", + "icon": "$media:layered_image", + "label": "$string:FunctioncallAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ed9da58e521d8e0936fd1942302f52aebd1f4502 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "FunctioncallAbility_desc", + "value": "description" + }, + { + "name": "FunctioncallAbility_label", + "value": "UsageInstructionsOne" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..43bdcbc5c778116e23eb12bd623f481476c3a0f4 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testFunctionCall', 0, async (done: Function) => { + console.log('uitest: TestFunctionCall begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'FunctioncallAbility' + } + 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('FunctioncallAbility'); + + 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: TestFunctionCall end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..71d4c586d9a9338c4184cb5d969510f26b84cc1e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/functioncall/src/ohosTest/module.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "functioncall_test", + "type": "feature", + "deviceTypes": [ + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..343302a3ad0e1e176fdf53a74eeb56b3174d0f24 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/ohosTest.md @@ -0,0 +1,12 @@ +# UsageInstructionsOne 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +|---------------------------------|--------|---------------|------------------|------|------| +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 使用JSVM-API接口进行JSON操作 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 使用JSVM-API接口进行函数创建和调用 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 使用JSVM-API接口进行虚拟机快照相关开发 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 使用JSVM-API接口进行任务队列相关开发 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 使用JSVM-API接口进行WebAssembly模块相关开发 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..38bdcc9929e2c5bd7f51c4fc96a398ccebd0d6ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/build-profile.json5 @@ -0,0 +1,39 @@ +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..ca0dd64ef5cf5954330c5088bf0304069736d600 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/oh-package.json5 @@ -0,0 +1,11 @@ +{ + "name": "pumpmessageloop", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libpumpmessageloop.so": "file:./src/main/cpp/types/libpumpmessageloop" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6a1aee019c4c4e174e42df75d8b46e9ea49eb82b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(pumpmessageloop SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(pumpmessageloop PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86fdd57333e24303e2f268d410c44d0b7148ffd7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/hello.cpp @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [Start oh_jsvm_pump_message_loop_and_perform_microtask_checkpoint] +#include +#include +// [StartExclude oh_jsvm_pump_message_loop_and_perform_microtask_checkpoint] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_pump_message_loop_and_perform_microtask_checkpoint] + +// 待执行的js代码 +static const char *STR_TASK = R"JS( + // wasm 字节码 (以add 模块为例) + // 以下 wasmBuffer 对应的 wasm 字节码文本格式如下所示,只包含了一个函数 add + // (module + // (func $add (param $lhs i32) (param $rhs i32) (result i32) + // local.get $lhs + // local.get $rhs + // i32.add + // ) + // (export "add" (func $add)) + // ) + var wasmBytes = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, + 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, + 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, + 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b]); + + var p = WebAssembly.instantiate(wasmBytes, {}); + p.then((result) => { + consoleinfo("Called with instance " + result); + }); + p.finally(() => { + consoleinfo("Called Finally"); + }); +)JS"; + +// 保证js代码中的打印信息可以正常输出 +static JSVM_Value ConsoleInfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + size_t argc = 1; + JSVM_Value args[1]; + #define MAX_LOG_LENGTH 255 + char log[MAX_LOG_LENGTH + 1] = ""; + 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); + log[MAX_LOG_LENGTH] = 0; + OH_LOG_INFO(LOG_APP, "JSVM API TEST: %{public}s", log); + return nullptr; +} + +// 注册consoleinfo的方法 +JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = ConsoleInfo}, +}; +JSVM_PropertyDescriptor descriptor[] = { + {"consoleinfo", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT}, +}; + +static int32_t TestJSVM() +{ + JSVM_InitOptions init_options; + memset(&init_options, 0, sizeof(init_options)); + if (g_aa == 0) { + OH_JSVM_Init(&init_options); + g_aa++; + } + // 创建JavaScript虚拟机实例,打开虚拟机作用域 + JSVM_VM vm; + JSVM_CreateVMOptions options; + memset(&options, 0, sizeof(options)); + CHECK(OH_JSVM_CreateVM(&options, &vm)); + JSVM_VMScope vm_scope; + CHECK(OH_JSVM_OpenVMScope(vm, &vm_scope)); + + JSVM_Env env; + CHECK(OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env)); + JSVM_EnvScope envScope; + CHECK_RET(OH_JSVM_OpenEnvScope(env, &envScope)); + JSVM_HandleScope handlescope; + CHECK_RET(OH_JSVM_OpenHandleScope(env, &handlescope)); + JSVM_Value sourcecodevalue; + CHECK_RET(OH_JSVM_CreateStringUtf8(env, STR_TASK, strlen(STR_TASK), &sourcecodevalue)); + JSVM_Script script; + CHECK_RET(OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script)); + JSVM_Value result; + CHECK_RET(OH_JSVM_RunScript(env, script, &result)); + bool rst = false; + auto start = std::chrono::system_clock::now(); + while (true) { + // 如果任务队列中没有任务启动,则rst设置为false + CHECK_RET(OH_JSVM_PumpMessageLoop(vm, &rst)); + CHECK_RET(OH_JSVM_PerformMicrotaskCheckpoint(vm)); + // 定时退出 + auto now = std::chrono::system_clock::now(); + auto cost = std::chrono::duration_cast(now - start).count(); + const int timeoutMs = 100; + if (cost > timeoutMs) { + break; + } + } + + // 关闭并销毁环境和虚拟机 + CHECK_RET(OH_JSVM_CloseHandleScope(env, handlescope)); + CHECK_RET(OH_JSVM_CloseEnvScope(env, envScope)); + CHECK(OH_JSVM_DestroyEnv(env)); + CHECK(OH_JSVM_CloseVMScope(vm, vm_scope)); + CHECK(OH_JSVM_DestroyVM(vm)); + return 0; +} +// [End oh_jsvm_pump_message_loop_and_perform_microtask_checkpoint] + +static napi_value RunTest(napi_env env, napi_callback_info info) +{ + TestJSVM(); + return nullptr; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "pumpmessageloop", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/types/libpumpmessageloop/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/types/libpumpmessageloop/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/types/libpumpmessageloop/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/types/libpumpmessageloop/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/types/libpumpmessageloop/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..24f6bafaba63baf43e0ec70a23b59a083ba34a01 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/cpp/types/libpumpmessageloop/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libpumpmessageloop.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a6233fa3079e5e8c3c2839537566b1292b4699ff --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libpumpmessageloop.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/ets/pumpmessageloopability/PumpmessageloopAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/ets/pumpmessageloopability/PumpmessageloopAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..ce70b476cba95bff5b33c4f821ef7d81e737bea7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/ets/pumpmessageloopability/PumpmessageloopAbility.ets @@ -0,0 +1,41 @@ +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class PumpmessageloopAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..36fe86318c9193f8ee15a762c777e84bdf381032 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/module.json5 @@ -0,0 +1,27 @@ +{ + "module": { + "name": "pumpmessageloop", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "PumpmessageloopAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "PumpmessageloopAbility", + "srcEntry": "./ets/pumpmessageloopability/PumpmessageloopAbility.ets", + "description": "$string:PumpmessageloopAbility_desc", + "icon": "$media:layered_image", + "label": "$string:PumpmessageloopAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..16cbc00444cdbaf8429eaa5205fd587e54e935b8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "PumpmessageloopAbility_desc", + "value": "description" + }, + { + "name": "PumpmessageloopAbility_label", + "value": "UsageInstructionsOne" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f58841e4f3e9abfedd3333348711be0cda3622d2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击OH_JSVM_PumpMessageLoop执行 + */ + it('testPumpMessageLoop', 0, async (done: Function) => { + console.log('uitest: TestPumpMessageLoop begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'PumpmessageloopAbility' + } + 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('PumpmessageloopAbility'); + + await driver.delayMs(1000); + const text = await driver.findComponent(ON.text('OH_JSVM_PumpMessageLoop')); + await text.click(); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('success')); + console.log('uitest: TestPumpMessageLoop end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..794c7dc4ed66bd98fa3865e07922906e2fcef545 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,5 @@ +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..16319397009db7e037bd2e563476472d2b510007 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/pumpmessageloop/src/ohosTest/module.json5 @@ -0,0 +1,11 @@ +{ + "module": { + "name": "pumpmessageloop_test", + "type": "feature", + "deviceTypes": [ + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/screenshots/UsageInstructionsOne_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/screenshots/UsageInstructionsOne_1.png new file mode 100644 index 0000000000000000000000000000000000000000..c4232daaf4a64052f4037d3cf8b92d484255a202 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/screenshots/UsageInstructionsOne_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/screenshots/UsageInstructionsOne_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/screenshots/UsageInstructionsOne_2.png new file mode 100644 index 0000000000000000000000000000000000000000..9b36b7e54d43bbae826fcaacbbb109fb67ad0b4e Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/screenshots/UsageInstructionsOne_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6950099cf93009d42eccb564ceeb60df72b00929 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "webassembly", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libwebassembly.so": "file:./src/main/cpp/types/libwebassembly" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f6de652817d223936429fd4fa6fffa778ed8f487 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsOne) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +# 添加名为entry的库 +add_library(webassembly SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(webassembly PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ff615ef482c81a49087a0ef9f0f396b38a097c9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/hello.cpp @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start jsvm_wasm] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +#include +// [StartExclude jsvm_wasm] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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。 +#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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude jsvm_wasm] + +#ifndef CHECK_STATUS +#define CHECK_STATUS(cond) \ + do { \ + if (!(cond)) { \ + OH_LOG_ERROR(LOG_APP, "CHECK FAILED"); \ + } \ + } while (0) +#endif + +// 判断一个 JSVM_Value 是否是 wasm module +static bool IsWasmModuleObject(JSVM_Env env, JSVM_Value value) +{ + bool result; + JSVM_Status status = OH_JSVM_IsWasmModuleObject(env, value, &result); + CHECK_STATUS(status == JSVM_OK); + return result; +} + +// 由 C 字符串创建 JSVM string +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(status == JSVM_OK); + return jsvmStr; +} + +// 由 C int32_t 创建 JSVM number +static JSVM_Value CreateInt32(JSVM_Env env, int32_t val) +{ + JSVM_Value jsvmInt32; + JSVM_Status status = OH_JSVM_CreateInt32(env, val, &jsvmInt32); + CHECK_STATUS(status == JSVM_OK); + return jsvmInt32; +} + +// 对 wasm module 进行实例化 +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(status == JSVM_OK); + + JSVM_Value webAssembly; + status = OH_JSVM_GetProperty(env, globalThis, CreateString(env, "WebAssembly"), &webAssembly); + CHECK_STATUS(status == JSVM_OK); + + JSVM_Value webAssemblyInstance; + status = OH_JSVM_GetProperty(env, webAssembly, CreateString(env, "Instance"), &webAssemblyInstance); + CHECK_STATUS(status == JSVM_OK); + + JSVM_Value instance; + JSVM_Value argv[] = {wasmModule}; + status = OH_JSVM_NewInstance(env, webAssemblyInstance, 1, argv, &instance); + CHECK_STATUS(status == JSVM_OK); + return instance; +} + +// 获取 wasm 字节码 (add 模块) +static std::vector GetAddWasmBuffer() +{ + // 以下 wasmBuffer 对应的 wasm 字节码文本格式如下所示,只包含了一个函数 add + // (module + // (func $add (param $lhs i32) (param $rhs i32) (result i32) + // local.get $lhs + // local.get $rhs + // i32.add + // ) + // (export "add" (func $add)) + // ) + std::vector wasmBuffer = {0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, + 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, + 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, + 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b}; + return wasmBuffer; +} + +// 验证 wasm instance 功能 (add 模块) +static void VerifyAddWasmInstance(JSVM_Env env, JSVM_Value wasmInstance) +{ + JSVM_Status status = JSVM_OK; + // 从 wasm instance 获取 exports.add 函数 + JSVM_Value exports; + status = OH_JSVM_GetProperty(env, wasmInstance, CreateString(env, "exports"), &exports); + CHECK_STATUS(status == JSVM_OK); + + JSVM_Value add; + status = OH_JSVM_GetProperty(env, exports, CreateString(env, "add"), &add); + CHECK_STATUS(status == JSVM_OK); + + // 执行 exports.add(1, 2),期望得到结果 3 + JSVM_Value undefined; + OH_JSVM_GetUndefined(env, &undefined); + JSVM_Value one = CreateInt32(env, 1); + JSVM_Value two = CreateInt32(env, 2); + JSVM_Value argv[] = {one, two}; + JSVM_Value result; + const int argumentCount = 2; + status = OH_JSVM_CallFunction(env, undefined, add, argumentCount, argv, &result); + CHECK_STATUS(status == JSVM_OK); + int32_t resultInt32; + OH_JSVM_GetValueInt32(env, result, &resultInt32); + CHECK_STATUS(resultInt32 == 3); +} + +// WebAssembly demo 主函数 +static JSVM_Value WasmDemo(JSVM_Env env, JSVM_CallbackInfo info) +{ + JSVM_Status status = JSVM_OK; + std::vector wasmBuffer = GetAddWasmBuffer(); + uint8_t *wasmBytecode = wasmBuffer.data(); + size_t wasmBytecodeLength = wasmBuffer.size(); + JSVM_Value wasmModule; + // 根据 wasm 字节码得到 wasm module + status = OH_JSVM_CompileWasmModule(env, wasmBytecode, wasmBytecodeLength, NULL, 0, NULL, &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(status == JSVM_OK); + // 对编译得到的 wasm module 进行实例化 + JSVM_Value wasmInstance = InstantiateWasmModule(env, wasmModule); + // 对实例化的 wasm instance 中的函数进行功能验证 + VerifyAddWasmInstance(env, wasmInstance); + + // 创建 wasm cache + const uint8_t *wasmCacheData = NULL; + size_t wasmCacheLength = 0; + status = OH_JSVM_CreateWasmCache(env, wasmModule, &wasmCacheData, &wasmCacheLength); + CHECK_STATUS(status == JSVM_OK); + // 期望 wasm cache 创建成功 + CHECK_STATUS(wasmCacheData != NULL); + CHECK_STATUS(wasmCacheLength > 0); + + // 通过将 wasm cache 赋值来模拟 cache 持久化,实际使用场景可能将 wasm cache 保存到文件 + std::vector cacheBuffer(wasmCacheData, wasmCacheData + wasmCacheLength); + + // cache 一旦保存完成后,需要显式释放,以免发生内存泄露 + // 注意:传入的 JSVM_CacheType 必须匹配 + status = OH_JSVM_ReleaseCache(env, wasmCacheData, JSVM_CACHE_TYPE_WASM); + 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); + + // 传入的 wasm cache 如果是匹配的,且内部校验通过 (如版本),则会接受 cache + CHECK_STATUS(!cacheRejected); + CHECK_STATUS(IsWasmModuleObject(env, wasmModule2)); + + // 对反序列化得到的 wasmModule2 进行同样的操作:函数编译、实例化、验证功能,期望也都是通过的 + status = OH_JSVM_CompileWasmFunction(env, wasmModule2, functionIndex, JSVM_WASM_OPT_HIGH); + 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 *method = param; +// 将 C++ callback WasmDemo 函数注册为 JSVM globalThis.wasmDemo 属性,供 JS 侧调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"wasmDemo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +const char *SRC_CALL_NATIVE = R"JS(wasmDemo())JS"; +// [End jsvm_wasm] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "webassembly", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/types/libwebassembly/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/types/libwebassembly/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/types/libwebassembly/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/types/libwebassembly/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/types/libwebassembly/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..2f65fddcc411fc9081366e6edde3e0e544a77448 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/cpp/types/libwebassembly/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libwebassembly.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..73cbcf69c6686df78ac9d8c3581c33fd9a53e4ba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libwebassembly.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/ets/webassemblyability/WebassemblyAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/ets/webassemblyability/WebassemblyAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8bbcded7fd2bead9a3756598252745d79b3d33b7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/ets/webassemblyability/WebassemblyAbility.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class WebassemblyAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cc5efda2694a113cb3c2bb9cce49f4ece183cd63 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "webassembly", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "WebassemblyAbility", + "deviceTypes": [ + "tablet", + "default" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "WebassemblyAbility", + "srcEntry": "./ets/webassemblyability/WebassemblyAbility.ets", + "description": "$string:WebassemblyAbility_desc", + "icon": "$media:layered_image", + "label": "$string:WebassemblyAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/float.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4b12542c8a3452e47baf63a791a90286c94a9838 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "WebassemblyAbility_desc", + "value": "description" + }, + { + "name": "WebassemblyAbility_label", + "value": "UsageInstructionsOne" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..c075f98ffab831551d81ce3b7b66a90cca18f548 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testWebassembly', 0, async (done: Function) => { + console.log('uitest: TestWebassembly begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'WebassemblyAbility' + } + 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('WebassemblyAbility'); + + 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: TestWebassembly end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1f2fb3cc7bbde2b7bd7bb23cd66e40acddb6d832 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsOne/webassembly/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "webassembly_test", + "type": "feature", + "deviceTypes": [ + "tablet", + "default" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/app.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c845cc69ba13e1f0fc598d9f907d9380589f0d93 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.samples.usageinstructionstwo", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0ecae715a5d4c8d8ae829ee037727e3593d409b7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "UsageInstructionsTwo" + } + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/resources/base/media/app_icon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/AppScope/resources/base/media/app_icon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/README.md b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b4159b57f34ad9fe12a922024c77abda42e8406b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/README.md @@ -0,0 +1,174 @@ +# ArkTS JSVM-API使用指导 + +### 介绍 + +使用JSVM-API实现跨语言交互,首先需要按照JSVM-API的机制实现模块的注册和加载等相关动作。 + +- ArkTS/JS侧:实现C++方法的调用。代码比较简单,import一个对应的so库后,即可调用C++方法。 +- Native侧:.cpp文件,实现模块的注册。需要提供注册lib库的名称,并在注册回调方法中定义接口的映射关系,即Native方法及对应的JS/ArkTS接口名称等。 + +该工程中展示的代码详细描述可查如下链接: + +- [使用JSVM-API接口获取JSVM API的版本号](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-version.md) +- [使用JSVM-API接口进行JavaScript代码调试调优](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-heapstatistics-debugger-cpuprofiler-heapsnapshot.md) +- [使用JSVM-API判断给定的两个JS value是否严格相等](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-strict-equals.md) +- [使用JSVM-API进行内存管理](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-memory-management.md) + +### 效果预览 + +| 首页 | 执行及结果即时反馈 | +| :----------------------------------------------------------------------: | :----------------------------------------------------------------------: | +| | | + +### 使用说明 + +1. 在主界面,可以点击Hello World,开始执行。 +2. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 + +### 工程目录 + +``` +adjustexternalmemory/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libadjustexternalmemory + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── adjustexternalmemoryability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getheapstatistics/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetheapstatistics + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接口 + │ ├── ets + │ │ ├── getheapstatisticsability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getversion/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libgetversion + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── getversionability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +getvm/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libentry + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── entryability + │ │ ├── entrybackupability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +memorypressurenotification/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libmemorypressurenotification + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── memorypressurenotificationability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +strictequals/src/ + ├── main + │ ├── cpp + │ │ ├── types + │ │ │ ├── libstrictequals + │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 + │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 + │ │ ├── CMakeLists.txt // 配置CMake打包参数 + │ │ ├── hello.cpp // 实现Native侧的runTest接 + │ ├── ets + │ │ ├── strictequalsability + │ │ ├── pages + │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 + │ ├── module.json5 + │ └── resources + ├── ohosTest + │ ├── ets + │ │ ├── test + │ │ ├── Ability.test.ets // 自动化测试代码 +``` + +### 相关权限 + +不涉及。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:Phone。 + +2.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 + +3.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..60036cd797a6dbfd8219ccaf6da0c9afe0420dbc --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "adjustexternalmemory", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libadjustexternalmemory.so": "file:./src/main/cpp/types/libadjustexternalmemory" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..fcb47014e0914c1d06007d56ba85d57b3b70415a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(adjustexternalmemory SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(adjustexternalmemory PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34f3d637c6f317624be9d88b1b02a9d52b0930b2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/hello.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_adjust_external_memory] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_adjust_external_memory] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_adjust_external_memory] + +// OH_JSVM_AdjustExternalMemory的样例方法 +static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 分配1MB的内存 + int64_t change = 1024 * 1024; + int64_t adjustedValue = 0; + JSVM_Status status = OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_AdjustExternalMemory: success"); + OH_LOG_INFO(LOG_APP, "JSVM Allocate memory size: %{public}d", adjustedValue); + } + JSVM_Value checked; + OH_JSVM_GetBoolean(env, true, &checked); + return checked; +} +// AdjustExternalMemory注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = AdjustExternalMemory}, +}; +static JSVM_CallbackStruct *method = param; +// AdjustExternalMemory方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"adjustExternalMemory", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = R"JS(adjustExternalMemory())JS"; +// [End oh_jsvm_adjust_external_memory] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "adjustexternalmemory", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/types/libadjustexternalmemory/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/types/libadjustexternalmemory/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/types/libadjustexternalmemory/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/types/libadjustexternalmemory/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/types/libadjustexternalmemory/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1255828d1de9006032aa960058761eee2c8b0d83 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/cpp/types/libadjustexternalmemory/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libadjustexternalmemory.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/ets/adjustexternalmemoryability/AdjustexternalmemoryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/ets/adjustexternalmemoryability/AdjustexternalmemoryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..56cc3a6fa60a64e4f53ac6800aed297333326290 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/ets/adjustexternalmemoryability/AdjustexternalmemoryAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class AdjustexternalmemoryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..3eeda3302aba655e52057895426f5a794bff3f09 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libadjustexternalmemory.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..15384a3f96285aa8f90e819cc4b794cc708202ba --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "adjustexternalmemory", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "AdjustexternalmemoryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "AdjustexternalmemoryAbility", + "srcEntry": "./ets/adjustexternalmemoryability/AdjustexternalmemoryAbility.ets", + "description": "$string:AdjustexternalmemoryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:AdjustexternalmemoryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..625d290483d42a3ae063b70245278fa2be31605e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "AdjustexternalmemoryAbility_desc", + "value": "description" + }, + { + "name": "AdjustexternalmemoryAbility_label", + "value": "UsageInstructionsTwo" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f6ba6ddb80a862b2d170af90e09cf0a829163c27 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testAdjustExternalMemory', 0, async (done: Function) => { + console.log('uitest: TestAdjustExternalMemory begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'AdjustexternalmemoryAbility' + } + 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('AdjustexternalmemoryAbility'); + + 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: TestAdjustExternalMemory end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..7627e73168882bff0199e48e4616eb1e6e9cdcac --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/adjustexternalmemory/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "adjustexternalmemory_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b15eb62245f86312f4df334135e9be14ad6639b9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/build-profile.json5 @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compatibleSdkVersion": "5.0.3(15)", + "targetSdkVersion": "5.0.3(15)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "getvm", + "srcPath": "./getvm", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getheapstatistics", + "srcPath": "./getheapstatistics", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "getversion", + "srcPath": "./getversion", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "adjustexternalmemory", + "srcPath": "./adjustexternalmemory", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "memorypressurenotification", + "srcPath": "./memorypressurenotification", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + }, + { + "name": "strictequals", + "srcPath": "./strictequals", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/code-linter.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7b0d2fc8829dee61da4f034b491eca5a99268eb --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getheapstatistics", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetheapstatistics.so": "file:./src/main/cpp/types/libgetheapstatistics" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..9a635c19549916df26dfbba22fbda471a9f36f20 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getheapstatistics SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getheapstatistics PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..350ab85ce4cef2557145951fac4874404fb47900 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/hello.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_heap_statistics] +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" +// [StartExclude oh_jsvm_get_heap_statistics] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_heap_statistics] + +// OH_JSVM_GetHeapStatistics的样例方法 +void PrintHeapStatistics(JSVM_HeapStatistics result) +{ + OH_LOG_INFO(LOG_APP, "JSVM API heap totalHeapSize: %{public}zu", result.totalHeapSize); + OH_LOG_INFO(LOG_APP, "JSVM API heap totalHeapSizeExecutable: %{public}zu", result.totalHeapSizeExecutable); + OH_LOG_INFO(LOG_APP, "JSVM API heap totalPhysicalSize: %{public}zu", result.totalPhysicalSize); + OH_LOG_INFO(LOG_APP, "JSVM API heap totalAvailableSize: %{public}zu", result.totalAvailableSize); + OH_LOG_INFO(LOG_APP, "JSVM API heap usedHeapSize: %{public}zu", result.usedHeapSize); + OH_LOG_INFO(LOG_APP, "JSVM API heap heapSizeLimit: %{public}zu", result.heapSizeLimit); + OH_LOG_INFO(LOG_APP, "JSVM API heap mallocedMemory: %{public}zu", result.mallocedMemory); + OH_LOG_INFO(LOG_APP, "JSVM API heap externalMemory: %{public}zu", result.externalMemory); + OH_LOG_INFO(LOG_APP, "JSVM API heap peakMallocedMemory: %{public}zu", result.peakMallocedMemory); + OH_LOG_INFO(LOG_APP, "JSVM API heap numberOfNativeContexts: %{public}zu", result.numberOfNativeContexts); + OH_LOG_INFO(LOG_APP, "JSVM API heap numberOfDetachedContexts: %{public}zu", result.numberOfDetachedContexts); + OH_LOG_INFO(LOG_APP, "JSVM API heap totalGlobalHandlesSize: %{public}zu", result.totalGlobalHandlesSize); + OH_LOG_INFO(LOG_APP, "JSVM API heap usedGlobalHandlesSize: %{public}zu", result.usedGlobalHandlesSize); +} + +static JSVM_Value GetHeapStatistics(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取当前虚拟机对象 + JSVM_VM testVm; + OH_JSVM_GetVM(env, &testVm); + // 获取虚拟机的堆统计信息 + JSVM_HeapStatistics result; + OH_JSVM_GetHeapStatistics(testVm, &result); + // 打印虚拟机堆统计信息 + PrintHeapStatistics(result); + // 返回虚拟机堆统计信息中‘本机上下文数量’ + JSVM_Value nativeContextsCnt = nullptr; + OH_JSVM_CreateInt64(env, result.numberOfNativeContexts, &nativeContextsCnt); + return nativeContextsCnt; +} +// GetHeapStatistics注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetHeapStatistics}, +}; +static JSVM_CallbackStruct *method = param; +// GetHeapStatistics方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getHeapStatistics", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = R"JS(getHeapStatistics())JS"; +// [End oh_jsvm_get_heap_statistics] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getheapstatistics", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/types/libgetheapstatistics/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/types/libgetheapstatistics/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/types/libgetheapstatistics/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/types/libgetheapstatistics/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/types/libgetheapstatistics/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6740937c4baf8840a3e857a6b3cfa3ff4fea22be --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/cpp/types/libgetheapstatistics/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetheapstatistics.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/ets/getheapstatisticsability/GetheapstatisticsAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/ets/getheapstatisticsability/GetheapstatisticsAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..eb8b50a8fb48cf93c2a0300b8652f9961080ad94 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/ets/getheapstatisticsability/GetheapstatisticsAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetheapstatisticsAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..318154626578e0c7ad4374b5e0b794584783ff37 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetheapstatistics.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..81c388c0a419b0d0aaf081c53d3c2ce42417b6d2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getheapstatistics", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetheapstatisticsAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetheapstatisticsAbility", + "srcEntry": "./ets/getheapstatisticsability/GetheapstatisticsAbility.ets", + "description": "$string:GetheapstatisticsAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetheapstatisticsAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..cff501cf69d624a84541b975aa7a32c3a33fa3e7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetheapstatisticsAbility_desc", + "value": "description" + }, + { + "name": "GetheapstatisticsAbility_label", + "value": "UsageInstructionsTwo" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..043966859f2eb5425e2e8174054d0278a3da6823 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetHeapStatistics', 0, async (done: Function) => { + console.log('uitest: TestGetHeapStatistics begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetheapstatisticsAbility' + } + 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('GetheapstatisticsAbility'); + + 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: TestGetHeapStatistics end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f9931db74f22d137797a88c98a0220f594a7f7b6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getheapstatistics/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getheapstatistics_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d21aec907603f283c1d58309cbe4efc78f7ab81f --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getversion", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetversion.so": "file:./src/main/cpp/types/libgetversion" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..eb90367e4b40964e31010d48f25c30f6db2ea2e2 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getversion SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getversion PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fa6199e0d2cb6331661863c42f6ad5172938188 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/hello.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "hilog/log.h" +#include "ark_runtime/jsvm.h" + +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) + +// [Start oh_jsvm_get_version_and_vm_info] +// OH_JSVM_GetVersion的样例方法 +static JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info) +{ + uint32_t jsVersion = 0; + // 调用接口,获取当前JSVM运行时支持的最高JSVM API版本 + JSVM_CALL(OH_JSVM_GetVersion(env, &jsVersion)); + int value = static_cast(jsVersion); + OH_LOG_INFO(LOG_APP, "JSVM GetVersion success:%{public}d", value); + return nullptr; +} + +// OH_JSVM_GetVMInfo的样例方法 +// 打印JSVM(JavaScript虚拟机)的各项信息 +void PrintVmInfo(JSVM_VMInfo vmInfo) +{ + OH_LOG_INFO(LOG_APP, "JSVM API apiVersion: %{public}d", vmInfo.apiVersion); + OH_LOG_INFO(LOG_APP, "JSVM API engine: %{public}s", vmInfo.engine); + OH_LOG_INFO(LOG_APP, "JSVM API version: %{public}s", vmInfo.version); + OH_LOG_INFO(LOG_APP, "JSVM API cachedDataVersionTag: 0x%{public}x", vmInfo.cachedDataVersionTag); +} + +static JSVM_Value GetVMInfo(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 调用接口,获取虚拟机的信息 + JSVM_VMInfo result; + JSVM_CALL(OH_JSVM_GetVMInfo(&result)); + // 输出VM虚拟机信息 + PrintVmInfo(result); + return nullptr; +} + +// IsStrictEquals注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetVersion}, + {.data = nullptr, .callback = GetVMInfo}, +}; +static JSVM_CallbackStruct *method = param; +// IsStrictEquals方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getVersion", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, + {"getVMInfo", nullptr, method + 1, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +// 样例测试js +static const char *STR_TASK = R"JS(getVersion();getVMInfo();)JS"; +// [End oh_jsvm_get_version_and_vm_info] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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, STR_TASK, 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getversion", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/types/libgetversion/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/types/libgetversion/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/types/libgetversion/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/types/libgetversion/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/types/libgetversion/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..045101c18b71f603031eb57c1f80b35a46a93cff --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/cpp/types/libgetversion/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetversion.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/ets/getversionability/GetversionAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/ets/getversionability/GetversionAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..10f2c73bb6ab64acbb48a50aecbcf66791d5ae88 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/ets/getversionability/GetversionAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class GetversionAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..a403e9baf230f7482b1b787b415683cc1cedf588 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetversion.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..72d31930686b2e7d3d75e69e8fed5e546364d9d0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getversion", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "GetversionAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "GetversionAbility", + "srcEntry": "./ets/getversionability/GetversionAbility.ets", + "description": "$string:GetversionAbility_desc", + "icon": "$media:layered_image", + "label": "$string:GetversionAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..274263abc0e240b7dda247ba870160084df6e158 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "GetversionAbility_desc", + "value": "description" + }, + { + "name": "GetversionAbility_label", + "value": "UsageInstructionsTwo" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..89734ba4ead9254a8a753b160430e7c91236776d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetVersion', 0, async (done: Function) => { + console.log('uitest: TestGetVersion begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'GetversionAbility' + } + 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('GetversionAbility'); + + 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: TestGetVersion end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..84f25198a213b8a451a2fd55853614e60324008e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getversion/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getversion_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..265dbe9f725f573310ebe5326f21638519e58dc0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "getvm", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libgetvm.so": "file:./src/main/cpp/types/libentry" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4f4d6b6d9ee679374e63263eb4d6d703a590f7ed --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(getvm SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(getvm PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acb683d7dc640035942328e13b7324d49a3ddb81 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/hello.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_get_vm] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_get_vm] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_get_vm] + +// OH_JSVM_GetVM的样例方法 +static JSVM_Value GetVM(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 获取当前虚拟机对象,后续可以进行与虚拟机相关的操作或分析 + JSVM_VM testVm; + JSVM_Status status = OH_JSVM_GetVM(env, &testVm); + JSVM_Value result = nullptr; + if (status != JSVM_OK || testVm == nullptr) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetVM: failed"); + OH_JSVM_GetBoolean(env, true, &result); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetVM: success"); + OH_JSVM_GetBoolean(env, false, &result); + } + return result; +} +// GetVM注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = GetVM}, +}; +static JSVM_CallbackStruct *method = param; +// GetVM方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"getVM", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = R"JS(getVM())JS"; +// [End oh_jsvm_get_vm] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "getvm", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/types/libentry/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/types/libentry/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..05c7308f965753c803bea9d02488406271dcdd3d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libgetvm.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/entryability/EntryAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..965554bb3a331e2418180b86da1993baf5e8d536 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/entrybackupability/EntryBackupAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..3cf4799224e2cb0b4f5313ccdb932205f1c63118 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libgetvm.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..73807e1bab2aba895a063882561612698c43c575 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/module.json5 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvm", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..da67aa185b21542466bae158fb292d7eb63b11ca --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "UsageInstructionsTwo" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/profile/backup_config.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/dark/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..e98c7d638662d8577061657128cd7835b124e6a6 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testGetVM', 0, async (done: Function) => { + console.log('uitest: TestGetVM 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: TestGetVM end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d01da0917d98c994993163fd3c8f02595127a2d9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/getvm/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "getvm_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/hvigor/hvigor-config.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a5e543f190732c159beb574dfc9fa37bc94e156 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..216aeb4f7b87134586aec0399ed08475bfbda5c0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "memorypressurenotification", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libmemorypressurenotification.so": "file:./src/main/cpp/types/libmemorypressurenotification" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..2afdde30dc40630da0f5b7e43d51fd96a8cbe672 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(memorypressurenotification SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(memorypressurenotification PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45d1e997e58ac1d82aa62eceb0695e3028c5e4f9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/hello.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_memory_pressure_notification] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_memory_pressure_notification] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_memory_pressure_notification] + +// OH_JSVM_MemoryPressureNotification的样例方法 +static JSVM_Value MemoryPressureNotification(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 设置当前JSVM的内存压力级别 + JSVM_Status status = OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_MemoryPressureNotification: success"); + OH_LOG_INFO(LOG_APP, "JSVM Current JSVM memory pressure level: %{public}d", + JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL); + } + return nullptr; +} +// MemoryPressureNotification注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = MemoryPressureNotification}, +}; +static JSVM_CallbackStruct *method = param; +// MemoryPressureNotification方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"memoryPressureNotification", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; + +const char *SRC_CALL_NATIVE = R"JS(memoryPressureNotification())JS"; +// [End oh_jsvm_memory_pressure_notification] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "memorypressurenotification", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/types/libmemorypressurenotification/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/types/libmemorypressurenotification/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/types/libmemorypressurenotification/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/types/libmemorypressurenotification/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/types/libmemorypressurenotification/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..29adc05a8d7df821b50396c9d3b4833ed16c3531 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/cpp/types/libmemorypressurenotification/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libmemorypressurenotification.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/ets/memorypressurenotificationability/MemorypressurenotificationAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/ets/memorypressurenotificationability/MemorypressurenotificationAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..1f29e75384d40bff8172d334ff86d2e8a45e203d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/ets/memorypressurenotificationability/MemorypressurenotificationAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class MemorypressurenotificationAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..c672c52f29267a06f48889cf8c1dc68e11cb081c --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libmemorypressurenotification.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9537498798aa4e10317830287735f3780d7531ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "memorypressurenotification", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "MemorypressurenotificationAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "MemorypressurenotificationAbility", + "srcEntry": "./ets/memorypressurenotificationability/MemorypressurenotificationAbility.ets", + "description": "$string:MemorypressurenotificationAbility_desc", + "icon": "$media:layered_image", + "label": "$string:MemorypressurenotificationAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..49248b372c03bc598f1ec1228c8f381c6790268d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "MemorypressurenotificationAbility_desc", + "value": "description" + }, + { + "name": "MemorypressurenotificationAbility_label", + "value": "UsageInstructionsTwo" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..dd181e4cf106f37a74d371d2e41bf961e2f4577d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testMemoryPressureNotification', 0, async (done: Function) => { + console.log('uitest: TestMemoryPressureNotification begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'MemorypressurenotificationAbility' + } + 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('MemorypressurenotificationAbility'); + + 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: TestMemoryPressureNotification end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..3177bcc11ab23f125fd62da1c70bb8050948f097 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/memorypressurenotification/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "memorypressurenotification_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/ohosTest.md b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..0634982710fcbe38d8ce5fb8a42a55717f86beb7 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/ohosTest.md @@ -0,0 +1,13 @@ +# UsageInstructionsTwo 测试用例归档 + +## 用例表 + +| 测试功能 | 预置条件 | 输入 | 预期输出 | 是否自动 | 测试结果 | +| -------------------------------------------------- | ------------ | --------------- | ---------------------- | -------- | -------- | +| 拉起应用 | 设备正常运行 | | 成功拉起应用 | 是 | Pass | +| 使用JSVM-API接口获取JSVM API的版本号 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 设置JavaScript对象保持活动状态的外部分配内存的数量 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 通知虚拟机系统内存不足并有选择地触发垃圾回收 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 判断给定的两个JS value是否严格相等 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 检索给定环境的虚拟机实例 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | +| 返回一组虚拟机堆的统计数据 | 位于首页 | 点击Hello World | 1秒后页面显示“success” | 是 | Pass | diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/screenshots/UsageInstructionsTwo_1.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/screenshots/UsageInstructionsTwo_1.png new file mode 100644 index 0000000000000000000000000000000000000000..d7a3902880a1573911e1014ba0ffce179a5fa9ca Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/screenshots/UsageInstructionsTwo_1.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/screenshots/UsageInstructionsTwo_2.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/screenshots/UsageInstructionsTwo_2.png new file mode 100644 index 0000000000000000000000000000000000000000..e57cd30d1f6ca6ff3bcc25f38187a45489918606 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/screenshots/UsageInstructionsTwo_2.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/.gitignore b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/build-profile.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01bed8940ff6f2d5843f8d1d7e184725f59be699 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/build-profile.json5 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + } + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + }, + "nativeLib": { + "debugSymbol": { + "strip": true, + "exclude": [] + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/hvigorfile.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4f43d54667f8327c367c8096bd08bb8c75aff54 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/hvigorfile.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/obfuscation-rules.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e1a669d623dab33720acec9678158116a39630e0 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/oh-package.json5 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "strictequals", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": { + "libstrictequals.so": "file:./src/main/cpp/types/libstrictequals" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/CMakeLists.txt b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..9c4f5b36c35d4be09b078771bd0f8306af392954 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.4.1) +project(UsageInstructionsTwo) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +# 日志打印配置 +add_definitions( "-DLOG_DOMAIN=0xd0d0" ) +add_definitions( "-DLOG_TAG=\"testTag\"" ) +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/include) + +add_library(strictequals SHARED hello.cpp) +# 构建此可执行文件需要链接的库 +target_link_libraries(strictequals PUBLIC libace_napi.z.so libjsvm.so libhilog_ndk.z.so) diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/hello.cpp b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..422c3d54e807e7a6f3c2bfe0f964d610ceeeeb65 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/hello.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [Start oh_jsvm_strict_equals] +#include "napi/native_api.h" +#include "ark_runtime/jsvm.h" +#include "hilog/log.h" +// [StartExclude oh_jsvm_strict_equals] +#define LOG_DOMAIN 0x3200 +#define LOG_TAG "APP" + +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; \ + } \ + } 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; \ + } \ + } while (0) + +// 用于调用theCall并检查其返回值是否为JSVM_OK。 +// 如果不是,则调用OH_JSVM_GetLastErrorInfo处理错误并返回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 +#define JSVM_CALL(theCall) JSVM_CALL_BASE(env, theCall, nullptr) +// [EndExclude oh_jsvm_strict_equals] + +// OH_JSVM_StrictEquals的样例方法 +static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info) +{ + // 接受两个入参 + size_t argc = 2; + JSVM_Value args[2] = {nullptr}; + OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); + // 调用OH_JSVM_StrictEquals接口判断给定的两个JavaScript value是否严格相等 + bool result = false; + JSVM_Status status = OH_JSVM_StrictEquals(env, args[0], args[1], &result); + if (status != JSVM_OK) { + OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_StrictEquals: failed"); + } else { + OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_StrictEquals: success: %{public}d", result); + } + JSVM_Value isStrictEqual; + OH_JSVM_GetBoolean(env, result, &isStrictEqual); + return isStrictEqual; +} +// IsStrictEquals注册回调 +static JSVM_CallbackStruct param[] = { + {.data = nullptr, .callback = IsStrictEquals}, +}; +static JSVM_CallbackStruct *method = param; +// IsStrictEquals方法别名,供JS调用 +static JSVM_PropertyDescriptor descriptor[] = { + {"isStrictEquals", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, +}; +// 样例测试js +const char* SRC_CALL_NATIVE = R"JS(data = '123';value = '123';isStrictEquals(data,value);)JS"; +// [End oh_jsvm_strict_equals] + +static int32_t TestJSVM() +{ + JSVM_InitOptions initOptions = {0}; + JSVM_VM vm; + 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; +} + +// 模块注册信息,供arkts侧调用 +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = {{"runTest", nullptr, RunTest, nullptr, nullptr, nullptr, napi_default, nullptr}}; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "strictequals", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/types/libstrictequals/Index.d.ts b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/types/libstrictequals/Index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..87c2a3033bf359f3f7b2e40f1280487aadaf4576 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/types/libstrictequals/Index.d.ts @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const runTest: () => void; \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/types/libstrictequals/oh-package.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/types/libstrictequals/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..58028a5e325aff200103cf3c248971a1870345b9 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/cpp/types/libstrictequals/oh-package.json5 @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "libstrictequals.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/ets/pages/Index.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..caf23cac32db70cfe9fe5948a481d6241010562d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/ets/pages/Index.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import napitest from 'libstrictequals.so'; + +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + Row() { + Column() { + Text(this.message) + .fontSize(30) + .fontWeight(FontWeight.Bold) + .onClick(() => { + try { + napitest.runTest(); + this.message = 'success'; + } catch (error) { + console.error('An error occurred: ', error); + this.message = 'fail'; + } + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/ets/strictequalsability/StrictequalsAbility.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/ets/strictequalsability/StrictequalsAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..01c4665b17ffd0af07289fd89b8d924a9aae273d --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/ets/strictequalsability/StrictequalsAbility.ets @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class StrictequalsAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +}; diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e01d7becac53bbf6f2fbd7c2c3ecbdab087bcc3e --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/module.json5 @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "strictequals", + "type": "feature", + "description": "$string:module_desc", + "mainElement": "StrictequalsAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "StrictequalsAbility", + "srcEntry": "./ets/strictequalsability/StrictequalsAbility.ets", + "description": "$string:StrictequalsAbility_desc", + "icon": "$media:layered_image", + "label": "$string:StrictequalsAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true + } + ] + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/element/color.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/element/string.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..74779e5a2e8037d618e9c55005263eac4f8d73da --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "StrictequalsAbility_desc", + "value": "description" + }, + { + "name": "StrictequalsAbility_label", + "value": "UsageInstructionsTwo" + } + ] +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/background.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/background.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/foreground.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/foreground.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/layered_image.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/startIcon.png b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/media/startIcon.png differ diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/profile/main_pages.json b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..721a7da2c9fe3ee480de7a9a3d32c3b6878484a8 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * 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; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + /** + * 打开应用,点击Hello World执行 + */ + it('testStrictEquals', 0, async (done: Function) => { + console.log('uitest: TestStrictEquals begin'); + + const want: Want = { + bundleName: bundleName, + abilityName: 'StrictequalsAbility' + } + 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('StrictequalsAbility'); + + 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: TestStrictEquals end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/List.test.ets b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/module.json5 b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..409f7a46b0cd8ce6325b4420f055b5a5c76b408b --- /dev/null +++ b/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo/strictequals/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "strictequals_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +}