1# Obtaining the JSVM API Version Using JSVM-API 2 3## Introduction 4 5This topic walks you through on how to use JSVM-API to obtain the API version and VM information. 6 7## Available APIs 8 9| API | Description | 10|----------------------------|--------------------------------| 11| OH_JSVM_GetVersion | Obtains the latest JSVM API version supported by the JSVM runtime. | 12| OH_JSVM_GetVMInfo | Obtains the VM information. | 13 14## Example 15 16If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ and ArkTS code related to version management. 17 18### OH_JSVM_GetVersion 19 20Use **OH_JSVM_GetVersion** to obtain the latest JSVM API version supported by the JSVM runtime. 21 22CPP code: 23 24```cpp 25// hello.cpp 26#include "napi/native_api.h" 27#include "ark_runtime/jsvm.h" 28#include <hilog/log.h> 29// Register the GetVersion callback. 30static JSVM_CallbackStruct param[] = { 31 {.data = nullptr, .callback = GetVersion}, 32}; 33static JSVM_CallbackStruct *method = param; 34// Set a property descriptor named getVersion and associate it with a callback. This allows the GetVersion callback to be called from JS. 35static JSVM_PropertyDescriptor descriptor[] = { 36 {"getVersion", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 37}; 38// Define OH_JSVM_GetVersion. 39static JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info) 40{ 41 uint32_t jsVersion = 0; 42 // Obtain the latest JSVM API version supported by the current JSVM runtime. 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 code: 57 58```ts 59import hilog from "@ohos.hilog" 60// Import the native APIs. 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 75Use **OH_JSVM_GetVMInfo** to obtain VM information. 76 77CPP code: 78 79```cpp 80// hello.cpp 81#include "napi/native_api.h" 82#include "ark_runtime/jsvm.h" 83#include <hilog/log.h> 84// Register the GetVMInfo callback. 85static JSVM_CallbackStruct param[] = { 86 {.data = nullptr, .callback = GetVMInfo}, 87}; 88static JSVM_CallbackStruct *method = param; 89// Set a property descriptor named GetVMInfo and associate it with a callback. This allows the GetVMInfo callback to be called from JS. 90static JSVM_PropertyDescriptor descriptor[] = { 91 {"getVMInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 92}; 93// Define OH_JSVM_GetVMInfo. 94// Print the JSVM information. 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 // Obtain the VM information. 104 JSVM_VMInfo result; 105 OH_JSVM_GetVMInfo(&result); 106 // Obtain the latest API version supported by the VM and print the information. 107 JSVM_Value version = nullptr; 108 OH_JSVM_CreateUint32(env, result.apiVersion, &version); 109 // Output VM information. 110 PrintVmInfo(result); 111 return version; 112} 113``` 114 115ArkTS code: 116 117```ts 118import hilog from "@ohos.hilog" 119// Import the native APIs. 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