• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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调试&定位](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/jsvm-debugger-cpuprofiler-heapsnapshot.md)
13- [使用 code cache 加速编译](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-about-code-cache.md)
14- [使用JSVM-API接口创建多个引擎执行JS代码并销毁](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-runtime-task.md)
15
16### 效果预览
17
18|                             首页                              |                      执行及结果即时反馈                       |
19| :-----------------------------------------------------------: | :-----------------------------------------------------------: |
20| <img src="./screenshots/JsvmDebug_1.png" style="zoom:33%;" /> | <img src="./screenshots/JsvmDebug_2.png" style="zoom:33%;" /> |
21
22### 使用说明
23
241. 在主界面,可以点击Hello World,开始执行。
252. 执行结果会即时反馈在屏幕中央,并在控制台打印log。
26
27### 工程目录
28
29```
30aboutcodecache/src/
31 ├── main
32 │   ├── cpp
33 │   │   ├── types
34 │   │   │   ├── libaboutcodecache
35 │   │   │   │   ├── Index.d.ts          // 提供JS侧的接口方法
36 │   │   │   │   ├── oh-package.json5 	 // 将index.d.ts与cpp文件关联
37 │   │   ├── CMakeLists.txt              // 配置CMake打包参数
38 │   │   ├── hello.cpp                   // 实现Native侧的runTest接口
39 │   ├── ets
40 │   │   ├── aboutcodecacheability
41 │   │   ├── pages
42 │   │       ├── Index.ets               // ArkTS侧调用C/C++方法实现
43 │   ├── module.json5
44 │   └── resources
45 ├── ohosTest
46 │   ├── ets
47 │   │   ├── test
48 │   │       ├── Ability.test.ets        // 自动化测试代码
49openinspector/src/
50 ├── main
51 │   ├── cpp
52 │   │   ├── types
53 │   │   │   ├── libentry
54 │   │   │   │   ├── Index.d.ts          // 提供JS侧的接口方法
55 │   │   │   │   ├── oh-package.json5 	 // 将index.d.ts与cpp文件关联
56 │   │   ├── CMakeLists.txt              // 配置CMake打包参数
57 │   │   ├── hello.cpp                   // 实现Native侧的runTest接口
58 │   ├── ets
59 │   │   ├── entryability
60 │   │   ├── entrybackupability
61 │   │   ├── pages
62 │   │       ├── Index.ets               // ArkTS侧调用C/C++方法实现
63 │   ├── module.json5
64 │   └── resources
65 ├── ohosTest
66 │   ├── ets
67 │   │   ├── test
68 │   │       ├── Ability.test.ets        // 自动化测试代码
69snapshot/src/
70 ├── main
71 │   ├── cpp
72 │   │   ├── types
73 │   │   │   ├── libsnapshot
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 │   │   ├── snapshotability
80 │   │   ├── pages
81 │   │       ├── Index.ets               // ArkTS侧调用C/C++方法实现
82 │   ├── module.json5
83 │   └── resources
84 ├── ohosTest
85 │   ├── ets
86 │   │   ├── test
87 │   │       ├── Ability.test.ets        // 自动化测试代码
88runtimetask/src/
89 ├── main
90 │   ├── cpp
91 │   │   ├── types
92 │   │   │   ├── libruntimetask
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 │   │   ├── runtimetaskability
99 │   │   ├── pages
100 │   │       ├── Index.ets               // ArkTS侧调用C/C++方法实现
101 │   ├── module.json5
102 │   └── resources
103 ├── ohosTest
104 │   ├── ets
105 │   │   ├── test
106 │   │       ├── Ability.test.ets        // 自动化测试代码
107```
108
109### 相关权限
110
111[ohos.permission.INTERNET](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissioninternet)
112
113### 依赖
114
115不涉及。
116
117### 约束与限制
118
1191.本示例仅支持标准系统上运行, 支持设备:Phone。
120
1212.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。
122
1233.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。
124
125### 下载
126
127如需单独下载本工程,执行如下命令:
128
129```
130git init
131git config core.sparsecheckout true
132echo code/DocsSample/ArkTS/JSVMAPI/JsvmDebug > .git/info/sparse-checkout
133git remote add origin https://gitee.com/openharmony/applications_app_samples.git
134git pull origin master
135```
136