1e41f4b71Sopenharmony_ci# Obtaining the JSVM API Version Using JSVM-API
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThis topic walks you through on how to use JSVM-API to obtain the API version and VM information.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Available APIs
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci| API                      | Description                      |
10e41f4b71Sopenharmony_ci|----------------------------|--------------------------------|
11e41f4b71Sopenharmony_ci| OH_JSVM_GetVersion         | Obtains the latest JSVM API version supported by the JSVM runtime. |
12e41f4b71Sopenharmony_ci| OH_JSVM_GetVMInfo          | Obtains the VM information.             |
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Example
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciIf 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.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci### OH_JSVM_GetVersion
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciUse **OH_JSVM_GetVersion** to obtain the latest JSVM API version supported by the JSVM runtime.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciCPP code:
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci```cpp
25e41f4b71Sopenharmony_ci// hello.cpp
26e41f4b71Sopenharmony_ci#include "napi/native_api.h"
27e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h"
28e41f4b71Sopenharmony_ci#include <hilog/log.h>
29e41f4b71Sopenharmony_ci// Register the GetVersion callback.
30e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = {
31e41f4b71Sopenharmony_ci    {.data = nullptr, .callback = GetVersion},
32e41f4b71Sopenharmony_ci};
33e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param;
34e41f4b71Sopenharmony_ci// Set a property descriptor named getVersion and associate it with a callback. This allows the GetVersion callback to be called from JS.
35e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = {
36e41f4b71Sopenharmony_ci    {"getVersion", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
37e41f4b71Sopenharmony_ci};
38e41f4b71Sopenharmony_ci// Define OH_JSVM_GetVersion.
39e41f4b71Sopenharmony_cistatic JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info)
40e41f4b71Sopenharmony_ci{
41e41f4b71Sopenharmony_ci    uint32_t jsVersion = 0;
42e41f4b71Sopenharmony_ci    // Obtain the latest JSVM API version supported by the current JSVM runtime.
43e41f4b71Sopenharmony_ci    JSVM_Status status = OH_JSVM_GetVersion(env, &jsVersion);
44e41f4b71Sopenharmony_ci    JSVM_Value result = nullptr;
45e41f4b71Sopenharmony_ci    OH_JSVM_CreateUint32(env, jsVersion, &result);
46e41f4b71Sopenharmony_ci    int value = static_cast<int>(jsVersion);
47e41f4b71Sopenharmony_ci    if (status != JSVM_OK) {
48e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "JSVM GetVersion fail");
49e41f4b71Sopenharmony_ci    } else {
50e41f4b71Sopenharmony_ci        OH_LOG_INFO(LOG_APP, "JSVM GetVersion success:%{public}d", value);
51e41f4b71Sopenharmony_ci    }
52e41f4b71Sopenharmony_ci    return result;
53e41f4b71Sopenharmony_ci}
54e41f4b71Sopenharmony_ci```
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ciArkTS code:
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci```ts
59e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog"
60e41f4b71Sopenharmony_ci// Import the native APIs.
61e41f4b71Sopenharmony_ciimport napitest from "libentry.so"
62e41f4b71Sopenharmony_cilet script: string = `
63e41f4b71Sopenharmony_ci    getVersion()
64e41f4b71Sopenharmony_ci`;
65e41f4b71Sopenharmony_citry {
66e41f4b71Sopenharmony_ci  let result = napitest.runJsVm(script);
67e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testJSVM', 'Test JSVM getVersion: %{public}s', result);
68e41f4b71Sopenharmony_ci} catch (error) {
69e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testJSVM', 'Test JSVM getVersion error: %{public}s', error.message);
70e41f4b71Sopenharmony_ci}
71e41f4b71Sopenharmony_ci```
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci### OH_JSVM_GetVMInfo
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ciUse **OH_JSVM_GetVMInfo** to obtain VM information.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ciCPP code:
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci```cpp
80e41f4b71Sopenharmony_ci// hello.cpp
81e41f4b71Sopenharmony_ci#include "napi/native_api.h"
82e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h"
83e41f4b71Sopenharmony_ci#include <hilog/log.h>
84e41f4b71Sopenharmony_ci// Register the GetVMInfo callback.
85e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = {
86e41f4b71Sopenharmony_ci    {.data = nullptr, .callback = GetVMInfo},
87e41f4b71Sopenharmony_ci};
88e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param;
89e41f4b71Sopenharmony_ci// Set a property descriptor named GetVMInfo and associate it with a callback. This allows the GetVMInfo callback to be called from JS.
90e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = {
91e41f4b71Sopenharmony_ci    {"getVMInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
92e41f4b71Sopenharmony_ci};
93e41f4b71Sopenharmony_ci// Define OH_JSVM_GetVMInfo.
94e41f4b71Sopenharmony_ci// Print the JSVM information.
95e41f4b71Sopenharmony_civoid PrintVmInfo(JSVM_VMInfo vmInfo) {
96e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "JSVM API apiVersion: %{public}d", vmInfo.apiVersion);
97e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "JSVM API engine: %{public}s", vmInfo.engine);
98e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "JSVM API version: %{public}s", vmInfo.version);
99e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "JSVM API cachedDataVersionTag: 0x%{public}x", vmInfo.cachedDataVersionTag);
100e41f4b71Sopenharmony_ci}
101e41f4b71Sopenharmony_cistatic JSVM_Value GetVMInfo(JSVM_Env env, JSVM_CallbackInfo info)
102e41f4b71Sopenharmony_ci{
103e41f4b71Sopenharmony_ci    // Obtain the VM information.
104e41f4b71Sopenharmony_ci    JSVM_VMInfo result;
105e41f4b71Sopenharmony_ci    OH_JSVM_GetVMInfo(&result);
106e41f4b71Sopenharmony_ci    // Obtain the latest API version supported by the VM and print the information.
107e41f4b71Sopenharmony_ci    JSVM_Value version = nullptr;
108e41f4b71Sopenharmony_ci    OH_JSVM_CreateUint32(env, result.apiVersion, &version);
109e41f4b71Sopenharmony_ci    // Output VM information.
110e41f4b71Sopenharmony_ci    PrintVmInfo(result);
111e41f4b71Sopenharmony_ci    return version;
112e41f4b71Sopenharmony_ci}
113e41f4b71Sopenharmony_ci```
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ciArkTS code:
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci```ts
118e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog"
119e41f4b71Sopenharmony_ci// Import the native APIs.
120e41f4b71Sopenharmony_ciimport napitest from "libentry.so"
121e41f4b71Sopenharmony_cilet script: string = `
122e41f4b71Sopenharmony_ci    getVMInfo()
123e41f4b71Sopenharmony_ci`
124e41f4b71Sopenharmony_citry {
125e41f4b71Sopenharmony_ci  let result = napitest.runJsVm(script);
126e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testJSVM', 'Test JSVM getVMInfo apiVersion: %{public}s', result);
127e41f4b71Sopenharmony_ci} catch (error) {
128e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testJSVM', 'Test JSVM getVMInfo error: %{public}s', error.message);
129e41f4b71Sopenharmony_ci}
130e41f4b71Sopenharmony_ci```
131