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