• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Working with Well-Known Symbols Using JSVM-API
2
3## Introduction
4
5This topic walks you through on how to use JSVM-API to obtain 11 well-known symbols.
6
7## Basic Concepts
8
9JSVM-API provides APIs for obtaining 11 well-known symbols.
10
11## Available APIs
12
13| API                                   | Description                      |
14|----------------------------------------|--------------------------------|
15| OH_JSVM_GetSymbolToStringTag           | Obtains the value equivalent to the JS **Symbol.toStringTag**. |
16| OH_JSVM_GetSymbolToPrimitive           | Obtains the value equivalent to the JS **Symbol.toPrimitive**. |
17| OH_JSVM_GetSymbolSplit                 | Obtains the value equivalent to the JS **Symbol.split**.  |
18| OH_JSVM_GetSymbolSearch                | Obtains the value equivalent to the JS **Symbol.search**.  |
19| OH_JSVM_GetSymbolReplace               | Obtains the value equivalent to the JS **Symbol.replace**.  |
20| OH_JSVM_GetSymbolMatch                 | Obtains the value equivalent to the JS **Symbol.match**.  |
21| OH_JSVM_GetSymbolIsConcatSpreadable    | Obtains the value equivalent to the JS **Symbol.isConcatSpreadable**.  |
22| OH_JSVM_GetSymbolHasInstance           | Obtains the value equivalent to the JS **Symbol.hasInstance**.  |
23| OH_JSVM_GetSymbolUnscopables           | Obtains the value equivalent to the JS **Symbol.unscopables**.  |
24| OH_JSVM_GetSymbolAsyncIterator         | Obtains the value equivalent to the JS **Symbol.asyncIterator**.  |
25| OH_JSVM_GetSymbolIterator              | Obtains the value equivalent to the JS **Symbol.iterator**.  |
26
27## Example
28
29If 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 obtaining the well-known symbols.
30
31### OH_JSVM_GetSymbolToStringTag
32
33CPP code:
34
35```cpp
36#include <string>
37
38static JSVM_Value WellKnownSymbols(JSVM_Env env, JSVM_CallbackInfo info) {
39    JSVM_VM vm;
40    OH_JSVM_GetVM(env, &vm);
41
42    JSVM_HandleScope handleScope;
43    OH_JSVM_OpenHandleScope(env, &handleScope);
44    std::string src = R"JS(Symbol.toStringTag)JS";
45    JSVM_Value jsSrc;
46    JSVM_Script script;
47    JSVM_Value result1;
48
49    OH_JSVM_CreateStringUtf8(env, src.c_str(), JSVM_AUTO_LENGTH, &jsSrc);
50    OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script);
51    OH_JSVM_RunScript(env, script, &result1);
52    JSVM_Value result2;
53    OH_JSVM_GetSymbolToStringTag(env, &result2);
54    bool is_equals = false;
55    OH_JSVM_StrictEquals(env, result1, result2, &is_equals);
56    OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetSymbolToStringTag result is correct : %{public}d\n", is_equals);
57    OH_JSVM_CloseHandleScope(env, handleScope);
58
59    return nullptr;
60}
61
62static JSVM_CallbackStruct param[] = {
63    {.data = nullptr, .callback = WellKnownSymbols},
64};
65
66static JSVM_CallbackStruct *method = param;
67
68// Alias for the wellKnownSymbols method to be called from JS.
69static JSVM_PropertyDescriptor descriptor[] = {
70    {"wellKnownSymbols", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
71};
72
73// Call the C++ code from JS.
74const char *srcCallNative = R"JS(wellKnownSymbols();)JS";
75
76```
77
78Expected result:
79```
80JSVM OH_JSVM_GetSymbolToStringTag result is correct : 1
81```
82