openharmony 鸿蒙 use-jsvm-about-well-known-symbols

2025-06-12 浏览 (1)

Working with Well-Known Symbols Using JSVM-API

Introduction

This topic walks you through on how to use JSVM-API to obtain 11 well-known symbols.

Basic Concepts

JSVM-API provides APIs for obtaining 11 well-known symbols.

Available APIs

APIDescription
OH_JSVM_GetSymbolToStringTagObtains the value equivalent to the JS Symbol.toStringTag.
OH_JSVM_GetSymbolToPrimitiveObtains the value equivalent to the JS Symbol.toPrimitive.
OH_JSVM_GetSymbolSplitObtains the value equivalent to the JS Symbol.split.
OH_JSVM_GetSymbolSearchObtains the value equivalent to the JS Symbol.search.
OH_JSVM_GetSymbolReplaceObtains the value equivalent to the JS Symbol.replace.
OH_JSVM_GetSymbolMatchObtains the value equivalent to the JS Symbol.match.
OH_JSVM_GetSymbolIsConcatSpreadableObtains the value equivalent to the JS Symbol.isConcatSpreadable.
OH_JSVM_GetSymbolHasInstanceObtains the value equivalent to the JS Symbol.hasInstance.
OH_JSVM_GetSymbolUnscopablesObtains the value equivalent to the JS Symbol.unscopables.
OH_JSVM_GetSymbolAsyncIteratorObtains the value equivalent to the JS Symbol.asyncIterator.
OH_JSVM_GetSymbolIteratorObtains the value equivalent to the JS Symbol.iterator.

Example

If you are just starting out with JSVM-API, see JSVM-API Development Process. The following demonstrates only the C++ code involved in obtaining the well-known symbols.

OH_JSVM_GetSymbolToStringTag

CPP code:

#include <string>

static JSVM_Value WellKnownSymbols(JSVM_Env env, JSVM_CallbackInfo info) {
    JSVM_VM vm;
    OH_JSVM_GetVM(env, &vm);

    JSVM_HandleScope handleScope;
    OH_JSVM_OpenHandleScope(env, &handleScope);
    std::string src = R"JS(Symbol.toStringTag)JS";
    JSVM_Value jsSrc;
    JSVM_Script script;
    JSVM_Value result1;

    OH_JSVM_CreateStringUtf8(env, src.c_str(), JSVM_AUTO_LENGTH, &jsSrc);
    OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script);
    OH_JSVM_RunScript(env, script, &result1);
    JSVM_Value result2;
    OH_JSVM_GetSymbolToStringTag(env, &result2);
    bool is_equals = false;
    OH_JSVM_StrictEquals(env, result1, result2, &is_equals);
    OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetSymbolToStringTag result is correct : %{public}d\n", is_equals);
    OH_JSVM_CloseHandleScope(env, handleScope);

    return nullptr;
}

static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = WellKnownSymbols},
};

static JSVM_CallbackStruct *method = param;

// Alias for the wellKnownSymbols method to be called from JS.
static JSVM_PropertyDescriptor descriptor[] = {
    {"wellKnownSymbols", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};

// Call the C++ code from JS.
const char *srcCallNative = R"JS(wellKnownSymbols();)JS";

Expected result:

JSVM OH_JSVM_GetSymbolToStringTag result is correct : 1

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Node-API

harmony 鸿蒙Building an NDK Project with CMake

harmony 鸿蒙Building an NDK Project with the DevEco Studio Template

harmony 鸿蒙NDK Project Building Overview

harmony 鸿蒙Building an NDK Project with Prebuilt Libraries

harmony 鸿蒙C/C++ Library Mechanisms

harmony 鸿蒙CPU Features

harmony 鸿蒙Creating an NDK Project

harmony 鸿蒙C/C++ Memory Error Detection

harmony 鸿蒙Debugging in DevEco Studio

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/hjpZ9V