目录

Working with Primitives Using JSVM-API

Introduction

JSVM-API provides APIs for converting data between C/C++ and JavaScript (JS) data types and obtaining the JS object of the specified type.

Basic Concepts

Before using JSVM to operate JS objects, you need to understand the following basic concepts:

Available APIs

API Description
OH_JSVM_CoerceToBool Converts a JS value to an object of the Boolean type.
OH_JSVM_CoerceToNumber Converts a JS value to an object of the number type.
OH_JSVM_CoerceToObject Converts a JS value to an object of the object type.
OH_JSVM_CoerceToString Converts a JS value to an object of the string type.
OH_JSVM_GetBoolean Obtains a JS singleton object that is used to represent the given Boolean value.
OH_JSVM_GetValueBool Obtains the C Boolean primitive equivalent of the given JS Boolean.
OH_JSVM_GetGlobal Obtains the global object of the current environment.
OH_JSVM_GetNull Obtains the JS null object.
OH_JSVM_GetUndefined Obtains the JS undefined object.

Example

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

[h2]OH_JSVM_CoerceToBool

Call OH_JSVM_CoerceToBool to forcibly convert a JS value to a JS Boolean value.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_CoerceToBool
static JSVM_Value CoerceToBool(JSVM_Env env, JSVM_CallbackInfo info)
{
    size_t argc = 1;
    JSVM_Value args[1] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
    JSVM_Value boolean = nullptr;
    JSVM_Status status = OH_JSVM_CoerceToBool(env, args[0], &boolean);
    if (status != JSVM_OK) {
        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToBool failed");
    } else {
        bool result = false;
        OH_JSVM_GetValueBool(env, boolean, &result);
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToBool success:%{public}d", result);
    }
    return boolean;
}
// Register callback for CoerceToBool
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = CoerceToBool},
};
static JSVM_CallbackStruct *method = param;
// Alias for the CoerceToBool method, called from the ArkTS side
static JSVM_PropertyDescriptor descriptor[] = {
    {"coerceToBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(coerceToBool("123"))JS";

Expected result:

JSVM OH_JSVM_CoerceToBool success:1

[h2]OH_JSVM_CoerceToNumber

Call OH_JSVM_CoerceToNumber to forcibly convert a JS value to a JS number.

CPP code:

#include "napi/native_api.h"
#include "hilog/log.h"
#include "ark_runtime/jsvm.h"
// ...

// Sample method for OH_JSVM_CoerceToNumber
static JSVM_Value CoerceToNumber(JSVM_Env env, JSVM_CallbackInfo info)
{
    size_t argc = 1;
    JSVM_Value args[1] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
    JSVM_Value number = nullptr;
    JSVM_Status status = OH_JSVM_CoerceToNumber(env, args[0], &number);
    if (status != JSVM_OK) {
        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToNumber failed");
    } else {
        int32_t result = 0;
        OH_JSVM_GetValueInt32(env, number, &result);
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToNumber success:%{public}d", result);
    }
    return number;
}
// Register callback for CoerceToNumber
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = CoerceToNumber},
};
static JSVM_CallbackStruct *method = param;
// Alias for the CoerceToNumber method, called from the ArkTS side
static JSVM_PropertyDescriptor descriptor[] = {
    {"coerceToNumber", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(coerceToNumber(true))JS";

Expected result:

JSVM OH_JSVM_CoerceToNumber success:1

[h2]OH_JSVM_CoerceToObject

Call OH_JSVM_CoerceToObject to forcibly convert a JS value to a JS object.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_CoerceToObject
static JSVM_Value CoerceToObject(JSVM_Env env, JSVM_CallbackInfo info)
{
    size_t argc = 1;
    JSVM_Value args[1] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
    JSVM_Value obj = nullptr;
    JSVM_Status status = OH_JSVM_CoerceToObject(env, args[0], &obj);
    if (status != JSVM_OK) {
        OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToObject failed");
        return nullptr;
    } else {
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToObject success");
    }
    return obj;
}
// Register callback for CoerceToObject
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = CoerceToObject},
};
static JSVM_CallbackStruct *method = param;
// Alias for the CoerceToObject method, called from the ArkTS side
static JSVM_PropertyDescriptor descriptor[] = {
    {"coerceToObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(coerceToObject(123))JS";

Expected result:

JSVM OH_JSVM_CoerceToObject success

[h2]OH_JSVM_CoerceToString

Call OH_JSVM_CoerceToString to forcibly convert a JS value to a JS string.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_CoerceToString
static JSVM_Value CoerceToString(JSVM_Env env, JSVM_CallbackInfo info)
{
    size_t argc = 1;
    JSVM_Value args[1] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
    JSVM_Value str = nullptr;
    JSVM_Status status = OH_JSVM_CoerceToString(env, args[0], &str);
    if (status != JSVM_OK) {
        OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToString failed");
        return nullptr;
    } else {
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToString success");
    }
    return str;
}
// Register callback for CoerceToString
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = CoerceToString},
};
static JSVM_CallbackStruct *method = param;
// Alias for the CoerceToString method, called from the ArkTS side
static JSVM_PropertyDescriptor descriptor[] = {
    {"coerceToString", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(coerceToString(22222))JS";

Expected result:

JSVM OH_JSVM_CoerceToString success

[h2]OH_JSVM_GetBoolean

Call OH_JSVM_GetBoolean to obtain a JS singleton object that is used to represent the given Boolean value.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_GetBoolean
static JSVM_Value GetBoolean(JSVM_Env env, JSVM_CallbackInfo info)
{
    // Pass in two arguments and parse them
    size_t argc = 2;
    JSVM_Value argv[2] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
    int32_t paramData = 0;
    OH_JSVM_GetValueInt32(env, argv[0], &paramData);
    int32_t paramValue = 0;
    OH_JSVM_GetValueInt32(env, argv[1], &paramValue);
    JSVM_Value returnValue = nullptr;
    bool type = false;
    if (paramData == paramValue) {
        OH_LOG_INFO(LOG_APP, "JSVM resultType equal");
        type = true;
    }
    JSVM_Status status = OH_JSVM_GetBoolean(env, type, &returnValue);
    if (status != JSVM_OK) {
        OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetBoolean failed");
    } else {
        bool result = false;
        OH_JSVM_GetValueBool(env, returnValue, &result);
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetBoolean success:%{public}d", result);
    }
    // Return the result
    return returnValue;
}
// Register callback for GetBoolean
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = GetBoolean},
};
static JSVM_CallbackStruct *method = param;
// Alias for the GetBoolean method, called from JS
static JSVM_PropertyDescriptor descriptor[] = {
    {"getBoolean", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(getBoolean(1, 2);
                                   getBoolean(1, 1);)JS";

Expected result:

JSVM OH_JSVM_GetBoolean success:0
JSVM resultType equal
JSVM OH_JSVM_GetBoolean success:1

[h2]OH_JSVM_GetValueBool

Call OH_JSVM_GetValueBool to obtain the C Boolean primitive equivalent of the given JS Boolean.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_GetValueBool
static JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info)
{
    size_t argc = 1;
    JSVM_Value args[1] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
    bool result = false;
    JSVM_Status status = OH_JSVM_GetValueBool(env, args[0], &result);
    if (status == JSVM_BOOLEAN_EXPECTED || status != JSVM_OK) {
        // If OH_JSVM_GetValueBool succeeds, JSVM_OK is returned; if a non-boolean value is passed in, JSVM_BOOLEAN_EXPECTED is returned
        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetValueBool fail:%{public}d", status);
        return nullptr;
    } else {
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetValueBool success:%{public}d", result);
    }
    JSVM_Value boolJv = nullptr;
    OH_JSVM_GetBoolean(env, result, &boolJv);
    return boolJv;
}
// Register callback for GetValueBool
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = GetValueBool},
};
static JSVM_CallbackStruct *method = param;
// Alias for the GetValueBool method, called from JS
static JSVM_PropertyDescriptor descriptor[] = {
    {"getValueBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(getValueBool("abc");
                                   getValueBool(true);
                                   getValueBool(false);)JS";

Expected result:

JSVM OH_JSVM_GetValueBool fail:7
JSVM OH_JSVM_GetValueBool success:1
JSVM OH_JSVM_GetValueBool success:0

[h2]OH_JSVM_GetGlobal

Call OH_JSVM_GetGlobal to obtain a JS global object. You can use this API to obtain the JSVM_Value that represents a JS global object, so that the JSVM module can interact with the global variables and functions defined in the JS context.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_GetGlobal
static JSVM_Value GetGlobal(JSVM_Env env, JSVM_CallbackInfo info)
{
    // Obtain the global object
    JSVM_Value value = nullptr;
    JSVM_Value global = nullptr;
    OH_JSVM_CreateInt32(env, 1, &value);
    JSVM_Status status = OH_JSVM_GetGlobal(env, &global);
    OH_JSVM_SetNamedProperty(env, global, "Row", value);
    if (status != JSVM_OK) {
        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetGlobal fail");
    } else {
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetGlobal success");
    }
    return global;
}
// Register callback for GetGlobal
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = GetGlobal},
};
static JSVM_CallbackStruct *method = param;
// Alias for the GetGlobal method, called from JS
static JSVM_PropertyDescriptor descriptor[] = {
    {"getGlobal", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(getGlobal())JS";

Expected result:

JSVM OH_JSVM_GetGlobal success

[h2]OH_JSVM_GetNull

Call OH_JSVM_GetNull to obtain a JavaScript null object.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_GetNull
static JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info)
{
    JSVM_Value nullValue = nullptr;
    JSVM_Status status = OH_JSVM_GetNull(env, &nullValue);
    if (status != JSVM_OK) {
        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetNull fail");
    } else {
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetNull success");
    }
    return nullValue;
}
// Register callback for GetNull
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = GetNull},
};
static JSVM_CallbackStruct *method = param;
// Alias for the GetNull method, called from JS
static JSVM_PropertyDescriptor descriptor[] = {
    {"getNull", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(getNull())JS";

Expected Result:

JSVM OH_JSVM_GetNull success

[h2]OH_JSVM_GetUndefined

Call OH_JSVM_GetUndefined to obtain a JavaScript undefined object.

CPP code:

#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...

// Sample method for OH_JSVM_GetUndefined
static JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info)
{
    // Obtain and parse the passed-in arguments
    size_t argc = 1;
    JSVM_Value args[1] = {nullptr};
    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
    // Create an undefined value
    JSVM_Value value = nullptr;
    JSVM_Status status = OH_JSVM_GetUndefined(env, &value);
    if (status != JSVM_OK) {
        OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetUndefined failed");
    } else {
        OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetUndefined success");
    }
    return value;
}
// Register callback for GetUndefined
static JSVM_CallbackStruct param[] = {
    {.data = nullptr, .callback = GetUndefined},
};
static JSVM_CallbackStruct *method = param;
// Alias for the GetUndefined method, called from JS
static JSVM_PropertyDescriptor descriptor[] = {
    {"getUndefined", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char *SRC_CALL_NATIVE = R"JS(getUndefined())JS";

Expected Result:

JSVM OH_JSVM_GetUndefined success