1# 使用JSVM-API接口获取JSVM API的版本号 2 3## 简介 4 5用于获取当前版本信息。 6 7## 接口说明 8 9| 接口 | 功能说明 | 10|----------------------------|--------------------------------| 11| OH_JSVM_GetVersion | 返回JSVM运行时支持的最高JSVM API版本。 | 12| OH_JSVM_GetVMInfo | 返回虚拟机的信息。 | 13 14## 使用示例 15 16JSVM-API接口开发流程参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md),本文仅对接口对应C++及ArkTS相关代码进行展示。 17 18### OH_JSVM_GetVersion 19 20获取当前环境支持的JSVM API的最高版本号。 21 22cpp部分代码 23 24```cpp 25// hello.cpp 26#include "napi/native_api.h" 27#include "ark_runtime/jsvm.h" 28#include <hilog/log.h> 29// GetVersion注册回调 30static JSVM_CallbackStruct param[] = { 31 {.data = nullptr, .callback = GetVersion}, 32}; 33static JSVM_CallbackStruct *method = param; 34// GetVersion方法别名,供JS调用 35static JSVM_PropertyDescriptor descriptor[] = { 36 {"getVersion", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 37}; 38// OH_JSVM_GetVersion的样例方法 39static JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info) 40{ 41 uint32_t jsVersion = 0; 42 // 调用接口,获取当前JSVM运行时支持的最高JSVM API版本 43 JSVM_Status status = OH_JSVM_GetVersion(env, &jsVersion); 44 JSVM_Value result = nullptr; 45 OH_JSVM_CreateUint32(env, jsVersion, &result); 46 int value = static_cast<int>(jsVersion); 47 if (status != JSVM_OK) { 48 OH_LOG_ERROR(LOG_APP, "JSVM GetVersion fail"); 49 } else { 50 OH_LOG_INFO(LOG_APP, "JSVM GetVersion success:%{public}d", value); 51 } 52 return result; 53} 54``` 55 56ArkTS侧示例代码 57 58```ts 59import hilog from "@ohos.hilog" 60// 通过import的方式,引入Native能力。 61import napitest from "libentry.so" 62let script: string = ` 63 getVersion() 64`; 65try { 66 let result = napitest.runJsVm(script); 67 hilog.info(0x0000, 'testJSVM', 'Test JSVM getVersion: %{public}s', result); 68} catch (error) { 69 hilog.error(0x0000, 'testJSVM', 'Test JSVM getVersion error: %{public}s', error.message); 70} 71``` 72 73### OH_JSVM_GetVMInfo 74 75获取虚拟机的信息。 76 77cpp部分代码 78 79```cpp 80// hello.cpp 81#include "napi/native_api.h" 82#include "ark_runtime/jsvm.h" 83#include <hilog/log.h> 84// GetVMInfo注册回调 85static JSVM_CallbackStruct param[] = { 86 {.data = nullptr, .callback = GetVMInfo}, 87}; 88static JSVM_CallbackStruct *method = param; 89// GetVMInfo方法别名,供JS调用 90static JSVM_PropertyDescriptor descriptor[] = { 91 {"getVMInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 92}; 93// OH_JSVM_GetVMInfo的样例方法 94// 打印JSVM(JavaScript虚拟机)的各项信息 95void PrintVmInfo(JSVM_VMInfo vmInfo) { 96 OH_LOG_INFO(LOG_APP, "JSVM API apiVersion: %{public}d", vmInfo.apiVersion); 97 OH_LOG_INFO(LOG_APP, "JSVM API engine: %{public}s", vmInfo.engine); 98 OH_LOG_INFO(LOG_APP, "JSVM API version: %{public}s", vmInfo.version); 99 OH_LOG_INFO(LOG_APP, "JSVM API cachedDataVersionTag: 0x%{public}x", vmInfo.cachedDataVersionTag); 100} 101static JSVM_Value GetVMInfo(JSVM_Env env, JSVM_CallbackInfo info) 102{ 103 // 调用接口,获取虚拟机的信息 104 JSVM_VMInfo result; 105 OH_JSVM_GetVMInfo(&result); 106 // 取出“此虚拟机支持的最高API版本”信息带出去打印 107 JSVM_Value version = nullptr; 108 OH_JSVM_CreateUint32(env, result.apiVersion, &version); 109 // 输出VM虚拟机信息 110 PrintVmInfo(result); 111 return version; 112} 113``` 114 115ArkTS侧示例代码 116 117```ts 118import hilog from "@ohos.hilog" 119// 通过import的方式,引入Native能力。 120import napitest from "libentry.so" 121let script: string = ` 122 getVMInfo() 123` 124try { 125 let result = napitest.runJsVm(script); 126 hilog.info(0x0000, 'testJSVM', 'Test JSVM getVMInfo apiVersion: %{public}s', result); 127} catch (error) { 128 hilog.error(0x0000, 'testJSVM', 'Test JSVM getVMInfo error: %{public}s', error.message); 129} 130``` 131