• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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++ code involved in using version-related APIs.
17
18### OH_JSVM_GetVersion && OH_JSVM_GetVMInfo
19
20Use **OH_JSVM_GetVersion && OH_JSVM_GetVMInfo** to obtain the latest JSVM API version supported by the current environment and the VM information.
21
22CPP code:
23
24```cpp
25// hello.cpp
26#include <string.h>
27
28// Define OH_JSVM_GetVersion.
29static JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info)
30{
31    uint32_t jsVersion = 0;
32    // Obtain the latest JSVM API version supported by the current JSVM runtime.
33    JSVM_CALL(OH_JSVM_GetVersion(env, &jsVersion));
34    int value = static_cast<int>(jsVersion);
35    OH_LOG_INFO(LOG_APP, "JSVM GetVersion success:%{public}d", value);
36    return nullptr;
37}
38
39// Define OH_JSVM_GetVMInfo.
40// Print the JSVM information.
41void PrintVmInfo(JSVM_VMInfo vmInfo) {
42    OH_LOG_INFO(LOG_APP, "JSVM API apiVersion: %{public}d", vmInfo.apiVersion);
43    OH_LOG_INFO(LOG_APP, "JSVM API engine: %{public}s", vmInfo.engine);
44    OH_LOG_INFO(LOG_APP, "JSVM API version: %{public}s", vmInfo.version);
45    OH_LOG_INFO(LOG_APP, "JSVM API cachedDataVersionTag: 0x%{public}x", vmInfo.cachedDataVersionTag);
46}
47
48static JSVM_Value GetVMInfo(JSVM_Env env, JSVM_CallbackInfo info)
49{
50    // Obtain the VM information.
51    JSVM_VMInfo result;
52    JSVM_CALL(OH_JSVM_GetVMInfo(&result));
53    // Output VM information.
54    PrintVmInfo(result);
55    return nullptr;
56}
57
58// JS code to be executed.
59static const char *srcCallNative = R"JS(getVersion();getVMInfo();)JS";
60
61// Register the GetVersion and GetVMInfo callbacks.
62static JSVM_CallbackStruct param[] = {
63    {.data = nullptr, .callback = GetVersion},
64    {.data = nullptr, .callback = GetVMInfo},
65};
66static JSVM_CallbackStruct *method = param;
67// Aliases of the GetVersion and GetVMInfo methods, which can be called from JS.
68static JSVM_PropertyDescriptor descriptor[] = {
69    {"getVersion", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
70    {"getVMInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
71};
72```
73