harmony 鸿蒙Setting the private Property Using JSVM-API

  • 2025-06-12
  • 浏览 (3)

Setting the private Property Using JSVM-API

Introduction

This topic walks you through on how to use JSVM-API to set a private property for an object.

Basic Concepts

In JSVM-API, you can create private keys, use the keys to create and delete properties of objects, and persist private key symbols.

Available APIs

API Description
OH_JSVM_CreateDataReference Creates a data reference with a specified reference count in JSVM.
OH_JSVM_GetReferenceData Checks whether a specified reference is valid. If the reference is valid, the JavaScript data associated with the reference is returned; otherwise, result is set to NULL.
OH_JSVM_CreatePrivate Creates a JS private key object.
OH_JSVM_SetPrivate Sets a private property for a passed-in object.
OH_JSVM_GetPrivate Obtains the private property of an object based on the private key.
OH_JSVM_DeletePrivate Deletes the private property of an object based on the private key.

Example

If you are just starting out with JSVM-API, see JSVM-API Development Process. The following demonstrates only the C++ code involved in manipulating private properties.

Creating a private Key, adding a private Property, and Deleting the Property

CPP code:

static JSVM_Value privateTest(JSVM_Env env, JSVM_CallbackInfo info) {
    JSVM_VM vm;
    JSVM_HandleScope outerScope;
    OH_JSVM_GetVM(env, &vm);
    OH_JSVM_OpenHandleScope(env, &outerScope);

    JSVM_HandleScope handleScope;
    JSVM_Data privateKey;
    JSVM_Value object;
    JSVM_Value property;
    JSVM_Ref privateRef;
    {
        OH_JSVM_OpenHandleScope(env, &handleScope);
        OH_JSVM_CreateObject(env, &object);
        OH_JSVM_CreatePrivate(env, nullptr, &privateKey);
        OH_JSVM_CreateInt32(env, 1, &property);
        OH_JSVM_SetPrivate(env, object, privateKey, property);
        OH_JSVM_GetPrivate(env, object, privateKey, &property);
        int propertyValue = 0;
        OH_JSVM_GetValueInt32(env, property, &propertyValue);
        OH_LOG_INFO(LOG_APP, "private property set: %{public}d\n", propertyValue);
        OH_JSVM_DeletePrivate(env, object, privateKey);
        OH_JSVM_GetPrivate(env, object, privateKey, &property);
        bool isUndefined = false;
        OH_JSVM_IsUndefined(env, property, &isUndefined);
        OH_LOG_INFO(LOG_APP, "private property deleted is undefined: %{public}d\n", isUndefined);
        OH_JSVM_CreateDataReference(env, privateKey, 1, &privateRef);
        OH_JSVM_CloseHandleScope(env, handleScope);
    }
    {
        OH_JSVM_OpenHandleScope(env, &handleScope);
        OH_JSVM_GetReferenceData(env, privateRef, &privateKey);
        OH_JSVM_CreateObject(env, &object);
        OH_JSVM_CreateInt32(env, 2, &property);
        OH_JSVM_SetPrivate(env, object, privateKey, property);
        OH_JSVM_GetPrivate(env, object, privateKey, &property);
        int propertyValue = 0;
        OH_JSVM_GetValueInt32(env, property, &propertyValue);
        OH_LOG_INFO(LOG_APP, "second private property set: %{public}d\n", propertyValue);
        OH_JSVM_CloseHandleScope(env, handleScope);
    }

    OH_JSVM_CloseHandleScope(env, outerScope);
    return nullptr;
}

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

static JSVM_CallbackStruct *method = param;

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

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

Expected Result

private property set: 1
private property deleted is undefined: 1
second private property set: 2

你可能感兴趣的鸿蒙文章

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

0  赞