• 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
30static JSVM_Value WrapperObject(JSVM_Env env, JSVM_CallbackInfo info) {
31    JSVM_VM vm;
32    OH_JSVM_GetVM(env, &vm);
33
34    JSVM_HandleScope handleScope;
35    OH_JSVM_OpenHandleScope(env, &handleScope);
36    string src = R"JS(new Number(42))JS";
37    JSVM_Value jsSrc;
38    JSVM_Script script;
39    JSVM_Value result;
40
41    OH_JSVM_CreateStringUtf8(env, src.c_str(), JSVM_AUTO_LENGTH, &jsSrc);
42    OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script);
43    OH_JSVM_RunScript(env, script, &result);
44    bool isNumberObject = false;
45    OH_JSVM_IsNumberObject(env, result, &isNumberObject);
46    OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_IsNumberObject: %{public}d\n", isNumberObject);
47    OH_JSVM_CloseHandleScope(env, handleScope);
48
49    return nullptr;
50}
51
52static JSVM_CallbackStruct param[] = {
53    {.data = nullptr, .callback = WrapperObject},
54};
55
56static JSVM_CallbackStruct *method = param;
57
58// Alias for the wrapperObject method to be called from JS.
59static JSVM_PropertyDescriptor descriptor[] = {
60    {"wrapperObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
61};
62
63// Call the C++ code from JS.
64const char *srcCallNative = R"JS(wrapperObject();)JS";
65
66```
67
68Expected result:
69```
70JSVM OH_JSVM_IsNumberObject: 1
71```
72