• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Working with Wrapper Objects Using JSVM-API
2
3## Introduction
4
5This topic walks you through on how to use JSVM-API to quickly determine the wrapper object type.
6
7## Basic Concepts
8
9JSVM-API provides APIs for determining five wrapper object types: number, Boolean, BigInt, string, and symbol.
10
11## Available APIs
12
13| API                                   | Description                      |
14|----------------------------------------|--------------------------------|
15| OH_JSVM_IsNumberObject            | Checks whether the result is a number object. |
16| OH_JSVM_IsBooleanObject           | Checks whether the result is a Boolean object. |
17| OH_JSVM_IsBigIntObject            | Checks whether the result is a BigInt object.  |
18| OH_JSVM_IsStringObject            | Checks whether the result is a string object.  |
19| OH_JSVM_IsSymbolObject            | Checks whether the result is a symbol object.  |
20
21## Example
22
23If 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 manipulating wrapper objects.
24
25### Determining a Number Object
26
27CPP code:
28
29```cpp
30#include <string>
31
32static JSVM_Value WrapperObject(JSVM_Env env, JSVM_CallbackInfo info) {
33    JSVM_VM vm;
34    OH_JSVM_GetVM(env, &vm);
35
36    JSVM_HandleScope handleScope;
37    OH_JSVM_OpenHandleScope(env, &handleScope);
38    std::string src = R"JS(new Number(42))JS";
39    JSVM_Value jsSrc;
40    JSVM_Script script;
41    JSVM_Value result;
42
43    OH_JSVM_CreateStringUtf8(env, src.c_str(), JSVM_AUTO_LENGTH, &jsSrc);
44    OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script);
45    OH_JSVM_RunScript(env, script, &result);
46    bool isNumberObject = false;
47    OH_JSVM_IsNumberObject(env, result, &isNumberObject);
48    OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_IsNumberObject: %{public}d\n", isNumberObject);
49    OH_JSVM_CloseHandleScope(env, handleScope);
50
51    return nullptr;
52}
53
54static JSVM_CallbackStruct param[] = {
55    {.data = nullptr, .callback = WrapperObject},
56};
57
58static JSVM_CallbackStruct *method = param;
59
60// Alias for the wrapperObject method to be called from JS.
61static JSVM_PropertyDescriptor descriptor[] = {
62    {"wrapperObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
63};
64
65// Call the C++ code from JS.
66const char *srcCallNative = R"JS(wrapperObject();)JS";
67
68```
69
70Expected result:
71```
72JSVM OH_JSVM_IsNumberObject: 1
73```
74