1# ArkTS JSVM-API使用指导 2 3### 介绍 4 5使用JSVM-API实现跨语言交互,首先需要按照JSVM-API的机制实现模块的注册和加载等相关动作。 6 7- ArkTS/JS侧:实现C++方法的调用。代码比较简单,import一个对应的so库后,即可调用C++方法。 8- Native侧:.cpp文件,实现模块的注册。需要提供注册lib库的名称,并在注册回调方法中定义接口的映射关系,即Native方法及对应的JS/ArkTS接口名称等。 9 10该工程中展示的代码详细描述可查如下链接: 11 12- [使用JSVM-API接口获取JSVM API的版本号](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-version.md) 13- [使用JSVM-API接口进行JavaScript代码调试调优](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-heapstatistics-debugger-cpuprofiler-heapsnapshot.md) 14- [使用JSVM-API判断给定的两个JS value是否严格相等](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-strict-equals.md) 15- [使用JSVM-API进行内存管理](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-memory-management.md) 16 17### 效果预览 18 19| 首页 | 执行及结果即时反馈 | 20| :----------------------------------------------------------------------: | :----------------------------------------------------------------------: | 21| <img src="./screenshots/UsageInstructionsTwo_1.png" style="zoom:33%;" /> | <img src="./screenshots/UsageInstructionsTwo_2.png" style="zoom:33%;" /> | 22 23### 使用说明 24 251. 在主界面,可以点击Hello World,开始执行。 262. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 27 28### 工程目录 29 30``` 31adjustexternalmemory/src/ 32 ├── main 33 │ ├── cpp 34 │ │ ├── types 35 │ │ │ ├── libadjustexternalmemory 36 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 37 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 38 │ │ ├── CMakeLists.txt // 配置CMake打包参数 39 │ │ ├── hello.cpp // 实现Native侧的runTest接口 40 │ ├── ets 41 │ │ ├── adjustexternalmemoryability 42 │ │ ├── pages 43 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 44 │ ├── module.json5 45 │ └── resources 46 ├── ohosTest 47 │ ├── ets 48 │ │ ├── test 49 │ │ ├── Ability.test.ets // 自动化测试代码 50getheapstatistics/src/ 51 ├── main 52 │ ├── cpp 53 │ │ ├── types 54 │ │ │ ├── libgetheapstatistics 55 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 56 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 57 │ │ ├── CMakeLists.txt // 配置CMake打包参数 58 │ │ ├── hello.cpp // 实现Native侧的runTest接口 59 │ ├── ets 60 │ │ ├── getheapstatisticsability 61 │ │ ├── pages 62 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 63 │ ├── module.json5 64 │ └── resources 65 ├── ohosTest 66 │ ├── ets 67 │ │ ├── test 68 │ │ ├── Ability.test.ets // 自动化测试代码 69getversion/src/ 70 ├── main 71 │ ├── cpp 72 │ │ ├── types 73 │ │ │ ├── libgetversion 74 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 75 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 76 │ │ ├── CMakeLists.txt // 配置CMake打包参数 77 │ │ ├── hello.cpp // 实现Native侧的runTest接 78 │ ├── ets 79 │ │ ├── getversionability 80 │ │ ├── pages 81 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 82 │ ├── module.json5 83 │ └── resources 84 ├── ohosTest 85 │ ├── ets 86 │ │ ├── test 87 │ │ ├── Ability.test.ets // 自动化测试代码 88getvm/src/ 89 ├── main 90 │ ├── cpp 91 │ │ ├── types 92 │ │ │ ├── libentry 93 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 94 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 95 │ │ ├── CMakeLists.txt // 配置CMake打包参数 96 │ │ ├── hello.cpp // 实现Native侧的runTest接 97 │ ├── ets 98 │ │ ├── entryability 99 │ │ ├── entrybackupability 100 │ │ ├── pages 101 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 102 │ ├── module.json5 103 │ └── resources 104 ├── ohosTest 105 │ ├── ets 106 │ │ ├── test 107 │ │ ├── Ability.test.ets // 自动化测试代码 108memorypressurenotification/src/ 109 ├── main 110 │ ├── cpp 111 │ │ ├── types 112 │ │ │ ├── libmemorypressurenotification 113 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 114 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 115 │ │ ├── CMakeLists.txt // 配置CMake打包参数 116 │ │ ├── hello.cpp // 实现Native侧的runTest接 117 │ ├── ets 118 │ │ ├── memorypressurenotificationability 119 │ │ ├── pages 120 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 121 │ ├── module.json5 122 │ └── resources 123 ├── ohosTest 124 │ ├── ets 125 │ │ ├── test 126 │ │ ├── Ability.test.ets // 自动化测试代码 127strictequals/src/ 128 ├── main 129 │ ├── cpp 130 │ │ ├── types 131 │ │ │ ├── libstrictequals 132 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 133 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 134 │ │ ├── CMakeLists.txt // 配置CMake打包参数 135 │ │ ├── hello.cpp // 实现Native侧的runTest接 136 │ ├── ets 137 │ │ ├── strictequalsability 138 │ │ ├── pages 139 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 140 │ ├── module.json5 141 │ └── resources 142 ├── ohosTest 143 │ ├── ets 144 │ │ ├── test 145 │ │ ├── Ability.test.ets // 自动化测试代码 146``` 147 148### 相关权限 149 150不涉及。 151 152### 依赖 153 154不涉及。 155 156### 约束与限制 157 1581.本示例仅支持标准系统上运行, 支持设备:Phone。 159 1602.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 161 1623.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 163 164### 下载 165 166如需单独下载本工程,执行如下命令: 167 168``` 169git init 170git config core.sparsecheckout true 171echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/UsageInstructionsTwo > .git/info/sparse-checkout 172git remote add origin https://gitee.com/openharmony/applications_app_samples.git 173git pull origin master 174``` 175