1e41f4b71Sopenharmony_ci# JSVM-API简介
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## 场景介绍
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciOpenHarmony JSVM-API是基于标准JS引擎提供的一套稳定的ABI,为开发者提供了较为完整的JS引擎能力,包括创建和销毁引擎,执行JS代码,JS/C++交互等关键能力。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciOpenHarmony JSVM-API是C语言接口,遵循C99标准。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci通过JSVM-API,开发者可以在应用运行期间直接执行一段动态加载的JS代码。也可以选择将一些对性能、底层系统调用有较高要求的核心功能用C/C++实现并将C++方法注册到JS侧,在JS代码中直接调用,提高应用的执行效率。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci本文中如无特别说明,后续均使用JSVM-API指代OpenHarmony JSVM-API能力。
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## JSVM-API的组成架构
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci**图1** JSVM-API的组成架构
17e41f4b71Sopenharmony_ci  
18e41f4b71Sopenharmony_ci![jsvm-api_mechanism](figures/jsvm-api_mechanism.png)
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci- Native Module:开发者使用JSVM-API开发的模块,用于在Native侧使用。
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- VM Life Cycle Manager:管理JSVM_VM的生命周期。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- JS Context Manager:管理JSVM_Env的生命周期。
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci- Context Snapshot:上下文快照,可用以缩短JS Context的创建时间。
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- JS Code Execute:执行JS代码。
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci- JS/C++ Interaction:连接JS层与C++层,用于支撑JS与C++之间的交互。
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci- Code Cache:编译后的JS代码的缓存,能提升JS代码执行的启动速度。
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci- Debugger:调试器,用于调试JS代码。
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci- CPU Profiler:该工具能记录JS代码执行所用的时间,使用此工具能帮助开发者分析JS代码的性能瓶颈,为代码优化提供数据支撑。
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci- Heap Snapshot:JS堆内存分析/调优工具,可以进行内存优化和发现内存泄漏问题。
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci- Heap Statistics:JS堆统计信息,包括内存大小及上下文数量。
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci- Memory Adjustment:调整外部内存大小、虚拟机内存压力,以加快触发GC。
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci- VM Information:JSVM_VM的信息。
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci- Standard JS Engine:标准JS引擎。
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci## JSVM-API的关键交互流程
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**图2** JSVM-API的关键交互流程
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci![JSVM-API 关键交互流程](figures/process_jsvm-api.png)
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ciJSVM-API和Native模块之间的交互流程,主要分为以下两步:
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci1. **初始化阶段**:在Native模块上初始化JSVM和JS上下文,并完成Native函数的注册。Native方法将会被挂载到JS执行环境的全局上下文即GlobalThis。
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci2. **调用阶段**:当JS侧调用通过JSVM-API注册到JS全局上下文的方法时,JS引擎会找到并调用对应的C/C++方法。
59