1# ArkTs使用JSVM-API接口进行生命周期相关开发 2 3### 介绍 4 5在JSVM-API中,JSVM_Value是一个表示JavaScript值的抽象类型,它可以表示任何JavaScript值,包括基本类型(如数字、字符串、布尔值)和对象类型(如数组、函数、对象等)。 JSVM_Value的生命周期与其在JavaScript中的对应值的生命周期紧密相关。当JavaScript值被垃圾回收时,与之关联的JSVM_Value也将不再有效。重要的是不要在JavaScript值不再存在时尝试使用JSVM_Value。 6 7框架层的scope通常用于管理JSVM_Value的生命周期。在JSVM-API中,可以使用OH_JSVM_OpenHandleScope和OH_JSVM_CloseHandleScope函数来创建和销毁scope。通过在scope内创建JSVM_Value,可以确保在scope结束时自动释放JSVM_Value,避免内存泄漏。 8 9JSVM_Ref是一个JSVM-API类型,用于管理JSVM_Value的生命周期。JSVM_Ref允许您在JSVM_Value的生命周期内保持对其的引用,即使它已经超出了其原始上下文的范围。这使得您可以在不同的上下文中共享JSVM_Value,并确保在不再需要时正确释放其内存。 10 11合理使用OH_JSVM_OpenHandleScope和OH_JSVM_CloseHandleScope管理JSVM_Value的生命周期,做到生命周期最小化,避免发生内存泄漏问题。 12 13每个JSVM_Value属于特定的HandleScope,HandleScope通过OH_JSVM_OpenHandleScope和OH_JSVM_CloseHandleScope来建立和关闭,HandleScope关闭后,所属的JSVM_Value就会自动释放。 14 15该工程中展示的代码详细描述可查如下链接: 16 17- [使用JSVM-API接口进行生命周期相关开发](https://docs.openharmony.cn/pages/v5.0/zh-cn/application-dev/napi/use-jsvm-life-cycle.md) 18 19### 效果预览 20 21| 首页 | 执行及结果即时反馈 | 22| :---------------------------------------------------------------: | :---------------------------------------------------------------: | 23| <img src="./screenshots/JsvmLifeCycle_1.png" style="zoom:33%;" /> | <img src="./screenshots/JsvmLifeCycle_2.png" style="zoom:33%;" /> | 24 25### 使用说明 26 271. 在主界面,可以点击Hello World,开始执行。 282. 执行结果会即时反馈在屏幕中央,并在控制台打印log。 29 30### 工程目录 31 32``` 33addfinalizer/src/ 34 ├── main 35 │ ├── cpp 36 │ │ ├── types 37 │ │ │ ├── libaddfinalizer 38 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 39 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 40 │ │ ├── CMakeLists.txt // 配置CMake打包参数 41 │ │ ├── hello.cpp // 实现Native侧的runTest接口 42 │ ├── ets 43 │ │ ├── addfinalizerability 44 │ │ ├── pages 45 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 46 │ ├── module.json5 47 │ └── resources 48 ├── ohosTest 49 │ ├── ets 50 │ │ ├── test 51 │ │ ├── Ability.test.ets // 自动化测试代码 52openescapablehandlescope/src/ 53 ├── main 54 │ ├── cpp 55 │ │ ├── types 56 │ │ │ ├── libopenescapablehandlescope 57 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 58 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关联 59 │ │ ├── CMakeLists.txt // 配置CMake打包参数 60 │ │ ├── hello.cpp // 实现Native侧的runTest接口 61 │ ├── ets 62 │ │ ├── openescapablehandlescopeability 63 │ │ ├── pages 64 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 65 │ ├── module.json5 66 │ └── resources 67 ├── ohosTest 68 │ ├── ets 69 │ │ ├── test 70 │ │ ├── Ability.test.ets // 自动化测试代码 71openhandlescope/src/ 72 ├── main 73 │ ├── cpp 74 │ │ ├── types 75 │ │ │ ├── libentry 76 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 77 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 78 │ │ ├── CMakeLists.txt // 配置CMake打包参数 79 │ │ ├── hello.cpp // 实现Native侧的runTest接 80 │ ├── ets 81 │ │ ├── entryability 82 │ │ ├── entrybackupability 83 │ │ ├── pages 84 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 85 │ ├── module.json5 86 │ └── resources 87 ├── ohosTest 88 │ ├── ets 89 │ │ ├── test 90 │ │ ├── Ability.test.ets // 自动化测试代码 91referenceref/src/ 92 ├── main 93 │ ├── cpp 94 │ │ ├── types 95 │ │ │ ├── libreferenceref 96 │ │ │ │ ├── Index.d.ts // 提供JS侧的接口方法 97 │ │ │ │ ├── oh-package.json5 // 将index.d.ts与cpp文件关 98 │ │ ├── CMakeLists.txt // 配置CMake打包参数 99 │ │ ├── hello.cpp // 实现Native侧的runTest接 100 │ ├── ets 101 │ │ ├── referencerefability 102 │ │ ├── pages 103 │ │ ├── Index.ets // ArkTS侧调用C/C++方法实现 104 │ ├── module.json5 105 │ └── resources 106 ├── ohosTest 107 │ ├── ets 108 │ │ ├── test 109 │ │ ├── Ability.test.ets // 自动化测试代码 110``` 111 112### 相关权限 113 114不涉及。 115 116### 依赖 117 118不涉及。 119 120### 约束与限制 121 1221.本示例仅支持标准系统上运行, 支持设备:Phone。 123 1242.本示例为Stage模型,支持API15版本SDK,版本号:5.0.3.135,镜像版本号:HarmonyOS NEXT_5.0.3.135。 125 1263.本示例需要使用DevEco Studio 5.0.3 Release (Build Version: 5.0.9.300, built on March 13, 2025)及以上版本才可编译运行。 127 128### 下载 129 130如需单独下载本工程,执行如下命令: 131 132``` 133git init 134git config core.sparsecheckout true 135echo code/DocsSample/ArkTS/JSVMAPI/JsvmUsageGuide/JsvmLifeCycle > .git/info/sparse-checkout 136git remote add origin https://gitee.com/openharmony/applications_app_samples.git 137git pull origin master 138``` 139