133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci#ifndef NAPI_EXPERIMENTAL 1633eb0b6dSopenharmony_ci#define NAPI_EXPERIMENTAL 1733eb0b6dSopenharmony_ci#endif 1833eb0b6dSopenharmony_ci 1933eb0b6dSopenharmony_ci#ifdef ENABLE_HITRACE 2033eb0b6dSopenharmony_ci#include <sys/prctl.h> 2133eb0b6dSopenharmony_ci#endif 2233eb0b6dSopenharmony_ci 2333eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 2433eb0b6dSopenharmony_ci#include "core/common/container_scope.h" 2533eb0b6dSopenharmony_ci#endif 2633eb0b6dSopenharmony_ci#include "ecmascript/napi/include/jsnapi.h" 2733eb0b6dSopenharmony_ci#include "native_api_internal.h" 2833eb0b6dSopenharmony_ci#include "native_engine/impl/ark/ark_native_engine.h" 2933eb0b6dSopenharmony_ci#include "native_engine/impl/ark/ark_native_reference.h" 3033eb0b6dSopenharmony_ci#include "native_engine/native_create_env.h" 3133eb0b6dSopenharmony_ci#include "native_engine/native_property.h" 3233eb0b6dSopenharmony_ci#include "native_engine/native_sendable.h" 3333eb0b6dSopenharmony_ci#include "native_engine/native_utils.h" 3433eb0b6dSopenharmony_ci#include "native_engine/native_value.h" 3533eb0b6dSopenharmony_ci#include "securec.h" 3633eb0b6dSopenharmony_ci#include "utils/log.h" 3733eb0b6dSopenharmony_ci#ifdef ENABLE_HITRACE 3833eb0b6dSopenharmony_ci#include "hitrace_meter.h" 3933eb0b6dSopenharmony_ci#endif 4033eb0b6dSopenharmony_ci 4133eb0b6dSopenharmony_ciusing panda::ArrayRef; 4233eb0b6dSopenharmony_ciusing panda::ArrayBufferRef; 4333eb0b6dSopenharmony_ciusing panda::BigIntRef; 4433eb0b6dSopenharmony_ciusing panda::BooleanRef; 4533eb0b6dSopenharmony_ciusing panda::BufferRef; 4633eb0b6dSopenharmony_ciusing panda::DateRef; 4733eb0b6dSopenharmony_ciusing panda::DataViewRef; 4833eb0b6dSopenharmony_ciusing panda::EscapeLocalScope; 4933eb0b6dSopenharmony_ciusing panda::FunctionRef; 5033eb0b6dSopenharmony_ciusing panda::Global; 5133eb0b6dSopenharmony_ciusing panda::IntegerRef; 5233eb0b6dSopenharmony_ciusing panda::JSNApi; 5333eb0b6dSopenharmony_ciusing panda::JsiRuntimeCallInfo; 5433eb0b6dSopenharmony_ciusing panda::Local; 5533eb0b6dSopenharmony_ciusing panda::LocalScope; 5633eb0b6dSopenharmony_ciusing panda::NativePointerRef; 5733eb0b6dSopenharmony_ciusing panda::NumberRef; 5833eb0b6dSopenharmony_ciusing panda::ObjectRef; 5933eb0b6dSopenharmony_ciusing panda::PrimitiveRef; 6033eb0b6dSopenharmony_ciusing panda::PromiseCapabilityRef; 6133eb0b6dSopenharmony_ciusing panda::PromiseRef; 6233eb0b6dSopenharmony_ciusing panda::PropertyAttribute; 6333eb0b6dSopenharmony_ciusing panda::StringRef; 6433eb0b6dSopenharmony_ciusing panda::SymbolRef; 6533eb0b6dSopenharmony_ciusing panda::TypedArrayRef; 6633eb0b6dSopenharmony_ciusing panda::ecmascript::EcmaVM; 6733eb0b6dSopenharmony_ci 6833eb0b6dSopenharmony_cistatic constexpr size_t MAX_BYTE_LENGTH = 2097152; 6933eb0b6dSopenharmony_cistatic constexpr size_t ONEMIB_BYTE_SIZE = 1048576; 7033eb0b6dSopenharmony_cistatic constexpr size_t SMALL_STRING_SIZE = 16; 7133eb0b6dSopenharmony_ci 7233eb0b6dSopenharmony_ciclass HandleScopeWrapper { 7333eb0b6dSopenharmony_cipublic: 7433eb0b6dSopenharmony_ci explicit HandleScopeWrapper(NativeEngine* engine) : scope_(engine->GetEcmaVm()) {} 7533eb0b6dSopenharmony_ci 7633eb0b6dSopenharmony_ciprivate: 7733eb0b6dSopenharmony_ci LocalScope scope_; 7833eb0b6dSopenharmony_ci}; 7933eb0b6dSopenharmony_ci 8033eb0b6dSopenharmony_ciclass EscapableHandleScopeWrapper { 8133eb0b6dSopenharmony_cipublic: 8233eb0b6dSopenharmony_ci explicit EscapableHandleScopeWrapper(NativeEngine* engine) 8333eb0b6dSopenharmony_ci : scope_(engine->GetEcmaVm()), escapeCalled_(false) {} 8433eb0b6dSopenharmony_ci 8533eb0b6dSopenharmony_ci bool IsEscapeCalled() const 8633eb0b6dSopenharmony_ci { 8733eb0b6dSopenharmony_ci return escapeCalled_; 8833eb0b6dSopenharmony_ci } 8933eb0b6dSopenharmony_ci 9033eb0b6dSopenharmony_ci template<typename T> 9133eb0b6dSopenharmony_ci Local<T> Escape(Local<T> value) 9233eb0b6dSopenharmony_ci { 9333eb0b6dSopenharmony_ci escapeCalled_ = true; 9433eb0b6dSopenharmony_ci return scope_.Escape(value); 9533eb0b6dSopenharmony_ci } 9633eb0b6dSopenharmony_ci 9733eb0b6dSopenharmony_ciprivate: 9833eb0b6dSopenharmony_ci EscapeLocalScope scope_; 9933eb0b6dSopenharmony_ci bool escapeCalled_; 10033eb0b6dSopenharmony_ci}; 10133eb0b6dSopenharmony_ci 10233eb0b6dSopenharmony_ciinline napi_handle_scope HandleScopeToNapiHandleScope(HandleScopeWrapper* s) 10333eb0b6dSopenharmony_ci{ 10433eb0b6dSopenharmony_ci return reinterpret_cast<napi_handle_scope>(s); 10533eb0b6dSopenharmony_ci} 10633eb0b6dSopenharmony_ci 10733eb0b6dSopenharmony_ciinline HandleScopeWrapper* NapiHandleScopeToHandleScope(napi_handle_scope s) 10833eb0b6dSopenharmony_ci{ 10933eb0b6dSopenharmony_ci return reinterpret_cast<HandleScopeWrapper*>(s); 11033eb0b6dSopenharmony_ci} 11133eb0b6dSopenharmony_ci 11233eb0b6dSopenharmony_ciinline napi_escapable_handle_scope EscapableHandleScopeToNapiEscapableHandleScope(EscapableHandleScopeWrapper* s) 11333eb0b6dSopenharmony_ci{ 11433eb0b6dSopenharmony_ci return reinterpret_cast<napi_escapable_handle_scope>(s); 11533eb0b6dSopenharmony_ci} 11633eb0b6dSopenharmony_ci 11733eb0b6dSopenharmony_ciinline EscapableHandleScopeWrapper* NapiEscapableHandleScopeToEscapableHandleScope(napi_escapable_handle_scope s) 11833eb0b6dSopenharmony_ci{ 11933eb0b6dSopenharmony_ci return reinterpret_cast<EscapableHandleScopeWrapper*>(s); 12033eb0b6dSopenharmony_ci} 12133eb0b6dSopenharmony_ci 12233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_last_error_info(napi_env env, const napi_extended_error_info** result) 12333eb0b6dSopenharmony_ci{ 12433eb0b6dSopenharmony_ci CHECK_ENV(env); 12533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 12633eb0b6dSopenharmony_ci 12733eb0b6dSopenharmony_ci *result = reinterpret_cast<napi_extended_error_info*>(reinterpret_cast<NativeEngine*>(env)->GetLastError()); 12833eb0b6dSopenharmony_ci if ((*result)->error_code == napi_ok) { 12933eb0b6dSopenharmony_ci napi_clear_last_error(env); 13033eb0b6dSopenharmony_ci } 13133eb0b6dSopenharmony_ci 13233eb0b6dSopenharmony_ci return napi_ok; 13333eb0b6dSopenharmony_ci} 13433eb0b6dSopenharmony_ci 13533eb0b6dSopenharmony_ci// Getters for defined singletons 13633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result) 13733eb0b6dSopenharmony_ci{ 13833eb0b6dSopenharmony_ci CHECK_ENV(env); 13933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 14033eb0b6dSopenharmony_ci 14133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 14233eb0b6dSopenharmony_ci Local<panda::PrimitiveRef> value = panda::JSValueRef::Undefined(vm); 14333eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 14433eb0b6dSopenharmony_ci 14533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 14633eb0b6dSopenharmony_ci} 14733eb0b6dSopenharmony_ci 14833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value* result) 14933eb0b6dSopenharmony_ci{ 15033eb0b6dSopenharmony_ci CHECK_ENV(env); 15133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 15233eb0b6dSopenharmony_ci 15333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 15433eb0b6dSopenharmony_ci Local<panda::PrimitiveRef> value = panda::JSValueRef::Null(vm); 15533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 15633eb0b6dSopenharmony_ci 15733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 15833eb0b6dSopenharmony_ci} 15933eb0b6dSopenharmony_ci 16033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_global(napi_env env, napi_value* result) 16133eb0b6dSopenharmony_ci{ 16233eb0b6dSopenharmony_ci CHECK_ENV(env); 16333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 16433eb0b6dSopenharmony_ci 16533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 16633eb0b6dSopenharmony_ci Local<panda::ObjectRef> value = panda::JSNApi::GetGlobalObject(vm); 16733eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 16833eb0b6dSopenharmony_ci 16933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 17033eb0b6dSopenharmony_ci} 17133eb0b6dSopenharmony_ci 17233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) 17333eb0b6dSopenharmony_ci{ 17433eb0b6dSopenharmony_ci CHECK_ENV(env); 17533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 17633eb0b6dSopenharmony_ci 17733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 17833eb0b6dSopenharmony_ci if (value) { 17933eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(panda::JSValueRef::True(vm)); 18033eb0b6dSopenharmony_ci } else { 18133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(panda::JSValueRef::False(vm)); 18233eb0b6dSopenharmony_ci } 18333eb0b6dSopenharmony_ci 18433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 18533eb0b6dSopenharmony_ci} 18633eb0b6dSopenharmony_ci 18733eb0b6dSopenharmony_ci// Methods to create Primitive types/Objects 18833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_object(napi_env env, napi_value* result) 18933eb0b6dSopenharmony_ci{ 19033eb0b6dSopenharmony_ci CHECK_ENV(env); 19133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 19233eb0b6dSopenharmony_ci 19333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 19433eb0b6dSopenharmony_ci auto obj = panda::ObjectRef::NewObject(vm); 19533eb0b6dSopenharmony_ci *result = reinterpret_cast<napi_value>(obj); 19633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 19733eb0b6dSopenharmony_ci} 19833eb0b6dSopenharmony_ci 19933eb0b6dSopenharmony_ci// Create JSObject with initial properties given by descriptors, note that property key must be String, and 20033eb0b6dSopenharmony_ci// must can not convert to element_index, also all keys must not duplicate. 20133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_object_with_properties(napi_env env, napi_value* result, size_t property_count, 20233eb0b6dSopenharmony_ci const napi_property_descriptor* properties) 20333eb0b6dSopenharmony_ci{ 20433eb0b6dSopenharmony_ci CHECK_ENV(env); 20533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 20633eb0b6dSopenharmony_ci 20733eb0b6dSopenharmony_ci Local<panda::ObjectRef> object; 20833eb0b6dSopenharmony_ci if (property_count <= panda::ObjectRef::MAX_PROPERTIES_ON_STACK) { 20933eb0b6dSopenharmony_ci char attrs[sizeof(PropertyAttribute) * panda::ObjectRef::MAX_PROPERTIES_ON_STACK]; 21033eb0b6dSopenharmony_ci char keys[sizeof(Local<panda::JSValueRef>) * panda::ObjectRef::MAX_PROPERTIES_ON_STACK]; 21133eb0b6dSopenharmony_ci object = NapiCreateObjectWithProperties(env, property_count, properties, 21233eb0b6dSopenharmony_ci reinterpret_cast<Local<panda::JSValueRef> *>(keys), 21333eb0b6dSopenharmony_ci reinterpret_cast<PropertyAttribute *>(attrs)); 21433eb0b6dSopenharmony_ci } else { 21533eb0b6dSopenharmony_ci void *attrs = malloc(sizeof(PropertyAttribute) * property_count); 21633eb0b6dSopenharmony_ci void *keys = malloc(sizeof(Local<panda::JSValueRef>) * property_count); 21733eb0b6dSopenharmony_ci if (attrs != nullptr && keys != nullptr) { 21833eb0b6dSopenharmony_ci object = NapiCreateObjectWithProperties(env, property_count, properties, 21933eb0b6dSopenharmony_ci reinterpret_cast<Local<panda::JSValueRef> *>(keys), 22033eb0b6dSopenharmony_ci reinterpret_cast<PropertyAttribute *>(attrs)); 22133eb0b6dSopenharmony_ci } else { 22233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 22333eb0b6dSopenharmony_ci object = panda::JSValueRef::Undefined(vm); 22433eb0b6dSopenharmony_ci napi_throw_error(env, nullptr, "malloc failed in napi_create_object_with_properties"); 22533eb0b6dSopenharmony_ci } 22633eb0b6dSopenharmony_ci if (attrs != nullptr) { 22733eb0b6dSopenharmony_ci free(attrs); 22833eb0b6dSopenharmony_ci } 22933eb0b6dSopenharmony_ci if (keys != nullptr) { 23033eb0b6dSopenharmony_ci free(keys); 23133eb0b6dSopenharmony_ci } 23233eb0b6dSopenharmony_ci } 23333eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 23433eb0b6dSopenharmony_ci 23533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 23633eb0b6dSopenharmony_ci} 23733eb0b6dSopenharmony_ci 23833eb0b6dSopenharmony_ci// Create JSObject with initial properties given by keys and values, note that property key must be String, and 23933eb0b6dSopenharmony_ci// must can not convert to element_index, also all keys must not duplicate. 24033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_object_with_named_properties(napi_env env, napi_value* result, 24133eb0b6dSopenharmony_ci size_t property_count, const char** keys, 24233eb0b6dSopenharmony_ci const napi_value* values) 24333eb0b6dSopenharmony_ci{ 24433eb0b6dSopenharmony_ci CHECK_ENV(env); 24533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 24633eb0b6dSopenharmony_ci 24733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 24833eb0b6dSopenharmony_ci Local<panda::ObjectRef> object = panda::ObjectRef::NewWithNamedProperties(vm, property_count, keys, 24933eb0b6dSopenharmony_ci reinterpret_cast<const Local<JSValueRef> *>(values)); 25033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 25133eb0b6dSopenharmony_ci 25233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 25333eb0b6dSopenharmony_ci} 25433eb0b6dSopenharmony_ci 25533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value* result) 25633eb0b6dSopenharmony_ci{ 25733eb0b6dSopenharmony_ci CHECK_ENV(env); 25833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 25933eb0b6dSopenharmony_ci 26033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 26133eb0b6dSopenharmony_ci Local<panda::ArrayRef> object = panda::ArrayRef::New(vm, 0); 26233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 26333eb0b6dSopenharmony_ci 26433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 26533eb0b6dSopenharmony_ci} 26633eb0b6dSopenharmony_ci 26733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_array_with_length(napi_env env, size_t length, napi_value* result) 26833eb0b6dSopenharmony_ci{ 26933eb0b6dSopenharmony_ci CHECK_ENV(env); 27033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 27133eb0b6dSopenharmony_ci 27233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 27333eb0b6dSopenharmony_ci Local<panda::ArrayRef> object = panda::ArrayRef::New(vm, length); 27433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 27533eb0b6dSopenharmony_ci 27633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 27733eb0b6dSopenharmony_ci} 27833eb0b6dSopenharmony_ci 27933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_sendable_array(napi_env env, napi_value* result) 28033eb0b6dSopenharmony_ci{ 28133eb0b6dSopenharmony_ci CHECK_ENV(env); 28233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 28333eb0b6dSopenharmony_ci 28433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 28533eb0b6dSopenharmony_ci Local<panda::SendableArrayRef> object = panda::SendableArrayRef::New(vm, 0); 28633eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 28733eb0b6dSopenharmony_ci 28833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 28933eb0b6dSopenharmony_ci} 29033eb0b6dSopenharmony_ci 29133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_sendable_array_with_length(napi_env env, size_t length, napi_value* result) 29233eb0b6dSopenharmony_ci{ 29333eb0b6dSopenharmony_ci CHECK_ENV(env); 29433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 29533eb0b6dSopenharmony_ci 29633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 29733eb0b6dSopenharmony_ci Local<panda::SendableArrayRef> object = panda::SendableArrayRef::New(vm, length); 29833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 29933eb0b6dSopenharmony_ci 30033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 30133eb0b6dSopenharmony_ci} 30233eb0b6dSopenharmony_ci 30333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_double(napi_env env, double value, napi_value* result) 30433eb0b6dSopenharmony_ci{ 30533eb0b6dSopenharmony_ci CHECK_ENV(env); 30633eb0b6dSopenharmony_ci CHECK_ARG(env, result); 30733eb0b6dSopenharmony_ci 30833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 30933eb0b6dSopenharmony_ci Local<panda::NumberRef> object = panda::NumberRef::New(vm, value); 31033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 31133eb0b6dSopenharmony_ci 31233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 31333eb0b6dSopenharmony_ci} 31433eb0b6dSopenharmony_ci 31533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_int32(napi_env env, int32_t value, napi_value* result) 31633eb0b6dSopenharmony_ci{ 31733eb0b6dSopenharmony_ci CHECK_ENV(env); 31833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 31933eb0b6dSopenharmony_ci 32033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 32133eb0b6dSopenharmony_ci Local<panda::NumberRef> object = panda::NumberRef::New(vm, value); 32233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 32333eb0b6dSopenharmony_ci 32433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 32533eb0b6dSopenharmony_ci} 32633eb0b6dSopenharmony_ci 32733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_uint32(napi_env env, uint32_t value, napi_value* result) 32833eb0b6dSopenharmony_ci{ 32933eb0b6dSopenharmony_ci CHECK_ENV(env); 33033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 33133eb0b6dSopenharmony_ci 33233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 33333eb0b6dSopenharmony_ci Local<panda::NumberRef> object = panda::NumberRef::New(vm, value); 33433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 33533eb0b6dSopenharmony_ci 33633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 33733eb0b6dSopenharmony_ci} 33833eb0b6dSopenharmony_ci 33933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_int64(napi_env env, int64_t value, napi_value* result) 34033eb0b6dSopenharmony_ci{ 34133eb0b6dSopenharmony_ci CHECK_ENV(env); 34233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 34333eb0b6dSopenharmony_ci 34433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 34533eb0b6dSopenharmony_ci Local<panda::NumberRef> object = panda::NumberRef::New(vm, value); 34633eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 34733eb0b6dSopenharmony_ci 34833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 34933eb0b6dSopenharmony_ci} 35033eb0b6dSopenharmony_ci 35133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, const char* str, size_t length, napi_value* result) 35233eb0b6dSopenharmony_ci{ 35333eb0b6dSopenharmony_ci CHECK_ENV(env); 35433eb0b6dSopenharmony_ci CHECK_ARG(env, str); 35533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 35633eb0b6dSopenharmony_ci 35733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 35833eb0b6dSopenharmony_ci if (length < SMALL_STRING_SIZE) { 35933eb0b6dSopenharmony_ci Local<panda::StringRef> object = panda::StringRef::NewFromUtf8WithoutStringTable( 36033eb0b6dSopenharmony_ci vm, str, (length == NAPI_AUTO_LENGTH) ? strlen(str) : length); 36133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 36233eb0b6dSopenharmony_ci } else { 36333eb0b6dSopenharmony_ci Local<panda::StringRef> object = panda::StringRef::NewFromUtf8( 36433eb0b6dSopenharmony_ci vm, str, (length == NAPI_AUTO_LENGTH) ? strlen(str) : length); 36533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 36633eb0b6dSopenharmony_ci } 36733eb0b6dSopenharmony_ci 36833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 36933eb0b6dSopenharmony_ci} 37033eb0b6dSopenharmony_ci 37133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_string_utf8(napi_env env, const char* str, size_t length, napi_value* result) 37233eb0b6dSopenharmony_ci{ 37333eb0b6dSopenharmony_ci CHECK_ENV(env); 37433eb0b6dSopenharmony_ci CHECK_ARG(env, str); 37533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 37633eb0b6dSopenharmony_ci 37733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 37833eb0b6dSopenharmony_ci if (length < SMALL_STRING_SIZE) { 37933eb0b6dSopenharmony_ci Local<panda::StringRef> object = panda::StringRef::NewFromUtf8WithoutStringTable( 38033eb0b6dSopenharmony_ci vm, str, (length == NAPI_AUTO_LENGTH) ? strlen(str) : length); 38133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 38233eb0b6dSopenharmony_ci } else { 38333eb0b6dSopenharmony_ci Local<panda::StringRef> object = panda::StringRef::NewFromUtf8( 38433eb0b6dSopenharmony_ci vm, str, (length == NAPI_AUTO_LENGTH) ? strlen(str) : length); 38533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 38633eb0b6dSopenharmony_ci } 38733eb0b6dSopenharmony_ci 38833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 38933eb0b6dSopenharmony_ci} 39033eb0b6dSopenharmony_ci 39133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_string_utf16( 39233eb0b6dSopenharmony_ci napi_env env, const char16_t* str, size_t length, napi_value* result) 39333eb0b6dSopenharmony_ci{ 39433eb0b6dSopenharmony_ci CHECK_ENV(env); 39533eb0b6dSopenharmony_ci CHECK_ARG(env, str); 39633eb0b6dSopenharmony_ci CHECK_ARG(env, result); 39733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, (length == NAPI_AUTO_LENGTH) || (length <= INT_MAX && length >= 0), napi_invalid_arg); 39833eb0b6dSopenharmony_ci 39933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 40033eb0b6dSopenharmony_ci int char16Length = static_cast<int>(std::char_traits<char16_t>::length(str)); 40133eb0b6dSopenharmony_ci if (length != NAPI_AUTO_LENGTH && length != static_cast<size_t>(char16Length)) { 40233eb0b6dSopenharmony_ci HILOG_WARN("`length` (%{public}zu) not equals to strlen(`str`) (%{public}d), result may be unexpected", 40333eb0b6dSopenharmony_ci length, char16Length); 40433eb0b6dSopenharmony_ci } 40533eb0b6dSopenharmony_ci if (length < SMALL_STRING_SIZE) { 40633eb0b6dSopenharmony_ci Local<panda::StringRef> object = panda::StringRef::NewFromUtf16WithoutStringTable( 40733eb0b6dSopenharmony_ci vm, str, (length == NAPI_AUTO_LENGTH) ? char16Length : length); 40833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 40933eb0b6dSopenharmony_ci } else { 41033eb0b6dSopenharmony_ci Local<panda::StringRef> object = panda::StringRef::NewFromUtf16( 41133eb0b6dSopenharmony_ci vm, str, (length == NAPI_AUTO_LENGTH) ? char16Length : length); 41233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 41333eb0b6dSopenharmony_ci } 41433eb0b6dSopenharmony_ci 41533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 41633eb0b6dSopenharmony_ci} 41733eb0b6dSopenharmony_ci 41833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_symbol(napi_env env, napi_value description, napi_value* result) 41933eb0b6dSopenharmony_ci{ 42033eb0b6dSopenharmony_ci CHECK_ENV(env); 42133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 42233eb0b6dSopenharmony_ci 42333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 42433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 42533eb0b6dSopenharmony_ci panda::Local<panda::JSValueRef> object = panda::JSValueRef::Undefined(vm); 42633eb0b6dSopenharmony_ci if (description == nullptr) { 42733eb0b6dSopenharmony_ci const char* str = ""; 42833eb0b6dSopenharmony_ci object = panda::StringRef::NewFromUtf8(vm, str, 0); 42933eb0b6dSopenharmony_ci } else { 43033eb0b6dSopenharmony_ci object = LocalValueFromJsValue(description); 43133eb0b6dSopenharmony_ci } 43233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, object->IsString(vm), napi_invalid_arg); 43333eb0b6dSopenharmony_ci Local<panda::SymbolRef> symbol = panda::SymbolRef::New(vm, object); 43433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(symbol); 43533eb0b6dSopenharmony_ci 43633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 43733eb0b6dSopenharmony_ci} 43833eb0b6dSopenharmony_ci 43933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_function(napi_env env, 44033eb0b6dSopenharmony_ci const char* utf8name, 44133eb0b6dSopenharmony_ci size_t length, 44233eb0b6dSopenharmony_ci napi_callback cb, 44333eb0b6dSopenharmony_ci void* data, 44433eb0b6dSopenharmony_ci napi_value* result) 44533eb0b6dSopenharmony_ci{ 44633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 44733eb0b6dSopenharmony_ci CHECK_ARG(env, cb); 44833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 44933eb0b6dSopenharmony_ci 45033eb0b6dSopenharmony_ci auto vm = const_cast<EcmaVM*>(reinterpret_cast<NativeEngine*>(env)->GetEcmaVm()); 45133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 45233eb0b6dSopenharmony_ci EscapeLocalScope scope(vm); 45333eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeCallback>(cb); 45433eb0b6dSopenharmony_ci const char* name = "defaultName"; 45533eb0b6dSopenharmony_ci NapiFunctionInfo* funcInfo = NapiFunctionInfo::CreateNewInstance(); 45633eb0b6dSopenharmony_ci if (funcInfo == nullptr) { 45733eb0b6dSopenharmony_ci HILOG_ERROR("funcInfo is nullptr"); 45833eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 45933eb0b6dSopenharmony_ci } 46033eb0b6dSopenharmony_ci funcInfo->callback = callback; 46133eb0b6dSopenharmony_ci funcInfo->data = data; 46233eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 46333eb0b6dSopenharmony_ci funcInfo->scopeId = OHOS::Ace::ContainerScope::CurrentId(); 46433eb0b6dSopenharmony_ci#endif 46533eb0b6dSopenharmony_ci 46633eb0b6dSopenharmony_ci Local<panda::FunctionRef> fn = panda::FunctionRef::NewConcurrent( 46733eb0b6dSopenharmony_ci vm, ArkNativeFunctionCallBack, 46833eb0b6dSopenharmony_ci [](void* env, void* externalPointer, void* data) { 46933eb0b6dSopenharmony_ci auto info = reinterpret_cast<NapiFunctionInfo*>(data); 47033eb0b6dSopenharmony_ci if (info != nullptr) { 47133eb0b6dSopenharmony_ci delete info; 47233eb0b6dSopenharmony_ci } 47333eb0b6dSopenharmony_ci }, 47433eb0b6dSopenharmony_ci reinterpret_cast<void*>(funcInfo), true 47533eb0b6dSopenharmony_ci ); 47633eb0b6dSopenharmony_ci Local<panda::StringRef> fnName = panda::StringRef::NewFromUtf8(vm, utf8name != nullptr ? utf8name : name); 47733eb0b6dSopenharmony_ci fn->SetName(vm, fnName); 47833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(scope.Escape(fn)); 47933eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 48033eb0b6dSopenharmony_ci} 48133eb0b6dSopenharmony_ci 48233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value* result) 48333eb0b6dSopenharmony_ci{ 48433eb0b6dSopenharmony_ci CHECK_ENV(env); 48533eb0b6dSopenharmony_ci CHECK_ARG(env, msg); 48633eb0b6dSopenharmony_ci CHECK_ARG(env, result); 48733eb0b6dSopenharmony_ci 48833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 48933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 49033eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeValue = panda::JSValueRef::Undefined(vm); 49133eb0b6dSopenharmony_ci if (code != nullptr) { 49233eb0b6dSopenharmony_ci codeValue = LocalValueFromJsValue(code); 49333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, codeValue->IsString(vm) || codeValue->IsNumber(), napi_invalid_arg); 49433eb0b6dSopenharmony_ci } 49533eb0b6dSopenharmony_ci 49633eb0b6dSopenharmony_ci auto msgValue = LocalValueFromJsValue(msg); 49733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, msgValue->IsString(vm), napi_invalid_arg); 49833eb0b6dSopenharmony_ci 49933eb0b6dSopenharmony_ci Local<panda::JSValueRef> errorVal = panda::Exception::Error(vm, msgValue); 50033eb0b6dSopenharmony_ci if (code != nullptr) { 50133eb0b6dSopenharmony_ci Local<panda::StringRef> codeKey = panda::StringRef::NewFromUtf8(vm, "code"); 50233eb0b6dSopenharmony_ci Local<panda::ObjectRef> errorObj(errorVal); 50333eb0b6dSopenharmony_ci errorObj->Set(vm, codeKey, codeValue); 50433eb0b6dSopenharmony_ci } 50533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(errorVal); 50633eb0b6dSopenharmony_ci 50733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 50833eb0b6dSopenharmony_ci} 50933eb0b6dSopenharmony_ci 51033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_type_error(napi_env env, napi_value code, napi_value msg, napi_value* result) 51133eb0b6dSopenharmony_ci{ 51233eb0b6dSopenharmony_ci CHECK_ENV(env); 51333eb0b6dSopenharmony_ci CHECK_ARG(env, msg); 51433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 51533eb0b6dSopenharmony_ci 51633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 51733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 51833eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeValue = panda::JSValueRef::Undefined(vm); 51933eb0b6dSopenharmony_ci if (code != nullptr) { 52033eb0b6dSopenharmony_ci codeValue = LocalValueFromJsValue(code); 52133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, codeValue->IsString(vm) || codeValue->IsNumber(), napi_invalid_arg); 52233eb0b6dSopenharmony_ci } 52333eb0b6dSopenharmony_ci auto msgValue = LocalValueFromJsValue(msg); 52433eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, msgValue->IsString(vm), napi_invalid_arg); 52533eb0b6dSopenharmony_ci 52633eb0b6dSopenharmony_ci Local<panda::JSValueRef> errorVal = panda::Exception::TypeError(vm, msgValue); 52733eb0b6dSopenharmony_ci if (code != nullptr) { 52833eb0b6dSopenharmony_ci Local<panda::StringRef> codeKey = panda::StringRef::NewFromUtf8(vm, "code"); 52933eb0b6dSopenharmony_ci Local<panda::ObjectRef> errorObj(errorVal); 53033eb0b6dSopenharmony_ci errorObj->Set(vm, codeKey, codeValue); 53133eb0b6dSopenharmony_ci } 53233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(errorVal); 53333eb0b6dSopenharmony_ci 53433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 53533eb0b6dSopenharmony_ci} 53633eb0b6dSopenharmony_ci 53733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_range_error(napi_env env, napi_value code, napi_value msg, napi_value* result) 53833eb0b6dSopenharmony_ci{ 53933eb0b6dSopenharmony_ci CHECK_ENV(env); 54033eb0b6dSopenharmony_ci CHECK_ARG(env, msg); 54133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 54233eb0b6dSopenharmony_ci 54333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 54433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 54533eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeValue = panda::JSValueRef::Undefined(vm); 54633eb0b6dSopenharmony_ci 54733eb0b6dSopenharmony_ci if (code != nullptr) { 54833eb0b6dSopenharmony_ci codeValue = LocalValueFromJsValue(code); 54933eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, codeValue->IsString(vm) || codeValue->IsNumber(), napi_invalid_arg); 55033eb0b6dSopenharmony_ci } 55133eb0b6dSopenharmony_ci auto msgValue = LocalValueFromJsValue(msg); 55233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, msgValue->IsString(vm), napi_invalid_arg); 55333eb0b6dSopenharmony_ci 55433eb0b6dSopenharmony_ci Local<panda::JSValueRef> errorVal = panda::Exception::RangeError(vm, msgValue); 55533eb0b6dSopenharmony_ci if (code != nullptr) { 55633eb0b6dSopenharmony_ci Local<panda::StringRef> codeKey = panda::StringRef::NewFromUtf8(vm, "code"); 55733eb0b6dSopenharmony_ci Local<panda::ObjectRef> errorObj(errorVal); 55833eb0b6dSopenharmony_ci errorObj->Set(vm, codeKey, codeValue); 55933eb0b6dSopenharmony_ci } 56033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(errorVal); 56133eb0b6dSopenharmony_ci 56233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 56333eb0b6dSopenharmony_ci} 56433eb0b6dSopenharmony_ci 56533eb0b6dSopenharmony_ci// Methods to get the native napi_value from Primitive type 56633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_typeof(napi_env env, napi_value value, napi_valuetype* result) 56733eb0b6dSopenharmony_ci{ 56833eb0b6dSopenharmony_ci CHECK_ENV(env); 56933eb0b6dSopenharmony_ci CHECK_ARG(env, value); 57033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 57133eb0b6dSopenharmony_ci 57233eb0b6dSopenharmony_ci auto valueObj = LocalValueFromJsValue(value); 57333eb0b6dSopenharmony_ci napi_valuetype resultType; 57433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 57533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 57633eb0b6dSopenharmony_ci 57733eb0b6dSopenharmony_ci if (valueObj->IsNumber()) { 57833eb0b6dSopenharmony_ci resultType = napi_number; 57933eb0b6dSopenharmony_ci } else if (valueObj->IsString(vm)) { 58033eb0b6dSopenharmony_ci resultType = napi_string; 58133eb0b6dSopenharmony_ci } else if (valueObj->IsFunction(vm)) { 58233eb0b6dSopenharmony_ci resultType = napi_function; 58333eb0b6dSopenharmony_ci } else if (valueObj->IsNativePointer(vm)) { 58433eb0b6dSopenharmony_ci resultType = napi_external; 58533eb0b6dSopenharmony_ci } else if (valueObj->IsNull()) { 58633eb0b6dSopenharmony_ci resultType = napi_null; 58733eb0b6dSopenharmony_ci } else if (valueObj->IsBoolean()) { 58833eb0b6dSopenharmony_ci resultType = napi_boolean; 58933eb0b6dSopenharmony_ci } else if (valueObj->IsUndefined()) { 59033eb0b6dSopenharmony_ci resultType = napi_undefined; 59133eb0b6dSopenharmony_ci } else if (valueObj->IsSymbol(vm)) { 59233eb0b6dSopenharmony_ci resultType = napi_symbol; 59333eb0b6dSopenharmony_ci } else if (valueObj->IsBigInt(vm)) { 59433eb0b6dSopenharmony_ci resultType = napi_bigint; 59533eb0b6dSopenharmony_ci } else if (valueObj->IsObject(vm)) { 59633eb0b6dSopenharmony_ci resultType = napi_object; 59733eb0b6dSopenharmony_ci } else { 59833eb0b6dSopenharmony_ci resultType = napi_undefined; 59933eb0b6dSopenharmony_ci } 60033eb0b6dSopenharmony_ci *result = resultType; 60133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 60233eb0b6dSopenharmony_ci} 60333eb0b6dSopenharmony_ci 60433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_double(napi_env env, napi_value value, double* result) 60533eb0b6dSopenharmony_ci{ 60633eb0b6dSopenharmony_ci CHECK_ENV(env); 60733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 60833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 60933eb0b6dSopenharmony_ci 61033eb0b6dSopenharmony_ci panda::JSValueRef* nativeValue = reinterpret_cast<panda::JSValueRef*>(value); 61133eb0b6dSopenharmony_ci bool isNumber = false; 61233eb0b6dSopenharmony_ci double dValue = nativeValue->GetValueDouble(isNumber); 61333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isNumber, napi_number_expected); 61433eb0b6dSopenharmony_ci *result = dValue; 61533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 61633eb0b6dSopenharmony_ci} 61733eb0b6dSopenharmony_ci 61833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_int32(napi_env env, napi_value value, int32_t* result) 61933eb0b6dSopenharmony_ci{ 62033eb0b6dSopenharmony_ci CHECK_ENV(env); 62133eb0b6dSopenharmony_ci CHECK_ARG(env, value); 62233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 62333eb0b6dSopenharmony_ci 62433eb0b6dSopenharmony_ci panda::JSValueRef* nativeValue = reinterpret_cast<panda::JSValueRef*>(value); 62533eb0b6dSopenharmony_ci bool isNumber = false; 62633eb0b6dSopenharmony_ci int32_t i32Value = nativeValue->GetValueInt32(isNumber); 62733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isNumber, napi_number_expected); 62833eb0b6dSopenharmony_ci *result = i32Value; 62933eb0b6dSopenharmony_ci 63033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 63133eb0b6dSopenharmony_ci} 63233eb0b6dSopenharmony_ci 63333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_uint32(napi_env env, napi_value value, uint32_t* result) 63433eb0b6dSopenharmony_ci{ 63533eb0b6dSopenharmony_ci CHECK_ENV(env); 63633eb0b6dSopenharmony_ci CHECK_ARG(env, value); 63733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 63833eb0b6dSopenharmony_ci 63933eb0b6dSopenharmony_ci panda::JSValueRef* nativeValue = reinterpret_cast<panda::JSValueRef*>(value); 64033eb0b6dSopenharmony_ci bool isNumber = false; 64133eb0b6dSopenharmony_ci uint32_t u32Value = nativeValue->GetValueUint32(isNumber); 64233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isNumber, napi_number_expected); 64333eb0b6dSopenharmony_ci *result = u32Value; 64433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 64533eb0b6dSopenharmony_ci} 64633eb0b6dSopenharmony_ci 64733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_int64(napi_env env, napi_value value, int64_t* result) 64833eb0b6dSopenharmony_ci{ 64933eb0b6dSopenharmony_ci CHECK_ENV(env); 65033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 65133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 65233eb0b6dSopenharmony_ci 65333eb0b6dSopenharmony_ci panda::JSValueRef* nativeValue = reinterpret_cast<panda::JSValueRef*>(value); 65433eb0b6dSopenharmony_ci bool isNumber = false; 65533eb0b6dSopenharmony_ci int64_t i64Value = nativeValue->GetValueInt64(isNumber); 65633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isNumber, napi_number_expected); 65733eb0b6dSopenharmony_ci *result = i64Value; 65833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 65933eb0b6dSopenharmony_ci} 66033eb0b6dSopenharmony_ci 66133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_bool(napi_env env, napi_value value, bool* result) 66233eb0b6dSopenharmony_ci{ 66333eb0b6dSopenharmony_ci CHECK_ENV(env); 66433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 66533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 66633eb0b6dSopenharmony_ci 66733eb0b6dSopenharmony_ci panda::JSValueRef* nativeValue = reinterpret_cast<panda::JSValueRef*>(value); 66833eb0b6dSopenharmony_ci bool isBool = false; 66933eb0b6dSopenharmony_ci bool bValue = nativeValue->GetValueBool(isBool); 67033eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isBool, napi_boolean_expected); 67133eb0b6dSopenharmony_ci *result = bValue; 67233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 67333eb0b6dSopenharmony_ci} 67433eb0b6dSopenharmony_ci 67533eb0b6dSopenharmony_ci// Copies LATIN-1 encoded bytes from a string into a buffer. 67633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env, 67733eb0b6dSopenharmony_ci napi_value value, 67833eb0b6dSopenharmony_ci char* buf, 67933eb0b6dSopenharmony_ci size_t bufsize, 68033eb0b6dSopenharmony_ci size_t* result) 68133eb0b6dSopenharmony_ci{ 68233eb0b6dSopenharmony_ci CHECK_ENV(env); 68333eb0b6dSopenharmony_ci CHECK_ARG(env, value); 68433eb0b6dSopenharmony_ci 68533eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 68633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 68733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 68833eb0b6dSopenharmony_ci 68933eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsString(vm), napi_string_expected); 69033eb0b6dSopenharmony_ci Local<panda::StringRef> stringVal(nativeValue); 69133eb0b6dSopenharmony_ci if (buf == nullptr) { 69233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 69333eb0b6dSopenharmony_ci *result = stringVal->Length(vm); 69433eb0b6dSopenharmony_ci } else if (bufsize != 0) { 69533eb0b6dSopenharmony_ci uint32_t copied = stringVal->WriteLatin1(vm, buf, bufsize); 69633eb0b6dSopenharmony_ci buf[copied] = '\0'; 69733eb0b6dSopenharmony_ci if (result != nullptr) { 69833eb0b6dSopenharmony_ci *result = copied; 69933eb0b6dSopenharmony_ci } 70033eb0b6dSopenharmony_ci } else if (result != nullptr) { 70133eb0b6dSopenharmony_ci *result = 0; 70233eb0b6dSopenharmony_ci } 70333eb0b6dSopenharmony_ci 70433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 70533eb0b6dSopenharmony_ci} 70633eb0b6dSopenharmony_ci 70733eb0b6dSopenharmony_ci// Copies UTF-8 encoded bytes from a string into a buffer. 70833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env, 70933eb0b6dSopenharmony_ci napi_value value, 71033eb0b6dSopenharmony_ci char* buf, 71133eb0b6dSopenharmony_ci size_t bufsize, 71233eb0b6dSopenharmony_ci size_t* result) 71333eb0b6dSopenharmony_ci{ 71433eb0b6dSopenharmony_ci CHECK_ENV(env); 71533eb0b6dSopenharmony_ci CHECK_ARG(env, value); 71633eb0b6dSopenharmony_ci 71733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 71833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 71933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 72033eb0b6dSopenharmony_ci 72133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsString(vm), napi_string_expected); 72233eb0b6dSopenharmony_ci Local<panda::StringRef> stringVal(nativeValue); 72333eb0b6dSopenharmony_ci if (buf == nullptr) { 72433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 72533eb0b6dSopenharmony_ci *result = stringVal->Utf8Length(vm, true) - 1; 72633eb0b6dSopenharmony_ci } else if (bufsize != 0) { 72733eb0b6dSopenharmony_ci uint32_t copied = stringVal->WriteUtf8(vm, buf, bufsize - 1, true) - 1; 72833eb0b6dSopenharmony_ci buf[copied] = '\0'; 72933eb0b6dSopenharmony_ci if (result != nullptr) { 73033eb0b6dSopenharmony_ci *result = copied; 73133eb0b6dSopenharmony_ci } 73233eb0b6dSopenharmony_ci } else if (result != nullptr) { 73333eb0b6dSopenharmony_ci *result = 0; 73433eb0b6dSopenharmony_ci } 73533eb0b6dSopenharmony_ci 73633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 73733eb0b6dSopenharmony_ci} 73833eb0b6dSopenharmony_ci 73933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env, 74033eb0b6dSopenharmony_ci napi_value value, 74133eb0b6dSopenharmony_ci char16_t* buf, 74233eb0b6dSopenharmony_ci size_t bufsize, 74333eb0b6dSopenharmony_ci size_t* result) 74433eb0b6dSopenharmony_ci{ 74533eb0b6dSopenharmony_ci CHECK_ENV(env); 74633eb0b6dSopenharmony_ci CHECK_ARG(env, value); 74733eb0b6dSopenharmony_ci 74833eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 74933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 75033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 75133eb0b6dSopenharmony_ci 75233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsString(vm), napi_string_expected); 75333eb0b6dSopenharmony_ci Local<panda::StringRef> stringVal(nativeValue); 75433eb0b6dSopenharmony_ci if (buf == nullptr) { 75533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 75633eb0b6dSopenharmony_ci *result = stringVal->Length(vm); 75733eb0b6dSopenharmony_ci } else if (bufsize == 1) { 75833eb0b6dSopenharmony_ci buf[0] = '\0'; 75933eb0b6dSopenharmony_ci if (result != nullptr) { 76033eb0b6dSopenharmony_ci *result = 0; 76133eb0b6dSopenharmony_ci } 76233eb0b6dSopenharmony_ci } else if (bufsize != 0) { 76333eb0b6dSopenharmony_ci uint32_t copied = stringVal->WriteUtf16(vm, buf, bufsize - 1); // bufsize - 1 : reserve the position of buf "\0" 76433eb0b6dSopenharmony_ci buf[copied] = '\0'; 76533eb0b6dSopenharmony_ci if (result != nullptr) { 76633eb0b6dSopenharmony_ci *result = copied; 76733eb0b6dSopenharmony_ci } 76833eb0b6dSopenharmony_ci } else if (result != nullptr) { 76933eb0b6dSopenharmony_ci *result = 0; 77033eb0b6dSopenharmony_ci } 77133eb0b6dSopenharmony_ci 77233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 77333eb0b6dSopenharmony_ci} 77433eb0b6dSopenharmony_ci 77533eb0b6dSopenharmony_ci// Methods to coerce values 77633eb0b6dSopenharmony_ci// These APIs may execute user scripts 77733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_coerce_to_bool(napi_env env, napi_value value, napi_value* result) 77833eb0b6dSopenharmony_ci{ 77933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 78033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 78133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 78233eb0b6dSopenharmony_ci 78333eb0b6dSopenharmony_ci Local<panda::JSValueRef> val = LocalValueFromJsValue(value); 78433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 78533eb0b6dSopenharmony_ci Local<panda::BooleanRef> boolVal = val->ToBoolean(vm); 78633eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(boolVal); 78733eb0b6dSopenharmony_ci 78833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 78933eb0b6dSopenharmony_ci} 79033eb0b6dSopenharmony_ci 79133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_coerce_to_number(napi_env env, napi_value value, napi_value* result) 79233eb0b6dSopenharmony_ci{ 79333eb0b6dSopenharmony_ci CHECK_ENV(env); 79433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 79533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 79633eb0b6dSopenharmony_ci 79733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 79833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 79933eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(nativeValue->ToNumber(vm)); 80033eb0b6dSopenharmony_ci 80133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 80233eb0b6dSopenharmony_ci} 80333eb0b6dSopenharmony_ci 80433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_coerce_to_object(napi_env env, napi_value value, napi_value* result) 80533eb0b6dSopenharmony_ci{ 80633eb0b6dSopenharmony_ci CHECK_ENV(env); 80733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 80833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 80933eb0b6dSopenharmony_ci 81033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 81133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 81233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(nativeValue->ToObject(vm)); 81333eb0b6dSopenharmony_ci 81433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 81533eb0b6dSopenharmony_ci} 81633eb0b6dSopenharmony_ci 81733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_coerce_to_string(napi_env env, napi_value value, napi_value* result) 81833eb0b6dSopenharmony_ci{ 81933eb0b6dSopenharmony_ci CHECK_ENV(env); 82033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 82133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 82233eb0b6dSopenharmony_ci 82333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 82433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 82533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(nativeValue->ToString(vm)); 82633eb0b6dSopenharmony_ci 82733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 82833eb0b6dSopenharmony_ci} 82933eb0b6dSopenharmony_ci 83033eb0b6dSopenharmony_ci// Methods to work with Objects 83133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_prototype(napi_env env, napi_value object, napi_value* result) 83233eb0b6dSopenharmony_ci{ 83333eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 83433eb0b6dSopenharmony_ci CHECK_ARG(env, object); 83533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 83633eb0b6dSopenharmony_ci 83733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 83833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 83933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 84033eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 84133eb0b6dSopenharmony_ci Local<panda::JSValueRef> val = obj->GetPrototype(vm); 84233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(val); 84333eb0b6dSopenharmony_ci 84433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 84533eb0b6dSopenharmony_ci} 84633eb0b6dSopenharmony_ci 84733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_property_names(napi_env env, napi_value object, napi_value* result) 84833eb0b6dSopenharmony_ci{ 84933eb0b6dSopenharmony_ci CHECK_ENV(env); 85033eb0b6dSopenharmony_ci CHECK_ARG(env, object); 85133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 85233eb0b6dSopenharmony_ci 85333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 85433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 85533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 85633eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 85733eb0b6dSopenharmony_ci Local<panda::ArrayRef> arrayVal = obj->GetOwnPropertyNames(vm); 85833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 85933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 86033eb0b6dSopenharmony_ci} 86133eb0b6dSopenharmony_ci 86233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_set_property(napi_env env, napi_value object, napi_value key, napi_value value) 86333eb0b6dSopenharmony_ci{ 86433eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 86533eb0b6dSopenharmony_ci CHECK_ARG(env, object); 86633eb0b6dSopenharmony_ci CHECK_ARG(env, key); 86733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 86833eb0b6dSopenharmony_ci 86933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 87033eb0b6dSopenharmony_ci auto propKey = LocalValueFromJsValue(key); 87133eb0b6dSopenharmony_ci auto propValue = LocalValueFromJsValue(value); 87233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 87333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 87433eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 87533eb0b6dSopenharmony_ci obj->Set(vm, propKey, propValue); 87633eb0b6dSopenharmony_ci 87733eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 87833eb0b6dSopenharmony_ci} 87933eb0b6dSopenharmony_ci 88033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_has_property(napi_env env, napi_value object, napi_value key, bool* result) 88133eb0b6dSopenharmony_ci{ 88233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 88333eb0b6dSopenharmony_ci CHECK_ARG(env, object); 88433eb0b6dSopenharmony_ci CHECK_ARG(env, key); 88533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 88633eb0b6dSopenharmony_ci 88733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 88833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 88933eb0b6dSopenharmony_ci Local<panda::JSValueRef> hasResult = JSNApi::NapiHasProperty(vm, reinterpret_cast<uintptr_t>(object), 89033eb0b6dSopenharmony_ci reinterpret_cast<uintptr_t>(key)); 89133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(hasResult), napi_object_expected); 89233eb0b6dSopenharmony_ci *result = hasResult->BooleaValue(vm); 89333eb0b6dSopenharmony_ci 89433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 89533eb0b6dSopenharmony_ci} 89633eb0b6dSopenharmony_ci 89733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_property(napi_env env, napi_value object, napi_value key, napi_value* result) 89833eb0b6dSopenharmony_ci{ 89933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 90033eb0b6dSopenharmony_ci CHECK_ARG(env, object); 90133eb0b6dSopenharmony_ci CHECK_ARG(env, key); 90233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 90333eb0b6dSopenharmony_ci 90433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 90533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 90633eb0b6dSopenharmony_ci Local<panda::JSValueRef> value = JSNApi::NapiGetProperty(vm, reinterpret_cast<uintptr_t>(object), 90733eb0b6dSopenharmony_ci reinterpret_cast<uintptr_t>(key)); 90833eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(value), napi_object_expected); 90933eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 91033eb0b6dSopenharmony_ci FunctionSetContainerId(vm, value); 91133eb0b6dSopenharmony_ci#endif 91233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 91333eb0b6dSopenharmony_ci 91433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 91533eb0b6dSopenharmony_ci} 91633eb0b6dSopenharmony_ci 91733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_delete_property(napi_env env, napi_value object, napi_value key, bool* result) 91833eb0b6dSopenharmony_ci{ 91933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 92033eb0b6dSopenharmony_ci CHECK_ARG(env, object); 92133eb0b6dSopenharmony_ci CHECK_ARG(env, key); 92233eb0b6dSopenharmony_ci 92333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 92433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 92533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 92633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm) || nativeValue->IsFunction(vm), napi_object_expected); 92733eb0b6dSopenharmony_ci auto deleteResult = JSNApi::NapiDeleteProperty(vm, reinterpret_cast<uintptr_t>(object), 92833eb0b6dSopenharmony_ci reinterpret_cast<uintptr_t>(key)); 92933eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(deleteResult), napi_object_expected); 93033eb0b6dSopenharmony_ci if (result) { 93133eb0b6dSopenharmony_ci *result = deleteResult->BooleaValue(vm); 93233eb0b6dSopenharmony_ci } 93333eb0b6dSopenharmony_ci 93433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 93533eb0b6dSopenharmony_ci} 93633eb0b6dSopenharmony_ci 93733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_has_own_property(napi_env env, napi_value object, napi_value key, bool* result) 93833eb0b6dSopenharmony_ci{ 93933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 94033eb0b6dSopenharmony_ci CHECK_ARG(env, object); 94133eb0b6dSopenharmony_ci CHECK_ARG(env, key); 94233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 94333eb0b6dSopenharmony_ci 94433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 94533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 94633eb0b6dSopenharmony_ci auto hasResult = JSNApi::NapiHasOwnProperty(vm, reinterpret_cast<uintptr_t>(object), 94733eb0b6dSopenharmony_ci reinterpret_cast<uintptr_t>(key)); 94833eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(hasResult), napi_object_expected); 94933eb0b6dSopenharmony_ci if (result) { 95033eb0b6dSopenharmony_ci *result = hasResult->BooleaValue(vm); 95133eb0b6dSopenharmony_ci } 95233eb0b6dSopenharmony_ci 95333eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 95433eb0b6dSopenharmony_ci} 95533eb0b6dSopenharmony_ci 95633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_set_named_property(napi_env env, napi_value object, const char* utf8name, napi_value value) 95733eb0b6dSopenharmony_ci{ 95833eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 95933eb0b6dSopenharmony_ci CHECK_ARG(env, object); 96033eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 96133eb0b6dSopenharmony_ci CHECK_ARG(env, value); 96233eb0b6dSopenharmony_ci 96333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 96433eb0b6dSopenharmony_ci auto propVal = LocalValueFromJsValue(value); 96533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 96633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 96733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm) || nativeValue->IsFunction(vm), napi_object_expected); 96833eb0b6dSopenharmony_ci Local<panda::ObjectRef> obj(nativeValue); 96933eb0b6dSopenharmony_ci obj->Set(vm, utf8name, propVal); 97033eb0b6dSopenharmony_ci 97133eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 97233eb0b6dSopenharmony_ci} 97333eb0b6dSopenharmony_ci 97433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_has_named_property(napi_env env, napi_value object, const char* utf8name, bool* result) 97533eb0b6dSopenharmony_ci{ 97633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 97733eb0b6dSopenharmony_ci CHECK_ARG(env, object); 97833eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 97933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 98033eb0b6dSopenharmony_ci 98133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 98233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 98333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 98433eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 98533eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::NewFromUtf8(vm, utf8name); 98633eb0b6dSopenharmony_ci *result = obj->Has(vm, key); 98733eb0b6dSopenharmony_ci 98833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 98933eb0b6dSopenharmony_ci} 99033eb0b6dSopenharmony_ci 99133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_named_property(napi_env env, 99233eb0b6dSopenharmony_ci napi_value object, 99333eb0b6dSopenharmony_ci const char* utf8name, 99433eb0b6dSopenharmony_ci napi_value* result) 99533eb0b6dSopenharmony_ci{ 99633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 99733eb0b6dSopenharmony_ci CHECK_ARG(env, object); 99833eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 99933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 100033eb0b6dSopenharmony_ci 100133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 100233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 100333eb0b6dSopenharmony_ci Local<panda::JSValueRef> value = JSNApi::NapiGetNamedProperty(vm, reinterpret_cast<uintptr_t>(object), utf8name); 100433eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(value), napi_object_expected); 100533eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 100633eb0b6dSopenharmony_ci FunctionSetContainerId(vm, value); 100733eb0b6dSopenharmony_ci#endif 100833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 100933eb0b6dSopenharmony_ci 101033eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 101133eb0b6dSopenharmony_ci} 101233eb0b6dSopenharmony_ci 101333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_own_property_descriptor(napi_env env, 101433eb0b6dSopenharmony_ci napi_value object, 101533eb0b6dSopenharmony_ci const char* utf8name, 101633eb0b6dSopenharmony_ci napi_value* result) 101733eb0b6dSopenharmony_ci{ 101833eb0b6dSopenharmony_ci CHECK_ENV(env); 101933eb0b6dSopenharmony_ci CHECK_ARG(env, object); 102033eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 102133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 102233eb0b6dSopenharmony_ci 102333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 102433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 102533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 102633eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 102733eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::NewFromUtf8(vm, utf8name); 102833eb0b6dSopenharmony_ci panda::PropertyAttribute property; 102933eb0b6dSopenharmony_ci obj->GetOwnProperty(vm, key, property); 103033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(property.GetValue(vm)); 103133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 103233eb0b6dSopenharmony_ci} 103333eb0b6dSopenharmony_ci 103433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_set_element(napi_env env, napi_value object, uint32_t index, napi_value value) 103533eb0b6dSopenharmony_ci{ 103633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 103733eb0b6dSopenharmony_ci CHECK_ARG(env, object); 103833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 103933eb0b6dSopenharmony_ci 104033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 104133eb0b6dSopenharmony_ci auto elementValue = LocalValueFromJsValue(value); 104233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 104333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 104433eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 104533eb0b6dSopenharmony_ci obj->Set(vm, index, elementValue); 104633eb0b6dSopenharmony_ci 104733eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 104833eb0b6dSopenharmony_ci} 104933eb0b6dSopenharmony_ci 105033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_has_element(napi_env env, napi_value object, uint32_t index, bool* result) 105133eb0b6dSopenharmony_ci{ 105233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 105333eb0b6dSopenharmony_ci CHECK_ARG(env, object); 105433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 105533eb0b6dSopenharmony_ci 105633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 105733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 105833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 105933eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 106033eb0b6dSopenharmony_ci *result = obj->Has(vm, index); 106133eb0b6dSopenharmony_ci 106233eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 106333eb0b6dSopenharmony_ci} 106433eb0b6dSopenharmony_ci 106533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_element(napi_env env, napi_value object, uint32_t index, napi_value* result) 106633eb0b6dSopenharmony_ci{ 106733eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 106833eb0b6dSopenharmony_ci CHECK_ARG(env, object); 106933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 107033eb0b6dSopenharmony_ci 107133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 107233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 107333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 107433eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 107533eb0b6dSopenharmony_ci Local<panda::JSValueRef> value = obj->Get(vm, index); 107633eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 107733eb0b6dSopenharmony_ci FunctionSetContainerId(vm, value); 107833eb0b6dSopenharmony_ci#endif 107933eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 108033eb0b6dSopenharmony_ci 108133eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 108233eb0b6dSopenharmony_ci} 108333eb0b6dSopenharmony_ci 108433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_delete_element(napi_env env, napi_value object, uint32_t index, bool* result) 108533eb0b6dSopenharmony_ci{ 108633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 108733eb0b6dSopenharmony_ci CHECK_ARG(env, object); 108833eb0b6dSopenharmony_ci 108933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 109033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 109133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 109233eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 109333eb0b6dSopenharmony_ci bool deleteResult = obj->Delete(vm, index); 109433eb0b6dSopenharmony_ci if (result) { 109533eb0b6dSopenharmony_ci *result = deleteResult; 109633eb0b6dSopenharmony_ci } 109733eb0b6dSopenharmony_ci 109833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 109933eb0b6dSopenharmony_ci} 110033eb0b6dSopenharmony_ci 110133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_define_properties(napi_env env, 110233eb0b6dSopenharmony_ci napi_value object, 110333eb0b6dSopenharmony_ci size_t property_count, 110433eb0b6dSopenharmony_ci const napi_property_descriptor* properties) 110533eb0b6dSopenharmony_ci{ 110633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 110733eb0b6dSopenharmony_ci CHECK_ARG(env, object); 110833eb0b6dSopenharmony_ci CHECK_ARG(env, properties); 110933eb0b6dSopenharmony_ci 111033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 111133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 111233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 111333eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, nativeObject); 111433eb0b6dSopenharmony_ci 111533eb0b6dSopenharmony_ci auto nativeProperties = reinterpret_cast<const NapiPropertyDescriptor*>(properties); 111633eb0b6dSopenharmony_ci for (size_t i = 0; i < property_count; i++) { 111733eb0b6dSopenharmony_ci if (nativeProperties[i].utf8name == nullptr) { 111833eb0b6dSopenharmony_ci auto name = LocalValueFromJsValue(nativeProperties[i].name); 111933eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, !name.IsEmpty() && (name->IsString(vm) || name->IsSymbol(vm)), 112033eb0b6dSopenharmony_ci napi_name_expected); 112133eb0b6dSopenharmony_ci } 112233eb0b6dSopenharmony_ci NapiDefineProperty(env, nativeObject, nativeProperties[i]); 112333eb0b6dSopenharmony_ci } 112433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 112533eb0b6dSopenharmony_ci} 112633eb0b6dSopenharmony_ci 112733eb0b6dSopenharmony_ci// Methods to work with Arrays 112833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_array(napi_env env, napi_value value, bool* result) 112933eb0b6dSopenharmony_ci{ 113033eb0b6dSopenharmony_ci CHECK_ENV(env); 113133eb0b6dSopenharmony_ci CHECK_ARG(env, value); 113233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 113333eb0b6dSopenharmony_ci 113433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 113533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 113633eb0b6dSopenharmony_ci 113733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 113833eb0b6dSopenharmony_ci *result = nativeValue->IsJSArray(vm) || nativeValue->IsSharedArray(vm); 113933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 114033eb0b6dSopenharmony_ci} 114133eb0b6dSopenharmony_ci 114233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_array_length(napi_env env, napi_value value, uint32_t* result) 114333eb0b6dSopenharmony_ci{ 114433eb0b6dSopenharmony_ci CHECK_ENV((env)); 114533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, (reinterpret_cast<NativeEngine*>(env))->lastException_.IsEmpty(), 114633eb0b6dSopenharmony_ci napi_pending_exception); 114733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 114833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 114933eb0b6dSopenharmony_ci 115033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 115133eb0b6dSopenharmony_ci bool isArrayOrSharedArray = false; 115233eb0b6dSopenharmony_ci auto nativeValue = reinterpret_cast<panda::JSValueRef *>(value); 115333eb0b6dSopenharmony_ci nativeValue->TryGetArrayLength(vm, &isArrayOrSharedArray, result); 115433eb0b6dSopenharmony_ci if (!isArrayOrSharedArray) { 115533eb0b6dSopenharmony_ci HILOG_ERROR("argument is not type of array or sharedarray"); 115633eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_array_expected); 115733eb0b6dSopenharmony_ci } 115833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 115933eb0b6dSopenharmony_ci} 116033eb0b6dSopenharmony_ci 116133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_sendable(napi_env env, napi_value value, bool* result) 116233eb0b6dSopenharmony_ci{ 116333eb0b6dSopenharmony_ci CHECK_ENV(env); 116433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 116533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 116633eb0b6dSopenharmony_ci 116733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 116833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 116933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 117033eb0b6dSopenharmony_ci 117133eb0b6dSopenharmony_ci *result = nativeValue->IsJSShared(vm) || nativeValue->IsString(vm) || nativeValue->IsNumber() || 117233eb0b6dSopenharmony_ci nativeValue->IsBoolean() || nativeValue->IsUndefined() || nativeValue->IsNull() || 117333eb0b6dSopenharmony_ci nativeValue->IsBigInt(vm); 117433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 117533eb0b6dSopenharmony_ci} 117633eb0b6dSopenharmony_ci 117733eb0b6dSopenharmony_ci// Methods to compare values 117833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_strict_equals(napi_env env, napi_value lhs, napi_value rhs, bool* result) 117933eb0b6dSopenharmony_ci{ 118033eb0b6dSopenharmony_ci CHECK_ENV(env); 118133eb0b6dSopenharmony_ci CHECK_ARG(env, lhs); 118233eb0b6dSopenharmony_ci CHECK_ARG(env, rhs); 118333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 118433eb0b6dSopenharmony_ci 118533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 118633eb0b6dSopenharmony_ci auto nativeLhs = LocalValueFromJsValue(lhs); 118733eb0b6dSopenharmony_ci auto nativeRhs = LocalValueFromJsValue(rhs); 118833eb0b6dSopenharmony_ci *result = nativeLhs->IsStrictEquals(vm, nativeRhs); 118933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 119033eb0b6dSopenharmony_ci} 119133eb0b6dSopenharmony_ci 119233eb0b6dSopenharmony_ci// Methods to work with Functions 119333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_call_function(napi_env env, 119433eb0b6dSopenharmony_ci napi_value recv, 119533eb0b6dSopenharmony_ci napi_value func, 119633eb0b6dSopenharmony_ci size_t argc, 119733eb0b6dSopenharmony_ci const napi_value* argv, 119833eb0b6dSopenharmony_ci napi_value* result) 119933eb0b6dSopenharmony_ci{ 120033eb0b6dSopenharmony_ci CHECK_ENV((env)); 120133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE((env), (reinterpret_cast<NativeEngine*>(env))->lastException_.IsEmpty(), 120233eb0b6dSopenharmony_ci napi_pending_exception); 120333eb0b6dSopenharmony_ci napi_clear_last_error((env)); 120433eb0b6dSopenharmony_ci CHECK_ARG(env, func); 120533eb0b6dSopenharmony_ci if (argc > 0) { 120633eb0b6dSopenharmony_ci CHECK_ARG(env, argv); 120733eb0b6dSopenharmony_ci } 120833eb0b6dSopenharmony_ci 120933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine *>(env)->GetEcmaVm(); 121033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 121133eb0b6dSopenharmony_ci 121233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, reinterpret_cast<panda::JSValueRef *>(func)->IsFunction(vm), napi_function_expected); 121333eb0b6dSopenharmony_ci panda::JSValueRef* thisObj = reinterpret_cast<panda::JSValueRef *>(recv); 121433eb0b6dSopenharmony_ci panda::FunctionRef* function = reinterpret_cast<panda::FunctionRef *>(func); 121533eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 121633eb0b6dSopenharmony_ci int32_t scopeId = OHOS::Ace::ContainerScope::CurrentId(); 121733eb0b6dSopenharmony_ci auto funcInfo = reinterpret_cast<NapiFunctionInfo *>(function->GetData(vm)); 121833eb0b6dSopenharmony_ci if (funcInfo != nullptr) { 121933eb0b6dSopenharmony_ci scopeId = funcInfo->scopeId; 122033eb0b6dSopenharmony_ci } 122133eb0b6dSopenharmony_ci OHOS::Ace::ContainerScope containerScope(scopeId); 122233eb0b6dSopenharmony_ci#endif 122333eb0b6dSopenharmony_ci panda::JSValueRef* value = 122433eb0b6dSopenharmony_ci function->CallForNapi(vm, thisObj, reinterpret_cast<panda::JSValueRef *const*>(argv), argc); 122533eb0b6dSopenharmony_ci // if pending exception, value will be a pointer to JSTaggedValue::Hole. 122633eb0b6dSopenharmony_ci if (UNLIKELY(!NapiStatusValidationCheck(value))) { 122733eb0b6dSopenharmony_ci HILOG_ERROR("pending exception when js function called, print exception info: "); 122833eb0b6dSopenharmony_ci panda::JSNApi::PrintExceptionInfo(vm); 122933eb0b6dSopenharmony_ci result = nullptr; 123033eb0b6dSopenharmony_ci reinterpret_cast<NativeEngine *>(env)->lastException_ = panda::JSNApi::GetUncaughtException(vm); 123133eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_pending_exception); 123233eb0b6dSopenharmony_ci } 123333eb0b6dSopenharmony_ci if (result) { 123433eb0b6dSopenharmony_ci *result = reinterpret_cast<napi_value>(value); 123533eb0b6dSopenharmony_ci } 123633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 123733eb0b6dSopenharmony_ci} 123833eb0b6dSopenharmony_ci 123933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_new_instance(napi_env env, 124033eb0b6dSopenharmony_ci napi_value constructor, 124133eb0b6dSopenharmony_ci size_t argc, 124233eb0b6dSopenharmony_ci const napi_value* argv, 124333eb0b6dSopenharmony_ci napi_value* result) 124433eb0b6dSopenharmony_ci{ 124533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 124633eb0b6dSopenharmony_ci CHECK_ARG(env, constructor); 124733eb0b6dSopenharmony_ci if (argc > 0) { 124833eb0b6dSopenharmony_ci CHECK_ARG(env, argv); 124933eb0b6dSopenharmony_ci } 125033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 125133eb0b6dSopenharmony_ci 125233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 125333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 125433eb0b6dSopenharmony_ci 125533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, reinterpret_cast<panda::JSValueRef*>(constructor)->IsFunction(vm), 125633eb0b6dSopenharmony_ci napi_function_expected); 125733eb0b6dSopenharmony_ci panda::FunctionRef* constructorVal = reinterpret_cast<panda::FunctionRef*>(constructor); 125833eb0b6dSopenharmony_ci panda::JSValueRef* instance = constructorVal->ConstructorOptimize(vm, 125933eb0b6dSopenharmony_ci reinterpret_cast<panda::JSValueRef**>(const_cast<napi_value*>(argv)), argc); 126033eb0b6dSopenharmony_ci if (tryCatch.HasCaught()) { 126133eb0b6dSopenharmony_ci HILOG_ERROR("CreateInstance occur Exception"); 126233eb0b6dSopenharmony_ci *result = nullptr; 126333eb0b6dSopenharmony_ci } else { 126433eb0b6dSopenharmony_ci *result = reinterpret_cast<napi_value>(instance); 126533eb0b6dSopenharmony_ci } 126633eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 126733eb0b6dSopenharmony_ci} 126833eb0b6dSopenharmony_ci 126933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_instanceof(napi_env env, napi_value object, napi_value constructor, bool* result) 127033eb0b6dSopenharmony_ci{ 127133eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 127233eb0b6dSopenharmony_ci CHECK_ARG(env, object); 127333eb0b6dSopenharmony_ci CHECK_ARG(env, constructor); 127433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 127533eb0b6dSopenharmony_ci 127633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 127733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 127833eb0b6dSopenharmony_ci 127933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 128033eb0b6dSopenharmony_ci auto nativeConstructor = LocalValueFromJsValue(constructor); 128133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 128233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeConstructor->IsFunction(vm), napi_function_expected); 128333eb0b6dSopenharmony_ci *result = nativeValue->InstanceOf(vm, nativeConstructor); 128433eb0b6dSopenharmony_ci 128533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 128633eb0b6dSopenharmony_ci} 128733eb0b6dSopenharmony_ci 128833eb0b6dSopenharmony_ci// Methods to work with napi_callbacks 128933eb0b6dSopenharmony_ci// Gets all callback info in a single call. (Ugly, but faster.) 129033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_cb_info(napi_env env, // [in] NAPI environment handle 129133eb0b6dSopenharmony_ci napi_callback_info cbinfo, // [in] Opaque callback-info handle 129233eb0b6dSopenharmony_ci size_t* argc, // [in-out] Specifies the size of the provided argv array 129333eb0b6dSopenharmony_ci // and receives the actual count of args. 129433eb0b6dSopenharmony_ci napi_value* argv, // [out] Array of values 129533eb0b6dSopenharmony_ci napi_value* this_arg, // [out] Receives the JS 'this' arg for the call 129633eb0b6dSopenharmony_ci void** data) // [out] Receives the data pointer for the callback. 129733eb0b6dSopenharmony_ci{ 129833eb0b6dSopenharmony_ci CHECK_ENV(env); 129933eb0b6dSopenharmony_ci CHECK_ARG(env, cbinfo); 130033eb0b6dSopenharmony_ci 130133eb0b6dSopenharmony_ci auto info = reinterpret_cast<panda::JsiRuntimeCallInfo*>(cbinfo); 130233eb0b6dSopenharmony_ci if ((argc != nullptr) && (argv != nullptr)) { 130333eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 130433eb0b6dSopenharmony_ci auto *vm = info->GetVM(); 130533eb0b6dSopenharmony_ci#endif 130633eb0b6dSopenharmony_ci size_t i = 0; 130733eb0b6dSopenharmony_ci if (*argc > 0) { 130833eb0b6dSopenharmony_ci size_t j = static_cast<size_t>(info->GetArgsNumber()); 130933eb0b6dSopenharmony_ci for (; i < j && i < *argc; i++) { 131033eb0b6dSopenharmony_ci panda::Local<panda::JSValueRef> value = info->GetCallArgRef(i); 131133eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 131233eb0b6dSopenharmony_ci FunctionSetContainerId(vm, value); 131333eb0b6dSopenharmony_ci#endif 131433eb0b6dSopenharmony_ci argv[i] = JsValueFromLocalValue(value); 131533eb0b6dSopenharmony_ci } 131633eb0b6dSopenharmony_ci } else { 131733eb0b6dSopenharmony_ci i = static_cast<size_t>(info->GetArgsNumber()); 131833eb0b6dSopenharmony_ci } 131933eb0b6dSopenharmony_ci if (i < *argc) { 132033eb0b6dSopenharmony_ci napi_value undefined = JsValueFromLocalValue( 132133eb0b6dSopenharmony_ci panda::JSValueRef::Undefined(reinterpret_cast<NativeEngine*>(env)->GetEcmaVm())); 132233eb0b6dSopenharmony_ci for (; i < *argc; i++) { 132333eb0b6dSopenharmony_ci argv[i] = undefined; 132433eb0b6dSopenharmony_ci } 132533eb0b6dSopenharmony_ci } 132633eb0b6dSopenharmony_ci } 132733eb0b6dSopenharmony_ci if (argc != nullptr) { 132833eb0b6dSopenharmony_ci *argc = static_cast<size_t>(info->GetArgsNumber()); 132933eb0b6dSopenharmony_ci } 133033eb0b6dSopenharmony_ci if (this_arg != nullptr) { 133133eb0b6dSopenharmony_ci *this_arg = JsValueFromLocalValue(info->GetThisRef()); 133233eb0b6dSopenharmony_ci } 133333eb0b6dSopenharmony_ci if (data != nullptr) { 133433eb0b6dSopenharmony_ci auto funcInfo = static_cast<NapiFunctionInfo*>(info->GetData()); 133533eb0b6dSopenharmony_ci if (funcInfo != nullptr) { 133633eb0b6dSopenharmony_ci *data = funcInfo->data; 133733eb0b6dSopenharmony_ci } 133833eb0b6dSopenharmony_ci } 133933eb0b6dSopenharmony_ci 134033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 134133eb0b6dSopenharmony_ci} 134233eb0b6dSopenharmony_ci 134333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_new_target(napi_env env, napi_callback_info cbinfo, napi_value* result) 134433eb0b6dSopenharmony_ci{ 134533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 134633eb0b6dSopenharmony_ci CHECK_ARG(env, cbinfo); 134733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 134833eb0b6dSopenharmony_ci 134933eb0b6dSopenharmony_ci auto info = reinterpret_cast<panda::JsiRuntimeCallInfo*>(cbinfo); 135033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 135133eb0b6dSopenharmony_ci auto thisVarObj = info->GetThisRef(); 135233eb0b6dSopenharmony_ci#ifdef ENABLE_CONTAINER_SCOPE 135333eb0b6dSopenharmony_ci panda::Local<panda::JSValueRef> newValue = info->GetNewTargetRef(); 135433eb0b6dSopenharmony_ci FunctionSetContainerId(vm, newValue); 135533eb0b6dSopenharmony_ci auto functionVal = newValue; 135633eb0b6dSopenharmony_ci#else 135733eb0b6dSopenharmony_ci auto functionVal = info->GetNewTargetRef(); 135833eb0b6dSopenharmony_ci#endif 135933eb0b6dSopenharmony_ci if (thisVarObj->InstanceOf(vm, functionVal)) { 136033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(functionVal); 136133eb0b6dSopenharmony_ci } else { 136233eb0b6dSopenharmony_ci *result = nullptr; 136333eb0b6dSopenharmony_ci } 136433eb0b6dSopenharmony_ci 136533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 136633eb0b6dSopenharmony_ci} 136733eb0b6dSopenharmony_ci 136833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_define_class(napi_env env, 136933eb0b6dSopenharmony_ci const char* utf8name, 137033eb0b6dSopenharmony_ci size_t length, 137133eb0b6dSopenharmony_ci napi_callback constructor, 137233eb0b6dSopenharmony_ci void* data, 137333eb0b6dSopenharmony_ci size_t property_count, 137433eb0b6dSopenharmony_ci const napi_property_descriptor* properties, 137533eb0b6dSopenharmony_ci napi_value* result) 137633eb0b6dSopenharmony_ci{ 137733eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 137833eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 137933eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, length == NAPI_AUTO_LENGTH || length <= INT_MAX, napi_object_expected); 138033eb0b6dSopenharmony_ci CHECK_ARG(env, constructor); 138133eb0b6dSopenharmony_ci if (property_count > 0) { 138233eb0b6dSopenharmony_ci CHECK_ARG(env, properties); 138333eb0b6dSopenharmony_ci } 138433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 138533eb0b6dSopenharmony_ci 138633eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeCallback>(constructor); 138733eb0b6dSopenharmony_ci auto nativeProperties = reinterpret_cast<const NapiPropertyDescriptor*>(properties); 138833eb0b6dSopenharmony_ci 138933eb0b6dSopenharmony_ci size_t nameLength = std::min(length, strlen(utf8name)); 139033eb0b6dSopenharmony_ci char newName[nameLength + 1]; 139133eb0b6dSopenharmony_ci if (strncpy_s(newName, nameLength + 1, utf8name, nameLength) != EOK) { 139233eb0b6dSopenharmony_ci HILOG_ERROR("napi_define_class strncpy_s failed"); 139333eb0b6dSopenharmony_ci *result = nullptr; 139433eb0b6dSopenharmony_ci } else { 139533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 139633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 139733eb0b6dSopenharmony_ci EscapeLocalScope scope(reinterpret_cast<NativeEngine*>(env)->GetEcmaVm()); 139833eb0b6dSopenharmony_ci auto resultValue = NapiDefineClass(env, newName, callback, data, nativeProperties, property_count); 139933eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(scope.Escape(resultValue)); 140033eb0b6dSopenharmony_ci } 140133eb0b6dSopenharmony_ci 140233eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 140333eb0b6dSopenharmony_ci} 140433eb0b6dSopenharmony_ci 140533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_define_sendable_class(napi_env env, 140633eb0b6dSopenharmony_ci const char* utf8name, 140733eb0b6dSopenharmony_ci size_t length, 140833eb0b6dSopenharmony_ci napi_callback constructor, 140933eb0b6dSopenharmony_ci void* data, 141033eb0b6dSopenharmony_ci size_t property_count, 141133eb0b6dSopenharmony_ci const napi_property_descriptor* properties, 141233eb0b6dSopenharmony_ci napi_value parent, 141333eb0b6dSopenharmony_ci napi_value* result) 141433eb0b6dSopenharmony_ci{ 141533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 141633eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 141733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, length == NAPI_AUTO_LENGTH || length <= INT_MAX, 141833eb0b6dSopenharmony_ci napi_object_expected); 141933eb0b6dSopenharmony_ci CHECK_ARG(env, constructor); 142033eb0b6dSopenharmony_ci if (property_count > 0) { 142133eb0b6dSopenharmony_ci CHECK_ARG(env, properties); 142233eb0b6dSopenharmony_ci } 142333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 142433eb0b6dSopenharmony_ci 142533eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeCallback>(constructor); 142633eb0b6dSopenharmony_ci auto nativeProperties = reinterpret_cast<const NapiPropertyDescriptor*>(properties); 142733eb0b6dSopenharmony_ci 142833eb0b6dSopenharmony_ci size_t nameLength = std::min(length, strlen(utf8name)); 142933eb0b6dSopenharmony_ci char newName[nameLength + 1]; 143033eb0b6dSopenharmony_ci if (strncpy_s(newName, nameLength + 1, utf8name, nameLength) != EOK) { 143133eb0b6dSopenharmony_ci HILOG_ERROR("napi_define_sendable_class strncpy_s failed"); 143233eb0b6dSopenharmony_ci *result = nullptr; 143333eb0b6dSopenharmony_ci } else { 143433eb0b6dSopenharmony_ci EscapeLocalScope scope(reinterpret_cast<NativeEngine*>(env)->GetEcmaVm()); 143533eb0b6dSopenharmony_ci auto resultValue = 143633eb0b6dSopenharmony_ci NapiDefineSendableClass(env, newName, callback, data, nativeProperties, property_count, parent); 143733eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(scope.Escape(resultValue)); 143833eb0b6dSopenharmony_ci } 143933eb0b6dSopenharmony_ci 144033eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 144133eb0b6dSopenharmony_ci} 144233eb0b6dSopenharmony_ci 144333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_sendable_object_with_properties(napi_env env, 144433eb0b6dSopenharmony_ci size_t property_count, 144533eb0b6dSopenharmony_ci const napi_property_descriptor* properties, 144633eb0b6dSopenharmony_ci napi_value* result) 144733eb0b6dSopenharmony_ci{ 144833eb0b6dSopenharmony_ci CHECK_ENV(env); 144933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 145033eb0b6dSopenharmony_ci 145133eb0b6dSopenharmony_ci auto nativeProperties = reinterpret_cast<const NapiPropertyDescriptor*>(properties); 145233eb0b6dSopenharmony_ci auto object = NapiCreateSObjectWithProperties(env, property_count, nativeProperties); 145333eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 145433eb0b6dSopenharmony_ci 145533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 145633eb0b6dSopenharmony_ci} 145733eb0b6dSopenharmony_ci 145833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_map(napi_env env, napi_value* result) 145933eb0b6dSopenharmony_ci{ 146033eb0b6dSopenharmony_ci CHECK_ENV(env); 146133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 146233eb0b6dSopenharmony_ci 146333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 146433eb0b6dSopenharmony_ci Local<panda::ArrayRef> object = panda::MapRef::New(vm); 146533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 146633eb0b6dSopenharmony_ci 146733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 146833eb0b6dSopenharmony_ci} 146933eb0b6dSopenharmony_ci 147033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_sendable_map(napi_env env, napi_value* result) 147133eb0b6dSopenharmony_ci{ 147233eb0b6dSopenharmony_ci CHECK_ENV(env); 147333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 147433eb0b6dSopenharmony_ci 147533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 147633eb0b6dSopenharmony_ci Local<panda::ArrayRef> object = panda::SendableMapRef::New(vm); 147733eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 147833eb0b6dSopenharmony_ci 147933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 148033eb0b6dSopenharmony_ci} 148133eb0b6dSopenharmony_ci 148233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_set_property(napi_env env, napi_value map, napi_value key, napi_value value) 148333eb0b6dSopenharmony_ci{ 148433eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 148533eb0b6dSopenharmony_ci CHECK_ARG(env, map); 148633eb0b6dSopenharmony_ci CHECK_ARG(env, key); 148733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 148833eb0b6dSopenharmony_ci 148933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 149033eb0b6dSopenharmony_ci auto propKey = LocalValueFromJsValue(key); 149133eb0b6dSopenharmony_ci auto propValue = LocalValueFromJsValue(value); 149233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 149333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 149433eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 149533eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 149633eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 149733eb0b6dSopenharmony_ci mapRef->Set(vm, propKey, propValue); 149833eb0b6dSopenharmony_ci } else { 149933eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 150033eb0b6dSopenharmony_ci mapRef->Set(vm, propKey, propValue); 150133eb0b6dSopenharmony_ci } 150233eb0b6dSopenharmony_ci 150333eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 150433eb0b6dSopenharmony_ci} 150533eb0b6dSopenharmony_ci 150633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_set_named_property(napi_env env, 150733eb0b6dSopenharmony_ci napi_value map, 150833eb0b6dSopenharmony_ci const char* utf8name, 150933eb0b6dSopenharmony_ci napi_value value) 151033eb0b6dSopenharmony_ci{ 151133eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 151233eb0b6dSopenharmony_ci CHECK_ARG(env, map); 151333eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 151433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 151533eb0b6dSopenharmony_ci 151633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 151733eb0b6dSopenharmony_ci auto propVal = LocalValueFromJsValue(value); 151833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 151933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 152033eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 152133eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 152233eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 152333eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 152433eb0b6dSopenharmony_ci mapRef->Set(vm, utf8name, propVal); 152533eb0b6dSopenharmony_ci } else { 152633eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 152733eb0b6dSopenharmony_ci mapRef->Set(vm, utf8name, propVal); 152833eb0b6dSopenharmony_ci } 152933eb0b6dSopenharmony_ci 153033eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 153133eb0b6dSopenharmony_ci} 153233eb0b6dSopenharmony_ci 153333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_get_property(napi_env env, napi_value map, napi_value key, napi_value* result) 153433eb0b6dSopenharmony_ci{ 153533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 153633eb0b6dSopenharmony_ci CHECK_ARG(env, map); 153733eb0b6dSopenharmony_ci CHECK_ARG(env, key); 153833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 153933eb0b6dSopenharmony_ci 154033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 154133eb0b6dSopenharmony_ci auto propKey = LocalValueFromJsValue(key); 154233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 154333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 154433eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 154533eb0b6dSopenharmony_ci Local<JSValueRef> value; 154633eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 154733eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 154833eb0b6dSopenharmony_ci value = mapRef->Get(vm, propKey); 154933eb0b6dSopenharmony_ci } else { 155033eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 155133eb0b6dSopenharmony_ci value = mapRef->Get(vm, propKey); 155233eb0b6dSopenharmony_ci } 155333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(value), napi_object_expected); 155433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 155533eb0b6dSopenharmony_ci 155633eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 155733eb0b6dSopenharmony_ci} 155833eb0b6dSopenharmony_ci 155933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_get_named_property(napi_env env, 156033eb0b6dSopenharmony_ci napi_value map, 156133eb0b6dSopenharmony_ci const char* utf8name, 156233eb0b6dSopenharmony_ci napi_value* result) 156333eb0b6dSopenharmony_ci{ 156433eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 156533eb0b6dSopenharmony_ci CHECK_ARG(env, map); 156633eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 156733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 156833eb0b6dSopenharmony_ci 156933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 157033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 157133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 157233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 157333eb0b6dSopenharmony_ci Local<JSValueRef> value; 157433eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 157533eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 157633eb0b6dSopenharmony_ci value = mapRef->Get(vm, utf8name); 157733eb0b6dSopenharmony_ci } else { 157833eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 157933eb0b6dSopenharmony_ci value = mapRef->Get(vm, utf8name); 158033eb0b6dSopenharmony_ci } 158133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(value), napi_object_expected); 158233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 158333eb0b6dSopenharmony_ci 158433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 158533eb0b6dSopenharmony_ci} 158633eb0b6dSopenharmony_ci 158733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_has_property(napi_env env, napi_value map, napi_value key, bool* result) 158833eb0b6dSopenharmony_ci{ 158933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 159033eb0b6dSopenharmony_ci CHECK_ARG(env, map); 159133eb0b6dSopenharmony_ci CHECK_ARG(env, key); 159233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 159333eb0b6dSopenharmony_ci 159433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 159533eb0b6dSopenharmony_ci auto propKey = LocalValueFromJsValue(key); 159633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 159733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 159833eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 159933eb0b6dSopenharmony_ci bool value; 160033eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 160133eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 160233eb0b6dSopenharmony_ci value = mapRef->Has(vm, propKey); 160333eb0b6dSopenharmony_ci } else { 160433eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 160533eb0b6dSopenharmony_ci value = mapRef->Has(vm, propKey); 160633eb0b6dSopenharmony_ci } 160733eb0b6dSopenharmony_ci *result = value; 160833eb0b6dSopenharmony_ci 160933eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 161033eb0b6dSopenharmony_ci} 161133eb0b6dSopenharmony_ci 161233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_has_named_property(napi_env env, napi_value map, const char* utf8name, bool* result) 161333eb0b6dSopenharmony_ci{ 161433eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 161533eb0b6dSopenharmony_ci CHECK_ARG(env, map); 161633eb0b6dSopenharmony_ci CHECK_ARG(env, utf8name); 161733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 161833eb0b6dSopenharmony_ci 161933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 162033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 162133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 162233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 162333eb0b6dSopenharmony_ci bool value; 162433eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 162533eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 162633eb0b6dSopenharmony_ci value = mapRef->Has(vm, utf8name); 162733eb0b6dSopenharmony_ci } else { 162833eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 162933eb0b6dSopenharmony_ci value = mapRef->Has(vm, utf8name); 163033eb0b6dSopenharmony_ci } 163133eb0b6dSopenharmony_ci *result = value; 163233eb0b6dSopenharmony_ci 163333eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 163433eb0b6dSopenharmony_ci} 163533eb0b6dSopenharmony_ci 163633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_delete_property(napi_env env, napi_value map, napi_value key) 163733eb0b6dSopenharmony_ci{ 163833eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 163933eb0b6dSopenharmony_ci CHECK_ARG(env, map); 164033eb0b6dSopenharmony_ci CHECK_ARG(env, key); 164133eb0b6dSopenharmony_ci 164233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 164333eb0b6dSopenharmony_ci auto propKey = LocalValueFromJsValue(key); 164433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 164533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 164633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 164733eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 164833eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 164933eb0b6dSopenharmony_ci mapRef->Delete(vm, propKey); 165033eb0b6dSopenharmony_ci } else { 165133eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 165233eb0b6dSopenharmony_ci mapRef->Delete(vm, propKey); 165333eb0b6dSopenharmony_ci } 165433eb0b6dSopenharmony_ci 165533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 165633eb0b6dSopenharmony_ci} 165733eb0b6dSopenharmony_ci 165833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_clear(napi_env env, napi_value map) 165933eb0b6dSopenharmony_ci{ 166033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 166133eb0b6dSopenharmony_ci CHECK_ARG(env, map); 166233eb0b6dSopenharmony_ci 166333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 166433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 166533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 166633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 166733eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 166833eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 166933eb0b6dSopenharmony_ci mapRef->Clear(vm); 167033eb0b6dSopenharmony_ci } else { 167133eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 167233eb0b6dSopenharmony_ci mapRef->Clear(vm); 167333eb0b6dSopenharmony_ci } 167433eb0b6dSopenharmony_ci 167533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 167633eb0b6dSopenharmony_ci} 167733eb0b6dSopenharmony_ci 167833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_get_size(napi_env env, napi_value map, uint32_t* result) 167933eb0b6dSopenharmony_ci{ 168033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 168133eb0b6dSopenharmony_ci CHECK_ARG(env, map); 168233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 168333eb0b6dSopenharmony_ci 168433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 168533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 168633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 168733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 168833eb0b6dSopenharmony_ci uint32_t value; 168933eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 169033eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 169133eb0b6dSopenharmony_ci value = static_cast<uint32_t>(mapRef->GetSize(vm)); 169233eb0b6dSopenharmony_ci } else { 169333eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 169433eb0b6dSopenharmony_ci value = static_cast<uint32_t>(mapRef->GetSize(vm)); 169533eb0b6dSopenharmony_ci } 169633eb0b6dSopenharmony_ci *result = value; 169733eb0b6dSopenharmony_ci 169833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 169933eb0b6dSopenharmony_ci} 170033eb0b6dSopenharmony_ci 170133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_get_entries(napi_env env, napi_value map, napi_value* result) 170233eb0b6dSopenharmony_ci{ 170333eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 170433eb0b6dSopenharmony_ci CHECK_ARG(env, map); 170533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 170633eb0b6dSopenharmony_ci 170733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 170833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 170933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 171033eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 171133eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 171233eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 171333eb0b6dSopenharmony_ci Local<panda::ArrayRef> arrayVal = mapRef->GetEntries(vm); 171433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 171533eb0b6dSopenharmony_ci } else { 171633eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 171733eb0b6dSopenharmony_ci Local<panda::SendableArrayRef> arrayVal = mapRef->GetEntries(vm); 171833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 171933eb0b6dSopenharmony_ci } 172033eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 172133eb0b6dSopenharmony_ci} 172233eb0b6dSopenharmony_ci 172333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_get_keys(napi_env env, napi_value map, napi_value* result) 172433eb0b6dSopenharmony_ci{ 172533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 172633eb0b6dSopenharmony_ci CHECK_ARG(env, map); 172733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 172833eb0b6dSopenharmony_ci 172933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 173033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 173133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 173233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 173333eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 173433eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 173533eb0b6dSopenharmony_ci Local<panda::ArrayRef> arrayVal = mapRef->GetKeys(vm); 173633eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 173733eb0b6dSopenharmony_ci } else { 173833eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 173933eb0b6dSopenharmony_ci Local<panda::SendableArrayRef> arrayVal = mapRef->GetKeys(vm); 174033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 174133eb0b6dSopenharmony_ci } 174233eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 174333eb0b6dSopenharmony_ci} 174433eb0b6dSopenharmony_ci 174533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_get_values(napi_env env, napi_value map, napi_value* result) 174633eb0b6dSopenharmony_ci{ 174733eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 174833eb0b6dSopenharmony_ci CHECK_ARG(env, map); 174933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 175033eb0b6dSopenharmony_ci 175133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(map); 175233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 175333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 175433eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm), napi_object_expected); 175533eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMap(vm))) { 175633eb0b6dSopenharmony_ci Local<panda::MapRef> mapRef(nativeValue); 175733eb0b6dSopenharmony_ci Local<panda::ArrayRef> arrayVal = mapRef->GetValues(vm); 175833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 175933eb0b6dSopenharmony_ci } else { 176033eb0b6dSopenharmony_ci Local<panda::SendableMapRef> mapRef(nativeValue); 176133eb0b6dSopenharmony_ci Local<panda::SendableArrayRef> arrayVal = mapRef->GetValues(vm); 176233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 176333eb0b6dSopenharmony_ci } 176433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 176533eb0b6dSopenharmony_ci} 176633eb0b6dSopenharmony_ci 176733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_map_iterator_get_next(napi_env env, napi_value iterator, napi_value* result) 176833eb0b6dSopenharmony_ci{ 176933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 177033eb0b6dSopenharmony_ci CHECK_ARG(env, iterator); 177133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 177233eb0b6dSopenharmony_ci 177333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(iterator); 177433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 177533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 177633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsMapIterator(vm) || nativeValue->IsSharedMapIterator(vm), 177733eb0b6dSopenharmony_ci napi_object_expected); 177833eb0b6dSopenharmony_ci Local<panda::JSValueRef> value; 177933eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsMapIterator(vm))) { 178033eb0b6dSopenharmony_ci Local<panda::MapIteratorRef> mapIter(nativeValue); 178133eb0b6dSopenharmony_ci value = mapIter->Next(vm); 178233eb0b6dSopenharmony_ci } else { 178333eb0b6dSopenharmony_ci Local<panda::SendableMapIteratorRef> mapIter(nativeValue); 178433eb0b6dSopenharmony_ci value = mapIter->Next(vm); 178533eb0b6dSopenharmony_ci } 178633eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 178733eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 178833eb0b6dSopenharmony_ci} 178933eb0b6dSopenharmony_ci 179033eb0b6dSopenharmony_ci// Methods to work with external data objects 179133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_wrap(napi_env env, 179233eb0b6dSopenharmony_ci napi_value js_object, 179333eb0b6dSopenharmony_ci void* native_object, 179433eb0b6dSopenharmony_ci napi_finalize finalize_cb, 179533eb0b6dSopenharmony_ci void* finalize_hint, 179633eb0b6dSopenharmony_ci napi_ref* result) 179733eb0b6dSopenharmony_ci{ 179833eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 179933eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 180033eb0b6dSopenharmony_ci CHECK_ARG(env, native_object); 180133eb0b6dSopenharmony_ci CHECK_ARG(env, finalize_cb); 180233eb0b6dSopenharmony_ci 180333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 180433eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeFinalize>(finalize_cb); 180533eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 180633eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 180733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 180833eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, nativeObject); 180933eb0b6dSopenharmony_ci size_t nativeBindingSize = 0; 181033eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference**>(result); 181133eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::GetNapiWrapperString(vm); 181233eb0b6dSopenharmony_ci if (native_object == nullptr && nativeObject->Has(vm, key)) { 181333eb0b6dSopenharmony_ci Local<panda::ObjectRef> wrapper = nativeObject->Get(vm, key); 181433eb0b6dSopenharmony_ci auto ref = reinterpret_cast<NativeReference*>(wrapper->GetNativePointerField(vm, 0)); 181533eb0b6dSopenharmony_ci // Try to remove native pointer from ArrayDataList 181633eb0b6dSopenharmony_ci wrapper->SetNativePointerField(vm, 0, nullptr, nullptr, nullptr, nativeBindingSize); 181733eb0b6dSopenharmony_ci nativeObject->Delete(vm, key); 181833eb0b6dSopenharmony_ci delete ref; 181933eb0b6dSopenharmony_ci } else { 182033eb0b6dSopenharmony_ci Local<panda::ObjectRef> object = panda::ObjectRef::New(vm); 182133eb0b6dSopenharmony_ci NativeReference* ref = nullptr; 182233eb0b6dSopenharmony_ci if (reference != nullptr) { 182333eb0b6dSopenharmony_ci ref = engine->CreateReference(js_object, 1, false, callback, native_object, finalize_hint); 182433eb0b6dSopenharmony_ci *reference = ref; 182533eb0b6dSopenharmony_ci } else { 182633eb0b6dSopenharmony_ci ref = engine->CreateReference(js_object, 0, true, callback, native_object, finalize_hint); 182733eb0b6dSopenharmony_ci } 182833eb0b6dSopenharmony_ci object->SetNativePointerFieldCount(vm, 1); 182933eb0b6dSopenharmony_ci object->SetNativePointerField(vm, 0, ref, nullptr, nullptr, nativeBindingSize); 183033eb0b6dSopenharmony_ci PropertyAttribute attr(object, true, false, true); 183133eb0b6dSopenharmony_ci nativeObject->DefineProperty(vm, key, attr); 183233eb0b6dSopenharmony_ci } 183333eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 183433eb0b6dSopenharmony_ci} 183533eb0b6dSopenharmony_ci 183633eb0b6dSopenharmony_ci// Ensure thread safety! Async finalizer will be called on the async thread. 183733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_wrap_async_finalizer(napi_env env, 183833eb0b6dSopenharmony_ci napi_value js_object, 183933eb0b6dSopenharmony_ci void* native_object, 184033eb0b6dSopenharmony_ci napi_finalize finalize_cb, 184133eb0b6dSopenharmony_ci void* finalize_hint, 184233eb0b6dSopenharmony_ci napi_ref* result, 184333eb0b6dSopenharmony_ci size_t native_binding_size) 184433eb0b6dSopenharmony_ci{ 184533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 184633eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 184733eb0b6dSopenharmony_ci CHECK_ARG(env, native_object); 184833eb0b6dSopenharmony_ci 184933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 185033eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeFinalize>(finalize_cb); 185133eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 185233eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 185333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 185433eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, nativeObject); 185533eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference**>(result); 185633eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::GetNapiWrapperString(vm); 185733eb0b6dSopenharmony_ci if (native_object == nullptr && nativeObject->Has(vm, key)) { 185833eb0b6dSopenharmony_ci Local<panda::ObjectRef> wrapper = nativeObject->Get(vm, key); 185933eb0b6dSopenharmony_ci auto ref = reinterpret_cast<NativeReference*>(wrapper->GetNativePointerField(vm, 0)); 186033eb0b6dSopenharmony_ci // Try to remove native pointer from ArrayDataList 186133eb0b6dSopenharmony_ci wrapper->SetNativePointerField(vm, 0, nullptr, nullptr, nullptr, native_binding_size); 186233eb0b6dSopenharmony_ci nativeObject->Delete(vm, key); 186333eb0b6dSopenharmony_ci delete ref; 186433eb0b6dSopenharmony_ci } else { 186533eb0b6dSopenharmony_ci Local<panda::ObjectRef> object = panda::ObjectRef::New(vm); 186633eb0b6dSopenharmony_ci NativeReference* ref = nullptr; 186733eb0b6dSopenharmony_ci if (reference != nullptr) { 186833eb0b6dSopenharmony_ci ref = engine->CreateAsyncReference(js_object, 1, false, callback, native_object, finalize_hint); 186933eb0b6dSopenharmony_ci *reference = ref; 187033eb0b6dSopenharmony_ci } else { 187133eb0b6dSopenharmony_ci ref = engine->CreateAsyncReference(js_object, 0, true, callback, native_object, finalize_hint); 187233eb0b6dSopenharmony_ci } 187333eb0b6dSopenharmony_ci object->SetNativePointerFieldCount(vm, 1); 187433eb0b6dSopenharmony_ci object->SetNativePointerField(vm, 0, ref, nullptr, nullptr, native_binding_size); 187533eb0b6dSopenharmony_ci PropertyAttribute attr(object, true, false, true); 187633eb0b6dSopenharmony_ci nativeObject->DefineProperty(vm, key, attr); 187733eb0b6dSopenharmony_ci } 187833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 187933eb0b6dSopenharmony_ci} 188033eb0b6dSopenharmony_ci 188133eb0b6dSopenharmony_ci// Methods to work with external data objects 188233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_wrap_with_size(napi_env env, 188333eb0b6dSopenharmony_ci napi_value js_object, 188433eb0b6dSopenharmony_ci void* native_object, 188533eb0b6dSopenharmony_ci napi_finalize finalize_cb, 188633eb0b6dSopenharmony_ci void* finalize_hint, 188733eb0b6dSopenharmony_ci napi_ref* result, 188833eb0b6dSopenharmony_ci size_t native_binding_size) 188933eb0b6dSopenharmony_ci{ 189033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 189133eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 189233eb0b6dSopenharmony_ci CHECK_ARG(env, native_object); 189333eb0b6dSopenharmony_ci 189433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 189533eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeFinalize>(finalize_cb); 189633eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 189733eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 189833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 189933eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, nativeObject); 190033eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference**>(result); 190133eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::GetNapiWrapperString(vm); 190233eb0b6dSopenharmony_ci if (native_object == nullptr && nativeObject->Has(vm, key)) { 190333eb0b6dSopenharmony_ci Local<panda::ObjectRef> wrapper = nativeObject->Get(vm, key); 190433eb0b6dSopenharmony_ci auto ref = reinterpret_cast<NativeReference*>(wrapper->GetNativePointerField(vm, 0)); 190533eb0b6dSopenharmony_ci // Try to remove native pointer from ArrayDataList 190633eb0b6dSopenharmony_ci wrapper->SetNativePointerField(vm, 0, nullptr, nullptr, nullptr, native_binding_size); 190733eb0b6dSopenharmony_ci nativeObject->Delete(vm, key); 190833eb0b6dSopenharmony_ci delete ref; 190933eb0b6dSopenharmony_ci } else { 191033eb0b6dSopenharmony_ci Local<panda::ObjectRef> object = panda::ObjectRef::New(vm); 191133eb0b6dSopenharmony_ci NativeReference* ref = nullptr; 191233eb0b6dSopenharmony_ci if (reference != nullptr) { 191333eb0b6dSopenharmony_ci ref = engine->CreateReference(js_object, 1, false, callback, native_object, finalize_hint, 191433eb0b6dSopenharmony_ci native_binding_size); 191533eb0b6dSopenharmony_ci *reference = ref; 191633eb0b6dSopenharmony_ci } else { 191733eb0b6dSopenharmony_ci ref = engine->CreateReference(js_object, 0, true, callback, native_object, finalize_hint, 191833eb0b6dSopenharmony_ci native_binding_size); 191933eb0b6dSopenharmony_ci } 192033eb0b6dSopenharmony_ci object->SetNativePointerFieldCount(vm, 1); 192133eb0b6dSopenharmony_ci object->SetNativePointerField(vm, 0, ref, nullptr, nullptr, native_binding_size); 192233eb0b6dSopenharmony_ci PropertyAttribute attr(object, true, false, true); 192333eb0b6dSopenharmony_ci nativeObject->DefineProperty(vm, key, attr); 192433eb0b6dSopenharmony_ci } 192533eb0b6dSopenharmony_ci 192633eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 192733eb0b6dSopenharmony_ci} 192833eb0b6dSopenharmony_ci 192933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_unwrap(napi_env env, napi_value js_object, void** result) 193033eb0b6dSopenharmony_ci{ 193133eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 193233eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 193333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 193433eb0b6dSopenharmony_ci 193533eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 193633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 193733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 193833eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, nativeObject); 193933eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::GetNapiWrapperString(vm); 194033eb0b6dSopenharmony_ci Local<panda::JSValueRef> val = nativeObject->Get(vm, key); 194133eb0b6dSopenharmony_ci *result = nullptr; 194233eb0b6dSopenharmony_ci if (val->IsObject(vm)) { 194333eb0b6dSopenharmony_ci Local<panda::ObjectRef> ext(val); 194433eb0b6dSopenharmony_ci auto ref = reinterpret_cast<NativeReference*>(ext->GetNativePointerField(vm, 0)); 194533eb0b6dSopenharmony_ci *result = ref != nullptr ? ref->GetData() : nullptr; 194633eb0b6dSopenharmony_ci } 194733eb0b6dSopenharmony_ci 194833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 194933eb0b6dSopenharmony_ci} 195033eb0b6dSopenharmony_ci 195133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_remove_wrap(napi_env env, napi_value js_object, void** result) 195233eb0b6dSopenharmony_ci{ 195333eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 195433eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 195533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 195633eb0b6dSopenharmony_ci 195733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 195833eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 195933eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 196033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 196133eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, nativeObject); 196233eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::GetNapiWrapperString(vm); 196333eb0b6dSopenharmony_ci Local<panda::JSValueRef> val = nativeObject->Get(vm, key); 196433eb0b6dSopenharmony_ci *result = nullptr; 196533eb0b6dSopenharmony_ci if (val->IsObject(vm)) { 196633eb0b6dSopenharmony_ci Local<panda::ObjectRef> ext(val); 196733eb0b6dSopenharmony_ci auto ref = reinterpret_cast<NativeReference*>(ext->GetNativePointerField(vm, 0)); 196833eb0b6dSopenharmony_ci *result = ref != nullptr ? ref->GetData() : nullptr; 196933eb0b6dSopenharmony_ci } 197033eb0b6dSopenharmony_ci 197133eb0b6dSopenharmony_ci size_t nativeBindingSize = 0; 197233eb0b6dSopenharmony_ci if (nativeObject->Has(vm, key)) { 197333eb0b6dSopenharmony_ci Local<panda::ObjectRef> wrapper = val; 197433eb0b6dSopenharmony_ci auto ref = reinterpret_cast<NativeReference*>(wrapper->GetNativePointerField(vm, 0)); 197533eb0b6dSopenharmony_ci nativeObject->Delete(vm, key); 197633eb0b6dSopenharmony_ci delete ref; 197733eb0b6dSopenharmony_ci } else { 197833eb0b6dSopenharmony_ci Local<panda::ObjectRef> object = panda::ObjectRef::New(vm); 197933eb0b6dSopenharmony_ci NativeReference* ref = nullptr; 198033eb0b6dSopenharmony_ci ref = engine->CreateReference(js_object, 0, true, nullptr, nullptr, nullptr); 198133eb0b6dSopenharmony_ci object->SetNativePointerFieldCount(vm, 1); 198233eb0b6dSopenharmony_ci object->SetNativePointerField(vm, 0, ref, nullptr, nullptr, nativeBindingSize); 198333eb0b6dSopenharmony_ci PropertyAttribute attr(object, true, false, true); 198433eb0b6dSopenharmony_ci nativeObject->DefineProperty(vm, key, attr); 198533eb0b6dSopenharmony_ci } 198633eb0b6dSopenharmony_ci 198733eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 198833eb0b6dSopenharmony_ci} 198933eb0b6dSopenharmony_ci 199033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_wrap_sendable(napi_env env, 199133eb0b6dSopenharmony_ci napi_value js_object, 199233eb0b6dSopenharmony_ci void* native_object, 199333eb0b6dSopenharmony_ci napi_finalize finalize_cb, 199433eb0b6dSopenharmony_ci void* finalize_hint) 199533eb0b6dSopenharmony_ci{ 199633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 199733eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 199833eb0b6dSopenharmony_ci CHECK_ARG(env, native_object); 199933eb0b6dSopenharmony_ci 200033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 200133eb0b6dSopenharmony_ci auto callback = reinterpret_cast<panda::NativePointerCallback>(finalize_cb); 200233eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 200333eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 200433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 200533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsSendableObject(vm), napi_object_expected); 200633eb0b6dSopenharmony_ci Local<ObjectRef> nativeObject(nativeValue); 200733eb0b6dSopenharmony_ci nativeObject->SetNativePointerFieldCount(vm, 1); 200833eb0b6dSopenharmony_ci nativeObject->SetNativePointerField(vm, 0, native_object, callback, finalize_hint); 200933eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 201033eb0b6dSopenharmony_ci} 201133eb0b6dSopenharmony_ci 201233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_wrap_sendable_with_size(napi_env env, 201333eb0b6dSopenharmony_ci napi_value js_object, 201433eb0b6dSopenharmony_ci void* native_object, 201533eb0b6dSopenharmony_ci napi_finalize finalize_cb, 201633eb0b6dSopenharmony_ci void* finalize_hint, 201733eb0b6dSopenharmony_ci size_t native_binding_size) 201833eb0b6dSopenharmony_ci{ 201933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 202033eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 202133eb0b6dSopenharmony_ci CHECK_ARG(env, native_object); 202233eb0b6dSopenharmony_ci 202333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 202433eb0b6dSopenharmony_ci auto callback = reinterpret_cast<panda::NativePointerCallback>(finalize_cb); 202533eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 202633eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 202733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 202833eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsSendableObject(vm), napi_object_expected); 202933eb0b6dSopenharmony_ci Local<ObjectRef> nativeObject(nativeValue); 203033eb0b6dSopenharmony_ci nativeObject->SetNativePointerFieldCount(vm, 1); 203133eb0b6dSopenharmony_ci nativeObject->SetNativePointerField(vm, 0, native_object, callback, finalize_hint, native_binding_size); 203233eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 203333eb0b6dSopenharmony_ci} 203433eb0b6dSopenharmony_ci 203533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_unwrap_sendable(napi_env env, napi_value js_object, void** result) 203633eb0b6dSopenharmony_ci{ 203733eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 203833eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 203933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 204033eb0b6dSopenharmony_ci 204133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 204233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 204333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 204433eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsSendableObject(vm), napi_object_expected); 204533eb0b6dSopenharmony_ci Local<ObjectRef> nativeObject(nativeValue); 204633eb0b6dSopenharmony_ci void* val = nativeObject->GetNativePointerField(vm, 0); 204733eb0b6dSopenharmony_ci *result = val; 204833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 204933eb0b6dSopenharmony_ci} 205033eb0b6dSopenharmony_ci 205133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_remove_wrap_sendable(napi_env env, napi_value js_object, void** result) 205233eb0b6dSopenharmony_ci{ 205333eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 205433eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 205533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 205633eb0b6dSopenharmony_ci 205733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 205833eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 205933eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 206033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 206133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsSendableObject(vm), napi_object_expected); 206233eb0b6dSopenharmony_ci Local<ObjectRef> nativeObject(nativeValue); 206333eb0b6dSopenharmony_ci void* val = nativeObject->GetNativePointerField(vm, 0); 206433eb0b6dSopenharmony_ci *result = val; 206533eb0b6dSopenharmony_ci nativeObject->SetNativePointerField(vm, 0, nullptr, nullptr, nullptr); 206633eb0b6dSopenharmony_ci 206733eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 206833eb0b6dSopenharmony_ci} 206933eb0b6dSopenharmony_ci 207033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_external( 207133eb0b6dSopenharmony_ci napi_env env, void* data, napi_finalize finalize_cb, void* finalize_hint, napi_value* result) 207233eb0b6dSopenharmony_ci{ 207333eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 207433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 207533eb0b6dSopenharmony_ci 207633eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 207733eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 207833eb0b6dSopenharmony_ci auto callback = reinterpret_cast<panda::NativePointerCallback>(finalize_cb); 207933eb0b6dSopenharmony_ci Local<panda::NativePointerRef> object = panda::NativePointerRef::New(vm, data, callback, finalize_hint, 0); 208033eb0b6dSopenharmony_ci 208133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 208233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 208333eb0b6dSopenharmony_ci} 208433eb0b6dSopenharmony_ci 208533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_external_with_size(napi_env env, 208633eb0b6dSopenharmony_ci void* data, 208733eb0b6dSopenharmony_ci napi_finalize finalize_cb, 208833eb0b6dSopenharmony_ci void* finalize_hint, 208933eb0b6dSopenharmony_ci napi_value* result, 209033eb0b6dSopenharmony_ci size_t native_binding_size) 209133eb0b6dSopenharmony_ci{ 209233eb0b6dSopenharmony_ci CHECK_ENV(env); 209333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 209433eb0b6dSopenharmony_ci 209533eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 209633eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 209733eb0b6dSopenharmony_ci auto callback = reinterpret_cast<panda::NativePointerCallback>(finalize_cb); 209833eb0b6dSopenharmony_ci Local<panda::NativePointerRef> object = 209933eb0b6dSopenharmony_ci panda::NativePointerRef::New(vm, data, callback, finalize_hint, native_binding_size); 210033eb0b6dSopenharmony_ci 210133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 210233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 210333eb0b6dSopenharmony_ci} 210433eb0b6dSopenharmony_ci 210533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_external(napi_env env, napi_value value, void** result) 210633eb0b6dSopenharmony_ci{ 210733eb0b6dSopenharmony_ci CHECK_ENV(env); 210833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 210933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 211033eb0b6dSopenharmony_ci 211133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 211233eb0b6dSopenharmony_ci bool isNativePointer = false; 211333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 211433eb0b6dSopenharmony_ci void* ret = nativeValue->GetNativePointerValue(vm, isNativePointer); 211533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isNativePointer, napi_object_expected); 211633eb0b6dSopenharmony_ci *result = ret; 211733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 211833eb0b6dSopenharmony_ci} 211933eb0b6dSopenharmony_ci 212033eb0b6dSopenharmony_ci// Methods to control object lifespan 212133eb0b6dSopenharmony_ci// Set initial_refcount to 0 for a weak reference, >0 for a strong reference. 212233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_reference(napi_env env, 212333eb0b6dSopenharmony_ci napi_value value, 212433eb0b6dSopenharmony_ci uint32_t initial_refcount, 212533eb0b6dSopenharmony_ci napi_ref* result) 212633eb0b6dSopenharmony_ci{ 212733eb0b6dSopenharmony_ci CHECK_ENV(env); 212833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 212933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 213033eb0b6dSopenharmony_ci auto engine = reinterpret_cast<ArkNativeEngine*>(env); 213133eb0b6dSopenharmony_ci auto ref = new ArkNativeReference(engine, value, initial_refcount); 213233eb0b6dSopenharmony_ci 213333eb0b6dSopenharmony_ci *result = reinterpret_cast<napi_ref>(ref); 213433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 213533eb0b6dSopenharmony_ci} 213633eb0b6dSopenharmony_ci 213733eb0b6dSopenharmony_ci// Deletes a reference. The referenced value is released, and may 213833eb0b6dSopenharmony_ci// be GC'd unless there are other references to it. 213933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref) 214033eb0b6dSopenharmony_ci{ 214133eb0b6dSopenharmony_ci CHECK_ENV(env); 214233eb0b6dSopenharmony_ci CHECK_ARG(env, ref); 214333eb0b6dSopenharmony_ci 214433eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference*>(ref); 214533eb0b6dSopenharmony_ci uint32_t refCount = reference->GetRefCount(); 214633eb0b6dSopenharmony_ci if (refCount > 0 || reference->GetFinalRun()) { 214733eb0b6dSopenharmony_ci delete reference; 214833eb0b6dSopenharmony_ci reference = nullptr; 214933eb0b6dSopenharmony_ci } else { 215033eb0b6dSopenharmony_ci reference->SetDeleteSelf(); 215133eb0b6dSopenharmony_ci } 215233eb0b6dSopenharmony_ci 215333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 215433eb0b6dSopenharmony_ci} 215533eb0b6dSopenharmony_ci 215633eb0b6dSopenharmony_ci// Increments the reference count, optionally returning the resulting count. 215733eb0b6dSopenharmony_ci// After this call the reference will be a strong reference because its 215833eb0b6dSopenharmony_ci// refcount is >0, and the referenced object is effectively "pinned". 215933eb0b6dSopenharmony_ci// Calling this when the refcount is 0 and the object is unavailable 216033eb0b6dSopenharmony_ci// results in an error. 216133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_reference_ref(napi_env env, napi_ref ref, uint32_t* result) 216233eb0b6dSopenharmony_ci{ 216333eb0b6dSopenharmony_ci CHECK_ENV(env); 216433eb0b6dSopenharmony_ci CHECK_ARG(env, ref); 216533eb0b6dSopenharmony_ci 216633eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference*>(ref); 216733eb0b6dSopenharmony_ci uint32_t refCount = reference->Ref(); 216833eb0b6dSopenharmony_ci 216933eb0b6dSopenharmony_ci if (result) { 217033eb0b6dSopenharmony_ci *result = refCount; 217133eb0b6dSopenharmony_ci } 217233eb0b6dSopenharmony_ci 217333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 217433eb0b6dSopenharmony_ci} 217533eb0b6dSopenharmony_ci 217633eb0b6dSopenharmony_ci// Decrements the reference count, optionally returning the resulting count. 217733eb0b6dSopenharmony_ci// If the result is 0 the reference is now weak and the object may be GC'd 217833eb0b6dSopenharmony_ci// at any time if there are no other references. Calling this when the 217933eb0b6dSopenharmony_ci// refcount is already 0 results in an error. 218033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_reference_unref(napi_env env, napi_ref ref, uint32_t* result) 218133eb0b6dSopenharmony_ci{ 218233eb0b6dSopenharmony_ci CHECK_ENV(env); 218333eb0b6dSopenharmony_ci CHECK_ARG(env, ref); 218433eb0b6dSopenharmony_ci 218533eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference*>(ref); 218633eb0b6dSopenharmony_ci uint32_t unrefCount = reference->Unref(); 218733eb0b6dSopenharmony_ci 218833eb0b6dSopenharmony_ci if (result) { 218933eb0b6dSopenharmony_ci *result = unrefCount; 219033eb0b6dSopenharmony_ci } 219133eb0b6dSopenharmony_ci 219233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 219333eb0b6dSopenharmony_ci} 219433eb0b6dSopenharmony_ci 219533eb0b6dSopenharmony_ci// Attempts to get a referenced value. If the reference is weak, 219633eb0b6dSopenharmony_ci// the value might no longer be available, in that case the call 219733eb0b6dSopenharmony_ci// is still successful but the result is nullptr. 219833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value* result) 219933eb0b6dSopenharmony_ci{ 220033eb0b6dSopenharmony_ci CHECK_ENV(env); 220133eb0b6dSopenharmony_ci CHECK_ARG(env, ref); 220233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 220333eb0b6dSopenharmony_ci 220433eb0b6dSopenharmony_ci auto reference = reinterpret_cast<NativeReference*>(ref); 220533eb0b6dSopenharmony_ci NativeEngine* engine = reinterpret_cast<NativeEngine*>(env); 220633eb0b6dSopenharmony_ci 220733eb0b6dSopenharmony_ci *result = reference->Get(engine); 220833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 220933eb0b6dSopenharmony_ci} 221033eb0b6dSopenharmony_ci 221133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_open_handle_scope(napi_env env, napi_handle_scope* result) 221233eb0b6dSopenharmony_ci{ 221333eb0b6dSopenharmony_ci CHECK_ENV(env); 221433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 221533eb0b6dSopenharmony_ci 221633eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 221733eb0b6dSopenharmony_ci *result = HandleScopeToNapiHandleScope(new HandleScopeWrapper(engine)); 221833eb0b6dSopenharmony_ci engine->openHandleScopes_++; 221933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 222033eb0b6dSopenharmony_ci} 222133eb0b6dSopenharmony_ci 222233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_close_handle_scope(napi_env env, napi_handle_scope scope) 222333eb0b6dSopenharmony_ci{ 222433eb0b6dSopenharmony_ci CHECK_ENV(env); 222533eb0b6dSopenharmony_ci CHECK_ARG(env, scope); 222633eb0b6dSopenharmony_ci 222733eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 222833eb0b6dSopenharmony_ci if (engine->openHandleScopes_ == 0) { 222933eb0b6dSopenharmony_ci return napi_handle_scope_mismatch; 223033eb0b6dSopenharmony_ci } 223133eb0b6dSopenharmony_ci 223233eb0b6dSopenharmony_ci engine->openHandleScopes_--; 223333eb0b6dSopenharmony_ci delete NapiHandleScopeToHandleScope(scope); 223433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 223533eb0b6dSopenharmony_ci} 223633eb0b6dSopenharmony_ci 223733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_open_escapable_handle_scope(napi_env env, napi_escapable_handle_scope* result) 223833eb0b6dSopenharmony_ci{ 223933eb0b6dSopenharmony_ci CHECK_ENV(env); 224033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 224133eb0b6dSopenharmony_ci 224233eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 224333eb0b6dSopenharmony_ci *result = EscapableHandleScopeToNapiEscapableHandleScope(new EscapableHandleScopeWrapper(engine)); 224433eb0b6dSopenharmony_ci engine->openHandleScopes_++; 224533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 224633eb0b6dSopenharmony_ci} 224733eb0b6dSopenharmony_ci 224833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_close_escapable_handle_scope(napi_env env, napi_escapable_handle_scope scope) 224933eb0b6dSopenharmony_ci{ 225033eb0b6dSopenharmony_ci CHECK_ENV(env); 225133eb0b6dSopenharmony_ci CHECK_ARG(env, scope); 225233eb0b6dSopenharmony_ci 225333eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 225433eb0b6dSopenharmony_ci if (engine->openHandleScopes_ == 0) { 225533eb0b6dSopenharmony_ci return napi_handle_scope_mismatch; 225633eb0b6dSopenharmony_ci } 225733eb0b6dSopenharmony_ci 225833eb0b6dSopenharmony_ci engine->openHandleScopes_--; 225933eb0b6dSopenharmony_ci delete NapiEscapableHandleScopeToEscapableHandleScope(scope); 226033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 226133eb0b6dSopenharmony_ci} 226233eb0b6dSopenharmony_ci 226333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_escape_handle(napi_env env, 226433eb0b6dSopenharmony_ci napi_escapable_handle_scope scope, 226533eb0b6dSopenharmony_ci napi_value escapee, 226633eb0b6dSopenharmony_ci napi_value* result) 226733eb0b6dSopenharmony_ci{ 226833eb0b6dSopenharmony_ci CHECK_ENV(env); 226933eb0b6dSopenharmony_ci CHECK_ARG(env, scope); 227033eb0b6dSopenharmony_ci CHECK_ARG(env, escapee); 227133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 227233eb0b6dSopenharmony_ci 227333eb0b6dSopenharmony_ci auto s = NapiEscapableHandleScopeToEscapableHandleScope(scope); 227433eb0b6dSopenharmony_ci if (!s->IsEscapeCalled()) { 227533eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(s->Escape(LocalValueFromJsValue(escapee))); 227633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 227733eb0b6dSopenharmony_ci } 227833eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_escape_called_twice); 227933eb0b6dSopenharmony_ci} 228033eb0b6dSopenharmony_ci 228133eb0b6dSopenharmony_ci// Methods to support error handling 228233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error) 228333eb0b6dSopenharmony_ci{ 228433eb0b6dSopenharmony_ci CHECK_ENV(env); 228533eb0b6dSopenharmony_ci CHECK_ARG(env, error); 228633eb0b6dSopenharmony_ci 228733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(error); 228833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 228933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 229033eb0b6dSopenharmony_ci 229133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsError(vm), napi_invalid_arg); 229233eb0b6dSopenharmony_ci panda::JSNApi::ThrowException(vm, nativeValue); 229333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 229433eb0b6dSopenharmony_ci} 229533eb0b6dSopenharmony_ci 229633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_throw_error(napi_env env, const char* code, const char* msg) 229733eb0b6dSopenharmony_ci{ 229833eb0b6dSopenharmony_ci CHECK_ENV(env); 229933eb0b6dSopenharmony_ci CHECK_ARG(env, msg); 230033eb0b6dSopenharmony_ci 230133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 230233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 230333eb0b6dSopenharmony_ci Local<panda::JSValueRef> error(panda::JSValueRef::Undefined(vm)); 230433eb0b6dSopenharmony_ci error = panda::Exception::Error(vm, StringRef::NewFromUtf8(vm, msg)); 230533eb0b6dSopenharmony_ci if (code != nullptr) { 230633eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeKey = panda::StringRef::NewFromUtf8(vm, "code"); 230733eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeValue = panda::StringRef::NewFromUtf8(vm, code); 230833eb0b6dSopenharmony_ci Local<panda::ObjectRef> errorObj(error); 230933eb0b6dSopenharmony_ci errorObj->Set(vm, codeKey, codeValue); 231033eb0b6dSopenharmony_ci } 231133eb0b6dSopenharmony_ci panda::JSNApi::ThrowException(vm, error); 231233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 231333eb0b6dSopenharmony_ci} 231433eb0b6dSopenharmony_ci 231533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_throw_type_error(napi_env env, const char* code, const char* msg) 231633eb0b6dSopenharmony_ci{ 231733eb0b6dSopenharmony_ci CHECK_ENV(env); 231833eb0b6dSopenharmony_ci CHECK_ARG(env, msg); 231933eb0b6dSopenharmony_ci 232033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 232133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 232233eb0b6dSopenharmony_ci Local<panda::JSValueRef> error(panda::JSValueRef::Undefined(vm)); 232333eb0b6dSopenharmony_ci error = panda::Exception::TypeError(vm, StringRef::NewFromUtf8(vm, msg)); 232433eb0b6dSopenharmony_ci if (code != nullptr) { 232533eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeKey = panda::StringRef::NewFromUtf8(vm, "code"); 232633eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeValue = panda::StringRef::NewFromUtf8(vm, code); 232733eb0b6dSopenharmony_ci Local<panda::ObjectRef> errorObj(error); 232833eb0b6dSopenharmony_ci errorObj->Set(vm, codeKey, codeValue); 232933eb0b6dSopenharmony_ci } 233033eb0b6dSopenharmony_ci panda::JSNApi::ThrowException(vm, error); 233133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 233233eb0b6dSopenharmony_ci} 233333eb0b6dSopenharmony_ci 233433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_throw_range_error(napi_env env, const char* code, const char* msg) 233533eb0b6dSopenharmony_ci{ 233633eb0b6dSopenharmony_ci CHECK_ENV(env); 233733eb0b6dSopenharmony_ci CHECK_ARG(env, msg); 233833eb0b6dSopenharmony_ci 233933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 234033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 234133eb0b6dSopenharmony_ci Local<panda::JSValueRef> error(panda::JSValueRef::Undefined(vm)); 234233eb0b6dSopenharmony_ci error = panda::Exception::RangeError(vm, StringRef::NewFromUtf8(vm, msg)); 234333eb0b6dSopenharmony_ci if (code != nullptr) { 234433eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeKey = panda::StringRef::NewFromUtf8(vm, "code"); 234533eb0b6dSopenharmony_ci Local<panda::JSValueRef> codeValue = panda::StringRef::NewFromUtf8(vm, code); 234633eb0b6dSopenharmony_ci Local<panda::ObjectRef> errorObj(error); 234733eb0b6dSopenharmony_ci errorObj->Set(vm, codeKey, codeValue); 234833eb0b6dSopenharmony_ci } 234933eb0b6dSopenharmony_ci panda::JSNApi::ThrowException(vm, error); 235033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 235133eb0b6dSopenharmony_ci} 235233eb0b6dSopenharmony_ci 235333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_error(napi_env env, napi_value value, bool* result) 235433eb0b6dSopenharmony_ci{ 235533eb0b6dSopenharmony_ci CHECK_ENV(env); 235633eb0b6dSopenharmony_ci CHECK_ARG(env, value); 235733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 235833eb0b6dSopenharmony_ci 235933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 236033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 236133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 236233eb0b6dSopenharmony_ci *result = nativeValue->IsError(vm); 236333eb0b6dSopenharmony_ci 236433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 236533eb0b6dSopenharmony_ci} 236633eb0b6dSopenharmony_ci 236733eb0b6dSopenharmony_ci// Methods to support catching exceptions 236833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result) 236933eb0b6dSopenharmony_ci{ 237033eb0b6dSopenharmony_ci CHECK_ENV(env); 237133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 237233eb0b6dSopenharmony_ci 237333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 237433eb0b6dSopenharmony_ci *result = panda::JSNApi::HasPendingException(vm); 237533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 237633eb0b6dSopenharmony_ci} 237733eb0b6dSopenharmony_ci 237833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env, napi_value* result) 237933eb0b6dSopenharmony_ci{ 238033eb0b6dSopenharmony_ci CHECK_ENV(env); 238133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 238233eb0b6dSopenharmony_ci 238333eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 238433eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 238533eb0b6dSopenharmony_ci engine->lastException_.Empty(); 238633eb0b6dSopenharmony_ci Local<panda::ObjectRef> exception = panda::JSNApi::GetAndClearUncaughtException(vm); 238733eb0b6dSopenharmony_ci if (!exception.IsNull()) { 238833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(exception); 238933eb0b6dSopenharmony_ci } 239033eb0b6dSopenharmony_ci 239133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 239233eb0b6dSopenharmony_ci} 239333eb0b6dSopenharmony_ci 239433eb0b6dSopenharmony_ci// Methods to work with array buffers and typed arrays 239533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result) 239633eb0b6dSopenharmony_ci{ 239733eb0b6dSopenharmony_ci CHECK_ENV(env); 239833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 239933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 240033eb0b6dSopenharmony_ci 240133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 240233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 240333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 240433eb0b6dSopenharmony_ci 240533eb0b6dSopenharmony_ci *result = nativeValue->IsArrayBuffer(vm) || nativeValue->IsSendableArrayBuffer(vm); 240633eb0b6dSopenharmony_ci 240733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 240833eb0b6dSopenharmony_ci} 240933eb0b6dSopenharmony_ci 241033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env, size_t byte_length, void** data, napi_value* result) 241133eb0b6dSopenharmony_ci{ 241233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 241333eb0b6dSopenharmony_ci CHECK_ARG(env, data); 241433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 241533eb0b6dSopenharmony_ci 241633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 241733eb0b6dSopenharmony_ci uint8_t** values = (uint8_t**)(data); 241833eb0b6dSopenharmony_ci Local<panda::ArrayBufferRef> res = panda::ArrayBufferRef::New(vm, byte_length); 241933eb0b6dSopenharmony_ci if (values != nullptr) { 242033eb0b6dSopenharmony_ci *values = reinterpret_cast<uint8_t*>(res->GetBuffer(vm)); 242133eb0b6dSopenharmony_ci } 242233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(res); 242333eb0b6dSopenharmony_ci 242433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 242533eb0b6dSopenharmony_ci} 242633eb0b6dSopenharmony_ci 242733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_sendable_arraybuffer(napi_env env, size_t byte_length, 242833eb0b6dSopenharmony_ci void** data, napi_value* result) 242933eb0b6dSopenharmony_ci{ 243033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 243133eb0b6dSopenharmony_ci CHECK_ARG(env, data); 243233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 243333eb0b6dSopenharmony_ci 243433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 243533eb0b6dSopenharmony_ci uint8_t** values = (uint8_t**)(data); 243633eb0b6dSopenharmony_ci Local<panda::SendableArrayBufferRef> res = panda::SendableArrayBufferRef::New(vm, byte_length); 243733eb0b6dSopenharmony_ci if (values != nullptr) { 243833eb0b6dSopenharmony_ci *values = reinterpret_cast<uint8_t*>(res->GetBuffer(vm)); 243933eb0b6dSopenharmony_ci } 244033eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(res); 244133eb0b6dSopenharmony_ci 244233eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 244333eb0b6dSopenharmony_ci} 244433eb0b6dSopenharmony_ci 244533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_external_arraybuffer(napi_env env, 244633eb0b6dSopenharmony_ci void* external_data, 244733eb0b6dSopenharmony_ci size_t byte_length, 244833eb0b6dSopenharmony_ci napi_finalize finalize_cb, 244933eb0b6dSopenharmony_ci void* finalize_hint, 245033eb0b6dSopenharmony_ci napi_value* result) 245133eb0b6dSopenharmony_ci{ 245233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 245333eb0b6dSopenharmony_ci CHECK_ARG(env, external_data); 245433eb0b6dSopenharmony_ci CHECK_ARG(env, finalize_cb); 245533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 245633eb0b6dSopenharmony_ci 245733eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 245833eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 245933eb0b6dSopenharmony_ci auto callback = reinterpret_cast<panda::NativePointerCallback>(finalize_cb); 246033eb0b6dSopenharmony_ci Local<panda::ArrayBufferRef> object = 246133eb0b6dSopenharmony_ci panda::ArrayBufferRef::New(vm, external_data, byte_length, callback, finalize_hint); 246233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 246333eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 246433eb0b6dSopenharmony_ci} 246533eb0b6dSopenharmony_ci 246633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_arraybuffer_info(napi_env env, 246733eb0b6dSopenharmony_ci napi_value arraybuffer, 246833eb0b6dSopenharmony_ci void** data, 246933eb0b6dSopenharmony_ci size_t* byte_length) 247033eb0b6dSopenharmony_ci{ 247133eb0b6dSopenharmony_ci CHECK_ENV(env); 247233eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 247333eb0b6dSopenharmony_ci CHECK_ARG(env, byte_length); 247433eb0b6dSopenharmony_ci 247533eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(arraybuffer); 247633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 247733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 247833eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsArrayBuffer(vm))) { 247933eb0b6dSopenharmony_ci Local<panda::ArrayBufferRef> res(nativeValue); 248033eb0b6dSopenharmony_ci if (data != nullptr) { 248133eb0b6dSopenharmony_ci *data = res->GetBuffer(vm); 248233eb0b6dSopenharmony_ci } 248333eb0b6dSopenharmony_ci *byte_length = res->ByteLength(vm); 248433eb0b6dSopenharmony_ci } else if (nativeValue->IsSendableArrayBuffer(vm)) { 248533eb0b6dSopenharmony_ci Local<panda::SendableArrayBufferRef> res(nativeValue); 248633eb0b6dSopenharmony_ci if (data != nullptr) { 248733eb0b6dSopenharmony_ci *data = res->GetBuffer(vm); 248833eb0b6dSopenharmony_ci } 248933eb0b6dSopenharmony_ci *byte_length = res->ByteLength(vm); 249033eb0b6dSopenharmony_ci } else { 249133eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_arraybuffer_expected); 249233eb0b6dSopenharmony_ci } 249333eb0b6dSopenharmony_ci 249433eb0b6dSopenharmony_ci return napi_clear_last_error(env); 249533eb0b6dSopenharmony_ci} 249633eb0b6dSopenharmony_ci 249733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result) 249833eb0b6dSopenharmony_ci{ 249933eb0b6dSopenharmony_ci CHECK_ENV(env); 250033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 250133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 250233eb0b6dSopenharmony_ci 250333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 250433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 250533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 250633eb0b6dSopenharmony_ci 250733eb0b6dSopenharmony_ci *result = nativeValue->IsTypedArray(vm) || nativeValue->IsSharedTypedArray(vm); 250833eb0b6dSopenharmony_ci 250933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 251033eb0b6dSopenharmony_ci} 251133eb0b6dSopenharmony_ci 251233eb0b6dSopenharmony_ciEXTERN_C_START 251333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_buffer(napi_env env, napi_value value, bool* result) 251433eb0b6dSopenharmony_ci{ 251533eb0b6dSopenharmony_ci CHECK_ENV(env); 251633eb0b6dSopenharmony_ci CHECK_ARG(env, value); 251733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 251833eb0b6dSopenharmony_ci 251933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 252033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 252133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 252233eb0b6dSopenharmony_ci 252333eb0b6dSopenharmony_ci *result = nativeValue->IsBuffer(vm); 252433eb0b6dSopenharmony_ci 252533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 252633eb0b6dSopenharmony_ci} 252733eb0b6dSopenharmony_ci 252833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_buffer(napi_env env, size_t size, void** data, napi_value* result) 252933eb0b6dSopenharmony_ci{ 253033eb0b6dSopenharmony_ci CHECK_ENV(env); 253133eb0b6dSopenharmony_ci CHECK_ARG(env, data); 253233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 253333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, size > 0, napi_invalid_arg); 253433eb0b6dSopenharmony_ci 253533eb0b6dSopenharmony_ci uint8_t** value = reinterpret_cast<uint8_t**>(data); 253633eb0b6dSopenharmony_ci if (!value) { 253733eb0b6dSopenharmony_ci HILOG_ERROR("value is empty"); 253833eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 253933eb0b6dSopenharmony_ci } 254033eb0b6dSopenharmony_ci 254133eb0b6dSopenharmony_ci if (size > MAX_BYTE_LENGTH) { 254233eb0b6dSopenharmony_ci HILOG_ERROR("Creat failed, current size: %{public}2f MiB, limit size: %{public}2f MiB", 254333eb0b6dSopenharmony_ci static_cast<float>(size) / static_cast<float>(ONEMIB_BYTE_SIZE), 254433eb0b6dSopenharmony_ci static_cast<float>(MAX_BYTE_LENGTH) / static_cast<float>(ONEMIB_BYTE_SIZE)); 254533eb0b6dSopenharmony_ci *value = nullptr; 254633eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 254733eb0b6dSopenharmony_ci } 254833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 254933eb0b6dSopenharmony_ci Local<panda::BufferRef> obj = BufferRef::New(vm, size); 255033eb0b6dSopenharmony_ci *value = reinterpret_cast<uint8_t*>(obj->GetBuffer(vm)); 255133eb0b6dSopenharmony_ci 255233eb0b6dSopenharmony_ci CHECK_ARG(env, *data); 255333eb0b6dSopenharmony_ci void* ptr = obj->GetBuffer(vm); 255433eb0b6dSopenharmony_ci CHECK_ARG(env, ptr); 255533eb0b6dSopenharmony_ci 255633eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(obj); 255733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 255833eb0b6dSopenharmony_ci} 255933eb0b6dSopenharmony_ci 256033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env, 256133eb0b6dSopenharmony_ci size_t length, 256233eb0b6dSopenharmony_ci const void* data, 256333eb0b6dSopenharmony_ci void** result_data, 256433eb0b6dSopenharmony_ci napi_value* result) 256533eb0b6dSopenharmony_ci{ 256633eb0b6dSopenharmony_ci CHECK_ENV(env); 256733eb0b6dSopenharmony_ci CHECK_ARG(env, data); 256833eb0b6dSopenharmony_ci CHECK_ARG(env, result_data); 256933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 257033eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, length > 0, napi_invalid_arg); 257133eb0b6dSopenharmony_ci 257233eb0b6dSopenharmony_ci uint8_t** value = reinterpret_cast<uint8_t**>(result_data); 257333eb0b6dSopenharmony_ci const uint8_t* recvdata = (uint8_t*)data; 257433eb0b6dSopenharmony_ci if (!value) { 257533eb0b6dSopenharmony_ci HILOG_ERROR("value is empty"); 257633eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 257733eb0b6dSopenharmony_ci } 257833eb0b6dSopenharmony_ci if (length > MAX_BYTE_LENGTH) { 257933eb0b6dSopenharmony_ci HILOG_ERROR("Creat failed, current size: %{public}2f MiB, limit size: %{public}2f MiB", 258033eb0b6dSopenharmony_ci static_cast<float>(length) / static_cast<float>(ONEMIB_BYTE_SIZE), 258133eb0b6dSopenharmony_ci static_cast<float>(MAX_BYTE_LENGTH) / static_cast<float>(ONEMIB_BYTE_SIZE)); 258233eb0b6dSopenharmony_ci *value = nullptr; 258333eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 258433eb0b6dSopenharmony_ci } 258533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 258633eb0b6dSopenharmony_ci Local<panda::BufferRef> obj = BufferRef::New(vm, length); 258733eb0b6dSopenharmony_ci if (obj->IsUndefined()) { 258833eb0b6dSopenharmony_ci HILOG_INFO("engine create buffer_copy failed!"); 258933eb0b6dSopenharmony_ci } 259033eb0b6dSopenharmony_ci *value = reinterpret_cast<uint8_t*>(obj->GetBuffer(vm)); 259133eb0b6dSopenharmony_ci if (memcpy_s(*value, length, recvdata, length) != EOK) { 259233eb0b6dSopenharmony_ci HILOG_ERROR("memcpy_s failed"); 259333eb0b6dSopenharmony_ci } 259433eb0b6dSopenharmony_ci 259533eb0b6dSopenharmony_ci void* ptr = obj->GetBuffer(vm); 259633eb0b6dSopenharmony_ci CHECK_ARG(env, ptr); 259733eb0b6dSopenharmony_ci 259833eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(obj); 259933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 260033eb0b6dSopenharmony_ci} 260133eb0b6dSopenharmony_ci 260233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_external_buffer(napi_env env, 260333eb0b6dSopenharmony_ci size_t length, 260433eb0b6dSopenharmony_ci void* data, 260533eb0b6dSopenharmony_ci napi_finalize finalize_cb, 260633eb0b6dSopenharmony_ci void* finalize_hint, 260733eb0b6dSopenharmony_ci napi_value* result) 260833eb0b6dSopenharmony_ci{ 260933eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 261033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 261133eb0b6dSopenharmony_ci CHECK_ARG(env, data); 261233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, length > 0, napi_invalid_arg); 261333eb0b6dSopenharmony_ci 261433eb0b6dSopenharmony_ci auto callback = reinterpret_cast<panda::NativePointerCallback>(finalize_cb); 261533eb0b6dSopenharmony_ci if (!data) { 261633eb0b6dSopenharmony_ci HILOG_ERROR("data is empty"); 261733eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 261833eb0b6dSopenharmony_ci } 261933eb0b6dSopenharmony_ci if (length > MAX_BYTE_LENGTH) { 262033eb0b6dSopenharmony_ci HILOG_ERROR("Creat failed, current size: %{public}2f MiB, limit size: %{public}2f MiB", 262133eb0b6dSopenharmony_ci static_cast<float>(length) / static_cast<float>(ONEMIB_BYTE_SIZE), 262233eb0b6dSopenharmony_ci static_cast<float>(MAX_BYTE_LENGTH) / static_cast<float>(ONEMIB_BYTE_SIZE)); 262333eb0b6dSopenharmony_ci data = nullptr; 262433eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 262533eb0b6dSopenharmony_ci } 262633eb0b6dSopenharmony_ci 262733eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 262833eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 262933eb0b6dSopenharmony_ci Local<panda::BufferRef> object = panda::BufferRef::New(vm, data, length, callback, finalize_hint); 263033eb0b6dSopenharmony_ci void* ptr = object->GetBuffer(vm); 263133eb0b6dSopenharmony_ci CHECK_ARG(env, ptr); 263233eb0b6dSopenharmony_ci 263333eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 263433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 263533eb0b6dSopenharmony_ci} 263633eb0b6dSopenharmony_ci 263733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_buffer_info(napi_env env, napi_value value, void** data, size_t* length) 263833eb0b6dSopenharmony_ci{ 263933eb0b6dSopenharmony_ci CHECK_ENV(env); 264033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 264133eb0b6dSopenharmony_ci 264233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 264333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 264433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 264533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsBuffer(vm), napi_status::napi_arraybuffer_expected); 264633eb0b6dSopenharmony_ci Local<panda::BufferRef> res(nativeValue); 264733eb0b6dSopenharmony_ci *data = res->GetBuffer(vm); 264833eb0b6dSopenharmony_ci *length = res->ByteLength(vm); 264933eb0b6dSopenharmony_ci 265033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 265133eb0b6dSopenharmony_ci} 265233eb0b6dSopenharmony_ci 265333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_object_freeze(napi_env env, napi_value object) 265433eb0b6dSopenharmony_ci{ 265533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 265633eb0b6dSopenharmony_ci CHECK_ARG(env, object); 265733eb0b6dSopenharmony_ci 265833eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 265933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 266033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 266133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 266233eb0b6dSopenharmony_ci Local<panda::ObjectRef> obj = nativeValue->ToEcmaObject(vm); 266333eb0b6dSopenharmony_ci obj->Freeze(vm); 266433eb0b6dSopenharmony_ci 266533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 266633eb0b6dSopenharmony_ci} 266733eb0b6dSopenharmony_ci 266833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_object_seal(napi_env env, napi_value object) 266933eb0b6dSopenharmony_ci{ 267033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 267133eb0b6dSopenharmony_ci CHECK_ARG(env, object); 267233eb0b6dSopenharmony_ci 267333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 267433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 267533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 267633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 267733eb0b6dSopenharmony_ci Local<panda::ObjectRef> obj = nativeValue->ToEcmaObject(vm); 267833eb0b6dSopenharmony_ci obj->Seal(vm); 267933eb0b6dSopenharmony_ci 268033eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 268133eb0b6dSopenharmony_ci} 268233eb0b6dSopenharmony_ci 268333eb0b6dSopenharmony_ciEXTERN_C_END 268433eb0b6dSopenharmony_ci 268533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_typedarray(napi_env env, 268633eb0b6dSopenharmony_ci napi_typedarray_type type, 268733eb0b6dSopenharmony_ci size_t length, 268833eb0b6dSopenharmony_ci napi_value arraybuffer, 268933eb0b6dSopenharmony_ci size_t byte_offset, 269033eb0b6dSopenharmony_ci napi_value* result) 269133eb0b6dSopenharmony_ci{ 269233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 269333eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 269433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 269533eb0b6dSopenharmony_ci 269633eb0b6dSopenharmony_ci auto value = LocalValueFromJsValue(arraybuffer); 269733eb0b6dSopenharmony_ci auto typedArrayType = (NativeTypedArrayType)type; 269833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 269933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 270033eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(vm), napi_status::napi_arraybuffer_expected); 270133eb0b6dSopenharmony_ci Local<panda::ArrayBufferRef> arrayBuf(value); 270233eb0b6dSopenharmony_ci 270333eb0b6dSopenharmony_ci if (!reinterpret_cast<NativeEngine*>(env)->NapiNewTypedArray(vm, typedArrayType, arrayBuf, 270433eb0b6dSopenharmony_ci byte_offset, length, result)) { 270533eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s invalid arg", __func__); 270633eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 270733eb0b6dSopenharmony_ci } 270833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 270933eb0b6dSopenharmony_ci} 271033eb0b6dSopenharmony_ci 271133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_sendable_typedarray(napi_env env, 271233eb0b6dSopenharmony_ci napi_typedarray_type type, 271333eb0b6dSopenharmony_ci size_t length, 271433eb0b6dSopenharmony_ci napi_value arraybuffer, 271533eb0b6dSopenharmony_ci size_t byte_offset, 271633eb0b6dSopenharmony_ci napi_value* result) 271733eb0b6dSopenharmony_ci{ 271833eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 271933eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 272033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 272133eb0b6dSopenharmony_ci 272233eb0b6dSopenharmony_ci auto value = LocalValueFromJsValue(arraybuffer); 272333eb0b6dSopenharmony_ci auto typedArrayType = (NativeTypedArrayType)type; 272433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 272533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 272633eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, value->IsSendableArrayBuffer(vm), napi_status::napi_arraybuffer_expected); 272733eb0b6dSopenharmony_ci Local<panda::SendableArrayBufferRef> arrayBuf(value); 272833eb0b6dSopenharmony_ci 272933eb0b6dSopenharmony_ci if (!reinterpret_cast<NativeEngine*>(env)->NapiNewSendableTypedArray(vm, typedArrayType, 273033eb0b6dSopenharmony_ci arrayBuf, byte_offset, 273133eb0b6dSopenharmony_ci length, result)) { 273233eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s invalid arg", __func__); 273333eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 273433eb0b6dSopenharmony_ci } 273533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 273633eb0b6dSopenharmony_ci} 273733eb0b6dSopenharmony_ci 273833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_typedarray_info(napi_env env, 273933eb0b6dSopenharmony_ci napi_value typedarray, 274033eb0b6dSopenharmony_ci napi_typedarray_type* type, 274133eb0b6dSopenharmony_ci size_t* length, 274233eb0b6dSopenharmony_ci void** data, 274333eb0b6dSopenharmony_ci napi_value* arraybuffer, 274433eb0b6dSopenharmony_ci size_t* byte_offset) 274533eb0b6dSopenharmony_ci{ 274633eb0b6dSopenharmony_ci CHECK_ENV(env); 274733eb0b6dSopenharmony_ci CHECK_ARG(env, typedarray); 274833eb0b6dSopenharmony_ci 274933eb0b6dSopenharmony_ci auto value = LocalValueFromJsValue(typedarray); 275033eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 275133eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 275233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 275333eb0b6dSopenharmony_ci if (LIKELY(value->IsTypedArray(vm))) { 275433eb0b6dSopenharmony_ci Local<panda::TypedArrayRef> typedArray = Local<panda::TypedArrayRef>(value); 275533eb0b6dSopenharmony_ci Local<ArrayBufferRef> localArrayBuffer = typedArray->GetArrayBuffer(vm); 275633eb0b6dSopenharmony_ci size_t byteoffset = typedArray->ByteOffset(vm); 275733eb0b6dSopenharmony_ci if (type != nullptr) { 275833eb0b6dSopenharmony_ci *type = static_cast<napi_typedarray_type>(engine->GetTypedArrayType(typedArray)); 275933eb0b6dSopenharmony_ci } 276033eb0b6dSopenharmony_ci if (length != nullptr) { 276133eb0b6dSopenharmony_ci *length = typedArray->ByteLength(vm); 276233eb0b6dSopenharmony_ci } 276333eb0b6dSopenharmony_ci if (data != nullptr) { 276433eb0b6dSopenharmony_ci *data = static_cast<uint8_t*>(localArrayBuffer->GetBuffer(vm)) + byteoffset; 276533eb0b6dSopenharmony_ci } 276633eb0b6dSopenharmony_ci if (arraybuffer != nullptr) { 276733eb0b6dSopenharmony_ci *arraybuffer = JsValueFromLocalValue(localArrayBuffer); 276833eb0b6dSopenharmony_ci } 276933eb0b6dSopenharmony_ci if (byte_offset != nullptr) { 277033eb0b6dSopenharmony_ci *byte_offset = byteoffset; 277133eb0b6dSopenharmony_ci } 277233eb0b6dSopenharmony_ci } else if (value->IsSharedTypedArray(vm)) { 277333eb0b6dSopenharmony_ci Local<panda::SendableTypedArrayRef> typedArray = Local<panda::SendableTypedArrayRef>(value); 277433eb0b6dSopenharmony_ci Local<panda::SendableArrayBufferRef> localArrayBuffer = typedArray->GetArrayBuffer(vm); 277533eb0b6dSopenharmony_ci size_t byteoffset = typedArray->ByteOffset(vm); 277633eb0b6dSopenharmony_ci if (type != nullptr) { 277733eb0b6dSopenharmony_ci *type = static_cast<napi_typedarray_type>(engine->GetSendableTypedArrayType(typedArray)); 277833eb0b6dSopenharmony_ci } 277933eb0b6dSopenharmony_ci if (length != nullptr) { 278033eb0b6dSopenharmony_ci *length = typedArray->ByteLength(vm); 278133eb0b6dSopenharmony_ci } 278233eb0b6dSopenharmony_ci if (data != nullptr) { 278333eb0b6dSopenharmony_ci *data = static_cast<uint8_t*>(localArrayBuffer->GetBuffer(vm)) + byteoffset; 278433eb0b6dSopenharmony_ci } 278533eb0b6dSopenharmony_ci if (arraybuffer != nullptr) { 278633eb0b6dSopenharmony_ci *arraybuffer = JsValueFromLocalValue(localArrayBuffer); 278733eb0b6dSopenharmony_ci } 278833eb0b6dSopenharmony_ci if (byte_offset != nullptr) { 278933eb0b6dSopenharmony_ci *byte_offset = byteoffset; 279033eb0b6dSopenharmony_ci } 279133eb0b6dSopenharmony_ci } else { 279233eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s invalid arg", __func__); 279333eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 279433eb0b6dSopenharmony_ci } 279533eb0b6dSopenharmony_ci 279633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 279733eb0b6dSopenharmony_ci} 279833eb0b6dSopenharmony_ci 279933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_dataview(napi_env env, 280033eb0b6dSopenharmony_ci size_t length, 280133eb0b6dSopenharmony_ci napi_value arraybuffer, 280233eb0b6dSopenharmony_ci size_t byte_offset, 280333eb0b6dSopenharmony_ci napi_value* result) 280433eb0b6dSopenharmony_ci{ 280533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 280633eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 280733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 280833eb0b6dSopenharmony_ci 280933eb0b6dSopenharmony_ci auto arrayBufferValue = LocalValueFromJsValue(arraybuffer); 281033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 281133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 281233eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, arrayBufferValue->IsArrayBuffer(vm), napi_status::napi_arraybuffer_expected); 281333eb0b6dSopenharmony_ci Local<panda::ArrayBufferRef> res(arrayBufferValue); 281433eb0b6dSopenharmony_ci if (length + byte_offset > static_cast<size_t>(res->ByteLength(vm))) { 281533eb0b6dSopenharmony_ci napi_throw_range_error( 281633eb0b6dSopenharmony_ci env, 281733eb0b6dSopenharmony_ci "ERR_NAPI_INVALID_DATAVIEW_ARGS", 281833eb0b6dSopenharmony_ci "byte_offset + byte_length should be less than or " 281933eb0b6dSopenharmony_ci "equal to the size in bytes of the array passed in"); 282033eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_pending_exception); 282133eb0b6dSopenharmony_ci } 282233eb0b6dSopenharmony_ci 282333eb0b6dSopenharmony_ci Local<panda::DataViewRef> dataView = panda::DataViewRef::New(vm, res, byte_offset, length); 282433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(dataView); 282533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 282633eb0b6dSopenharmony_ci} 282733eb0b6dSopenharmony_ci 282833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_dataview(napi_env env, napi_value value, bool* result) 282933eb0b6dSopenharmony_ci{ 283033eb0b6dSopenharmony_ci CHECK_ENV(env); 283133eb0b6dSopenharmony_ci CHECK_ARG(env, value); 283233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 283333eb0b6dSopenharmony_ci 283433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 283533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 283633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 283733eb0b6dSopenharmony_ci *result = nativeValue->IsDataView(vm); 283833eb0b6dSopenharmony_ci 283933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 284033eb0b6dSopenharmony_ci} 284133eb0b6dSopenharmony_ci 284233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_dataview_info(napi_env env, 284333eb0b6dSopenharmony_ci napi_value dataview, 284433eb0b6dSopenharmony_ci size_t* bytelength, 284533eb0b6dSopenharmony_ci void** data, 284633eb0b6dSopenharmony_ci napi_value* arraybuffer, 284733eb0b6dSopenharmony_ci size_t* byte_offset) 284833eb0b6dSopenharmony_ci{ 284933eb0b6dSopenharmony_ci CHECK_ENV(env); 285033eb0b6dSopenharmony_ci CHECK_ARG(env, dataview); 285133eb0b6dSopenharmony_ci 285233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(dataview); 285333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 285433eb0b6dSopenharmony_ci bool isDataView = false; 285533eb0b6dSopenharmony_ci nativeValue->GetDataViewInfo(vm, isDataView, bytelength, data, 285633eb0b6dSopenharmony_ci reinterpret_cast<panda::JSValueRef**>(arraybuffer), byte_offset); 285733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, isDataView, napi_status::napi_invalid_arg); 285833eb0b6dSopenharmony_ci 285933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 286033eb0b6dSopenharmony_ci} 286133eb0b6dSopenharmony_ci 286233eb0b6dSopenharmony_ci// version management 286333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result) 286433eb0b6dSopenharmony_ci{ 286533eb0b6dSopenharmony_ci CHECK_ENV(env); 286633eb0b6dSopenharmony_ci CHECK_ARG(env, result); 286733eb0b6dSopenharmony_ci 286833eb0b6dSopenharmony_ci *result = NAPI_VERSION; 286933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 287033eb0b6dSopenharmony_ci} 287133eb0b6dSopenharmony_ci 287233eb0b6dSopenharmony_ci// Promises 287333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_promise(napi_env env, napi_deferred* deferred, napi_value* promise) 287433eb0b6dSopenharmony_ci{ 287533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 287633eb0b6dSopenharmony_ci CHECK_ARG(env, deferred); 287733eb0b6dSopenharmony_ci CHECK_ARG(env, promise); 287833eb0b6dSopenharmony_ci 287933eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 288033eb0b6dSopenharmony_ci NativeDeferred* nativeDeferred = nullptr; 288133eb0b6dSopenharmony_ci auto resultValue = engine->CreatePromise(&nativeDeferred); 288233eb0b6dSopenharmony_ci if (LocalValueFromJsValue(resultValue)->IsUndefined()) { 288333eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_generic_failure); 288433eb0b6dSopenharmony_ci } 288533eb0b6dSopenharmony_ci *deferred = reinterpret_cast<napi_deferred>(nativeDeferred); 288633eb0b6dSopenharmony_ci *promise = resultValue; 288733eb0b6dSopenharmony_ci 288833eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 288933eb0b6dSopenharmony_ci} 289033eb0b6dSopenharmony_ci 289133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, napi_deferred deferred, napi_value resolution) 289233eb0b6dSopenharmony_ci{ 289333eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 289433eb0b6dSopenharmony_ci CHECK_ARG(env, deferred); 289533eb0b6dSopenharmony_ci CHECK_ARG(env, resolution); 289633eb0b6dSopenharmony_ci 289733eb0b6dSopenharmony_ci auto nativeDeferred = reinterpret_cast<NativeDeferred*>(deferred); 289833eb0b6dSopenharmony_ci nativeDeferred->Resolve(resolution); 289933eb0b6dSopenharmony_ci delete nativeDeferred; 290033eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 290133eb0b6dSopenharmony_ci} 290233eb0b6dSopenharmony_ci 290333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_reject_deferred(napi_env env, napi_deferred deferred, napi_value rejection) 290433eb0b6dSopenharmony_ci{ 290533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 290633eb0b6dSopenharmony_ci CHECK_ARG(env, deferred); 290733eb0b6dSopenharmony_ci CHECK_ARG(env, rejection); 290833eb0b6dSopenharmony_ci 290933eb0b6dSopenharmony_ci auto nativeDeferred = reinterpret_cast<NativeDeferred*>(deferred); 291033eb0b6dSopenharmony_ci nativeDeferred->Reject(rejection); 291133eb0b6dSopenharmony_ci delete nativeDeferred; 291233eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 291333eb0b6dSopenharmony_ci} 291433eb0b6dSopenharmony_ci 291533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_promise(napi_env env, napi_value value, bool* is_promise) 291633eb0b6dSopenharmony_ci{ 291733eb0b6dSopenharmony_ci CHECK_ENV(env); 291833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 291933eb0b6dSopenharmony_ci CHECK_ARG(env, is_promise); 292033eb0b6dSopenharmony_ci 292133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 292233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 292333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 292433eb0b6dSopenharmony_ci *is_promise = nativeValue->IsPromise(vm); 292533eb0b6dSopenharmony_ci 292633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 292733eb0b6dSopenharmony_ci} 292833eb0b6dSopenharmony_ci 292933eb0b6dSopenharmony_ci// promise reject events 293033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_set_promise_rejection_callback(napi_env env, napi_ref ref, napi_ref checkRef) 293133eb0b6dSopenharmony_ci{ 293233eb0b6dSopenharmony_ci CHECK_ENV(env); 293333eb0b6dSopenharmony_ci CHECK_ARG(env, ref); 293433eb0b6dSopenharmony_ci CHECK_ARG(env, checkRef); 293533eb0b6dSopenharmony_ci 293633eb0b6dSopenharmony_ci auto rejectCallbackRef = reinterpret_cast<NativeReference*>(ref); 293733eb0b6dSopenharmony_ci auto checkCallbackRef = reinterpret_cast<NativeReference*>(checkRef); 293833eb0b6dSopenharmony_ci if (rejectCallbackRef == nullptr || checkCallbackRef == nullptr) { 293933eb0b6dSopenharmony_ci HILOG_ERROR("rejectCallbackRef or checkCallbackRef is nullptr"); 294033eb0b6dSopenharmony_ci } else { 294133eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 294233eb0b6dSopenharmony_ci auto vm = const_cast<EcmaVM*>(engine->GetEcmaVm()); 294333eb0b6dSopenharmony_ci engine->SetPromiseRejectCallBackRef(rejectCallbackRef); 294433eb0b6dSopenharmony_ci engine->SetCheckCallbackRef(checkCallbackRef); 294533eb0b6dSopenharmony_ci panda::JSNApi::SetHostPromiseRejectionTracker(const_cast<EcmaVM*>(vm), engine->GetPromiseRejectCallback(), 294633eb0b6dSopenharmony_ci reinterpret_cast<void*>(engine)); 294733eb0b6dSopenharmony_ci } 294833eb0b6dSopenharmony_ci 294933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 295033eb0b6dSopenharmony_ci} 295133eb0b6dSopenharmony_ci 295233eb0b6dSopenharmony_ci// Running a script 295333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_run_script(napi_env env, napi_value script, napi_value* result) 295433eb0b6dSopenharmony_ci{ 295533eb0b6dSopenharmony_ci CHECK_ENV(env); 295633eb0b6dSopenharmony_ci CHECK_ARG(env, script); 295733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 295833eb0b6dSopenharmony_ci 295933eb0b6dSopenharmony_ci *result = nullptr; 296033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 296133eb0b6dSopenharmony_ci} 296233eb0b6dSopenharmony_ci 296333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_run_actor(napi_env env, 296433eb0b6dSopenharmony_ci uint8_t* buffer, 296533eb0b6dSopenharmony_ci size_t bufferSize, 296633eb0b6dSopenharmony_ci const char* descriptor, 296733eb0b6dSopenharmony_ci napi_value* result, 296833eb0b6dSopenharmony_ci char* entryPoint) 296933eb0b6dSopenharmony_ci{ 297033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 297133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 297233eb0b6dSopenharmony_ci 297333eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 297433eb0b6dSopenharmony_ci *result = engine->RunActor(buffer, bufferSize, descriptor, entryPoint, false); 297533eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 297633eb0b6dSopenharmony_ci} 297733eb0b6dSopenharmony_ci 297833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_load_module(napi_env env, const char* path, napi_value* result) 297933eb0b6dSopenharmony_ci{ 298033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 298133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 298233eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 298333eb0b6dSopenharmony_ci *result = engine->NapiLoadModule(path, nullptr); 298433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 298533eb0b6dSopenharmony_ci} 298633eb0b6dSopenharmony_ci 298733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_load_module_with_info(napi_env env, 298833eb0b6dSopenharmony_ci const char* path, 298933eb0b6dSopenharmony_ci const char* module_info, 299033eb0b6dSopenharmony_ci napi_value* result) 299133eb0b6dSopenharmony_ci{ 299233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 299333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 299433eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 299533eb0b6dSopenharmony_ci *result = engine->NapiLoadModuleWithInfo(path, module_info); 299633eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 299733eb0b6dSopenharmony_ci} 299833eb0b6dSopenharmony_ci// Memory management 299933eb0b6dSopenharmony_ciNAPI_INNER_EXTERN napi_status napi_adjust_external_memory( 300033eb0b6dSopenharmony_ci napi_env env, int64_t change_in_bytes, int64_t* adjusted_value) 300133eb0b6dSopenharmony_ci{ 300233eb0b6dSopenharmony_ci CHECK_ENV(env); 300333eb0b6dSopenharmony_ci CHECK_ARG(env, adjusted_value); 300433eb0b6dSopenharmony_ci 300533eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 300633eb0b6dSopenharmony_ci engine->AdjustExternalMemory(change_in_bytes, adjusted_value); 300733eb0b6dSopenharmony_ci 300833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 300933eb0b6dSopenharmony_ci} 301033eb0b6dSopenharmony_ci 301133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_callable(napi_env env, napi_value value, bool* result) 301233eb0b6dSopenharmony_ci{ 301333eb0b6dSopenharmony_ci CHECK_ENV(env); 301433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 301533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 301633eb0b6dSopenharmony_ci 301733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 301833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 301933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 302033eb0b6dSopenharmony_ci 302133eb0b6dSopenharmony_ci *result = nativeValue->IsFunction(vm); 302233eb0b6dSopenharmony_ci 302333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 302433eb0b6dSopenharmony_ci} 302533eb0b6dSopenharmony_ci 302633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_arguments_object(napi_env env, napi_value value, bool* result) 302733eb0b6dSopenharmony_ci{ 302833eb0b6dSopenharmony_ci CHECK_ENV(env); 302933eb0b6dSopenharmony_ci CHECK_ARG(env, value); 303033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 303133eb0b6dSopenharmony_ci 303233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 303333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 303433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 303533eb0b6dSopenharmony_ci 303633eb0b6dSopenharmony_ci *result = nativeValue->IsArgumentsObject(vm); 303733eb0b6dSopenharmony_ci 303833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 303933eb0b6dSopenharmony_ci} 304033eb0b6dSopenharmony_ci 304133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_async_function(napi_env env, napi_value value, bool* result) 304233eb0b6dSopenharmony_ci{ 304333eb0b6dSopenharmony_ci CHECK_ENV(env); 304433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 304533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 304633eb0b6dSopenharmony_ci 304733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 304833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 304933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 305033eb0b6dSopenharmony_ci 305133eb0b6dSopenharmony_ci *result = nativeValue->IsAsyncFunction(vm); 305233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 305333eb0b6dSopenharmony_ci} 305433eb0b6dSopenharmony_ci 305533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_boolean_object(napi_env env, napi_value value, bool* result) 305633eb0b6dSopenharmony_ci{ 305733eb0b6dSopenharmony_ci CHECK_ENV(env); 305833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 305933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 306033eb0b6dSopenharmony_ci 306133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 306233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 306333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 306433eb0b6dSopenharmony_ci 306533eb0b6dSopenharmony_ci *result = nativeValue->IsJSPrimitiveBoolean(vm); 306633eb0b6dSopenharmony_ci 306733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 306833eb0b6dSopenharmony_ci} 306933eb0b6dSopenharmony_ci 307033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_generator_function(napi_env env, napi_value value, bool* result) 307133eb0b6dSopenharmony_ci{ 307233eb0b6dSopenharmony_ci CHECK_ENV(env); 307333eb0b6dSopenharmony_ci CHECK_ARG(env, value); 307433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 307533eb0b6dSopenharmony_ci 307633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 307733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 307833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 307933eb0b6dSopenharmony_ci 308033eb0b6dSopenharmony_ci *result = nativeValue->IsGeneratorFunction(vm); 308133eb0b6dSopenharmony_ci 308233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 308333eb0b6dSopenharmony_ci} 308433eb0b6dSopenharmony_ci 308533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_map_iterator(napi_env env, napi_value value, bool* result) 308633eb0b6dSopenharmony_ci{ 308733eb0b6dSopenharmony_ci CHECK_ENV(env); 308833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 308933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 309033eb0b6dSopenharmony_ci 309133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 309233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 309333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 309433eb0b6dSopenharmony_ci 309533eb0b6dSopenharmony_ci *result = nativeValue->IsMapIterator(vm); 309633eb0b6dSopenharmony_ci 309733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 309833eb0b6dSopenharmony_ci} 309933eb0b6dSopenharmony_ci 310033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_set_iterator(napi_env env, napi_value value, bool* result) 310133eb0b6dSopenharmony_ci{ 310233eb0b6dSopenharmony_ci CHECK_ENV(env); 310333eb0b6dSopenharmony_ci CHECK_ARG(env, value); 310433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 310533eb0b6dSopenharmony_ci 310633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 310733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 310833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 310933eb0b6dSopenharmony_ci 311033eb0b6dSopenharmony_ci *result = nativeValue->IsSetIterator(vm); 311133eb0b6dSopenharmony_ci 311233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 311333eb0b6dSopenharmony_ci} 311433eb0b6dSopenharmony_ci 311533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_generator_object(napi_env env, napi_value value, bool* result) 311633eb0b6dSopenharmony_ci{ 311733eb0b6dSopenharmony_ci CHECK_ENV(env); 311833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 311933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 312033eb0b6dSopenharmony_ci 312133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 312233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 312333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 312433eb0b6dSopenharmony_ci 312533eb0b6dSopenharmony_ci *result = nativeValue->IsGeneratorObject(vm); 312633eb0b6dSopenharmony_ci 312733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 312833eb0b6dSopenharmony_ci} 312933eb0b6dSopenharmony_ci 313033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_module_namespace_object(napi_env env, napi_value value, bool* result) 313133eb0b6dSopenharmony_ci{ 313233eb0b6dSopenharmony_ci CHECK_ENV(env); 313333eb0b6dSopenharmony_ci CHECK_ARG(env, value); 313433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 313533eb0b6dSopenharmony_ci 313633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 313733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 313833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 313933eb0b6dSopenharmony_ci 314033eb0b6dSopenharmony_ci *result = nativeValue->IsModuleNamespaceObject(vm); 314133eb0b6dSopenharmony_ci 314233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 314333eb0b6dSopenharmony_ci} 314433eb0b6dSopenharmony_ci 314533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_proxy(napi_env env, napi_value value, bool* result) 314633eb0b6dSopenharmony_ci{ 314733eb0b6dSopenharmony_ci CHECK_ENV(env); 314833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 314933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 315033eb0b6dSopenharmony_ci 315133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 315233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 315333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 315433eb0b6dSopenharmony_ci 315533eb0b6dSopenharmony_ci *result = nativeValue->IsProxy(vm); 315633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 315733eb0b6dSopenharmony_ci} 315833eb0b6dSopenharmony_ci 315933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_reg_exp(napi_env env, napi_value value, bool* result) 316033eb0b6dSopenharmony_ci{ 316133eb0b6dSopenharmony_ci CHECK_ENV(env); 316233eb0b6dSopenharmony_ci CHECK_ARG(env, value); 316333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 316433eb0b6dSopenharmony_ci 316533eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 316633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 316733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 316833eb0b6dSopenharmony_ci 316933eb0b6dSopenharmony_ci *result = nativeValue->IsRegExp(vm); 317033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 317133eb0b6dSopenharmony_ci} 317233eb0b6dSopenharmony_ci 317333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_number_object(napi_env env, napi_value value, bool* result) 317433eb0b6dSopenharmony_ci{ 317533eb0b6dSopenharmony_ci CHECK_ENV(env); 317633eb0b6dSopenharmony_ci CHECK_ARG(env, value); 317733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 317833eb0b6dSopenharmony_ci 317933eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 318033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 318133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 318233eb0b6dSopenharmony_ci 318333eb0b6dSopenharmony_ci *result = nativeValue->IsJSPrimitiveNumber(vm); 318433eb0b6dSopenharmony_ci 318533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 318633eb0b6dSopenharmony_ci} 318733eb0b6dSopenharmony_ci 318833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_map(napi_env env, napi_value value, bool* result) 318933eb0b6dSopenharmony_ci{ 319033eb0b6dSopenharmony_ci CHECK_ENV(env); 319133eb0b6dSopenharmony_ci CHECK_ARG(env, value); 319233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 319333eb0b6dSopenharmony_ci 319433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 319533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 319633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 319733eb0b6dSopenharmony_ci 319833eb0b6dSopenharmony_ci *result = nativeValue->IsMap(vm) || nativeValue->IsSharedMap(vm); 319933eb0b6dSopenharmony_ci return napi_clear_last_error(env); 320033eb0b6dSopenharmony_ci} 320133eb0b6dSopenharmony_ci 320233eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_set(napi_env env, napi_value value, bool* result) 320333eb0b6dSopenharmony_ci{ 320433eb0b6dSopenharmony_ci CHECK_ENV(env); 320533eb0b6dSopenharmony_ci CHECK_ARG(env, value); 320633eb0b6dSopenharmony_ci CHECK_ARG(env, result); 320733eb0b6dSopenharmony_ci 320833eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 320933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 321033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 321133eb0b6dSopenharmony_ci 321233eb0b6dSopenharmony_ci *result = nativeValue->IsSet(vm) || nativeValue->IsSharedSet(vm); 321333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 321433eb0b6dSopenharmony_ci} 321533eb0b6dSopenharmony_ci 321633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_string_object(napi_env env, napi_value value, bool* result) 321733eb0b6dSopenharmony_ci{ 321833eb0b6dSopenharmony_ci CHECK_ENV(env); 321933eb0b6dSopenharmony_ci CHECK_ARG(env, value); 322033eb0b6dSopenharmony_ci CHECK_ARG(env, result); 322133eb0b6dSopenharmony_ci 322233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 322333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 322433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 322533eb0b6dSopenharmony_ci 322633eb0b6dSopenharmony_ci *result = nativeValue->IsJSPrimitiveString(vm); 322733eb0b6dSopenharmony_ci 322833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 322933eb0b6dSopenharmony_ci} 323033eb0b6dSopenharmony_ci 323133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_symbol_object(napi_env env, napi_value value, bool* result) 323233eb0b6dSopenharmony_ci{ 323333eb0b6dSopenharmony_ci CHECK_ENV(env); 323433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 323533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 323633eb0b6dSopenharmony_ci 323733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 323833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 323933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 324033eb0b6dSopenharmony_ci 324133eb0b6dSopenharmony_ci *result = nativeValue->IsJSPrimitiveSymbol(vm); 324233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 324333eb0b6dSopenharmony_ci} 324433eb0b6dSopenharmony_ci 324533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_weak_map(napi_env env, napi_value value, bool* result) 324633eb0b6dSopenharmony_ci{ 324733eb0b6dSopenharmony_ci CHECK_ENV(env); 324833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 324933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 325033eb0b6dSopenharmony_ci 325133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 325233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 325333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 325433eb0b6dSopenharmony_ci 325533eb0b6dSopenharmony_ci *result = nativeValue->IsWeakMap(vm); 325633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 325733eb0b6dSopenharmony_ci} 325833eb0b6dSopenharmony_ci 325933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_weak_set(napi_env env, napi_value value, bool* result) 326033eb0b6dSopenharmony_ci{ 326133eb0b6dSopenharmony_ci CHECK_ENV(env); 326233eb0b6dSopenharmony_ci CHECK_ARG(env, value); 326333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 326433eb0b6dSopenharmony_ci 326533eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 326633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 326733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 326833eb0b6dSopenharmony_ci 326933eb0b6dSopenharmony_ci *result = nativeValue->IsWeakSet(vm); 327033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 327133eb0b6dSopenharmony_ci} 327233eb0b6dSopenharmony_ci 327333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_runtime(napi_env env, napi_env* result_env) 327433eb0b6dSopenharmony_ci{ 327533eb0b6dSopenharmony_ci CHECK_ENV(env); 327633eb0b6dSopenharmony_ci CHECK_ARG(env, result_env); 327733eb0b6dSopenharmony_ci 327833eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 327933eb0b6dSopenharmony_ci auto result = engine->CreateRuntime(); 328033eb0b6dSopenharmony_ci if (result == nullptr) { 328133eb0b6dSopenharmony_ci return napi_generic_failure; 328233eb0b6dSopenharmony_ci } 328333eb0b6dSopenharmony_ci *result_env = reinterpret_cast<napi_env>(result); 328433eb0b6dSopenharmony_ci 328533eb0b6dSopenharmony_ci return napi_ok; 328633eb0b6dSopenharmony_ci} 328733eb0b6dSopenharmony_ci 328833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_serialize(napi_env env, 328933eb0b6dSopenharmony_ci napi_value object, 329033eb0b6dSopenharmony_ci napi_value transfer_list, 329133eb0b6dSopenharmony_ci napi_value clone_list, 329233eb0b6dSopenharmony_ci void** result) 329333eb0b6dSopenharmony_ci{ 329433eb0b6dSopenharmony_ci CHECK_ENV(env); 329533eb0b6dSopenharmony_ci CHECK_ARG(env, object); 329633eb0b6dSopenharmony_ci CHECK_ARG(env, transfer_list); 329733eb0b6dSopenharmony_ci CHECK_ARG(env, clone_list); 329833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 329933eb0b6dSopenharmony_ci 330033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 330133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 330233eb0b6dSopenharmony_ci auto transferList = LocalValueFromJsValue(transfer_list); 330333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, transferList->IsUndefined() || transferList->IsJSArray(vm), napi_invalid_arg); 330433eb0b6dSopenharmony_ci auto cloneList = LocalValueFromJsValue(clone_list); 330533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, cloneList->IsUndefined() || cloneList->IsJSArray(vm), napi_invalid_arg); 330633eb0b6dSopenharmony_ci *result = panda::JSNApi::SerializeValue(vm, nativeValue, transferList, cloneList, false, false); 330733eb0b6dSopenharmony_ci 330833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 330933eb0b6dSopenharmony_ci} 331033eb0b6dSopenharmony_ci 331133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_serialize_inner(napi_env env, napi_value object, napi_value transfer_list, 331233eb0b6dSopenharmony_ci napi_value clone_list, bool defaultTransfer, bool defaultCloneSendable, 331333eb0b6dSopenharmony_ci void** result) 331433eb0b6dSopenharmony_ci{ 331533eb0b6dSopenharmony_ci CHECK_ENV(env); 331633eb0b6dSopenharmony_ci CHECK_ARG(env, object); 331733eb0b6dSopenharmony_ci CHECK_ARG(env, transfer_list); 331833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 331933eb0b6dSopenharmony_ci 332033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 332133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 332233eb0b6dSopenharmony_ci auto transferList = LocalValueFromJsValue(transfer_list); 332333eb0b6dSopenharmony_ci auto cloneList = LocalValueFromJsValue(clone_list); 332433eb0b6dSopenharmony_ci *result = 332533eb0b6dSopenharmony_ci panda::JSNApi::SerializeValue(vm, nativeValue, transferList, cloneList, defaultTransfer, defaultCloneSendable); 332633eb0b6dSopenharmony_ci 332733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 332833eb0b6dSopenharmony_ci} 332933eb0b6dSopenharmony_ci 333033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_deserialize(napi_env env, void* buffer, napi_value* object) 333133eb0b6dSopenharmony_ci{ 333233eb0b6dSopenharmony_ci CHECK_ENV(env); 333333eb0b6dSopenharmony_ci CHECK_ARG(env, buffer); 333433eb0b6dSopenharmony_ci CHECK_ARG(env, object); 333533eb0b6dSopenharmony_ci 333633eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 333733eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 333833eb0b6dSopenharmony_ci Local<panda::JSValueRef> res = panda::JSNApi::DeserializeValue(vm, buffer, reinterpret_cast<void*>(engine)); 333933eb0b6dSopenharmony_ci *object = JsValueFromLocalValue(res); 334033eb0b6dSopenharmony_ci 334133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 334233eb0b6dSopenharmony_ci} 334333eb0b6dSopenharmony_ci 334433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_delete_serialization_data(napi_env env, void* buffer) 334533eb0b6dSopenharmony_ci{ 334633eb0b6dSopenharmony_ci CHECK_ENV(env); 334733eb0b6dSopenharmony_ci CHECK_ARG(env, buffer); 334833eb0b6dSopenharmony_ci 334933eb0b6dSopenharmony_ci panda::JSNApi::DeleteSerializationData(buffer); 335033eb0b6dSopenharmony_ci 335133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 335233eb0b6dSopenharmony_ci} 335333eb0b6dSopenharmony_ci 335433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_bigint_int64(napi_env env, int64_t value, napi_value* result) 335533eb0b6dSopenharmony_ci{ 335633eb0b6dSopenharmony_ci CHECK_ENV(env); 335733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 335833eb0b6dSopenharmony_ci 335933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 336033eb0b6dSopenharmony_ci Local<panda::BigIntRef> object = panda::BigIntRef::New(vm, value); 336133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 336233eb0b6dSopenharmony_ci 336333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 336433eb0b6dSopenharmony_ci} 336533eb0b6dSopenharmony_ci 336633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_bigint_uint64(napi_env env, uint64_t value, napi_value* result) 336733eb0b6dSopenharmony_ci{ 336833eb0b6dSopenharmony_ci CHECK_ENV(env); 336933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 337033eb0b6dSopenharmony_ci 337133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 337233eb0b6dSopenharmony_ci Local<panda::BigIntRef> object = panda::BigIntRef::New(vm, value); 337333eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(object); 337433eb0b6dSopenharmony_ci 337533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 337633eb0b6dSopenharmony_ci} 337733eb0b6dSopenharmony_ci 337833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_bigint_int64( 337933eb0b6dSopenharmony_ci napi_env env, napi_value value, int64_t* result, bool* lossless) 338033eb0b6dSopenharmony_ci{ 338133eb0b6dSopenharmony_ci CHECK_ENV(env); 338233eb0b6dSopenharmony_ci CHECK_ARG(env, value); 338333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 338433eb0b6dSopenharmony_ci CHECK_ARG(env, lossless); 338533eb0b6dSopenharmony_ci 338633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 338733eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 338833eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 338933eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsBigInt(vm), napi_bigint_expected); 339033eb0b6dSopenharmony_ci Local<panda::BigIntRef> bigIntVal = nativeValue->ToBigInt(vm); 339133eb0b6dSopenharmony_ci bigIntVal->BigIntToInt64(vm, result, lossless); 339233eb0b6dSopenharmony_ci 339333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 339433eb0b6dSopenharmony_ci} 339533eb0b6dSopenharmony_ci 339633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_bigint_uint64( 339733eb0b6dSopenharmony_ci napi_env env, napi_value value, uint64_t* result, bool* lossless) 339833eb0b6dSopenharmony_ci{ 339933eb0b6dSopenharmony_ci CHECK_ENV(env); 340033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 340133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 340233eb0b6dSopenharmony_ci CHECK_ARG(env, lossless); 340333eb0b6dSopenharmony_ci 340433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 340533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 340633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 340733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsBigInt(vm), napi_bigint_expected); 340833eb0b6dSopenharmony_ci Local<panda::BigIntRef> bigIntVal = nativeValue->ToBigInt(vm); 340933eb0b6dSopenharmony_ci bigIntVal->BigIntToUint64(vm, result, lossless); 341033eb0b6dSopenharmony_ci 341133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 341233eb0b6dSopenharmony_ci} 341333eb0b6dSopenharmony_ci 341433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_date(napi_env env, napi_value value, bool* result) 341533eb0b6dSopenharmony_ci{ 341633eb0b6dSopenharmony_ci CHECK_ENV(env); 341733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 341833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 341933eb0b6dSopenharmony_ci 342033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 342133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 342233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 342333eb0b6dSopenharmony_ci 342433eb0b6dSopenharmony_ci *result = nativeValue->IsDate(vm); 342533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 342633eb0b6dSopenharmony_ci} 342733eb0b6dSopenharmony_ci 342833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_detached_arraybuffer(napi_env env, napi_value arraybuffer, bool* result) 342933eb0b6dSopenharmony_ci{ 343033eb0b6dSopenharmony_ci CHECK_ENV(env); 343133eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 343233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 343333eb0b6dSopenharmony_ci 343433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(arraybuffer); 343533eb0b6dSopenharmony_ci bool isArrayBuffer = false; 343633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 343733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 343833eb0b6dSopenharmony_ci 343933eb0b6dSopenharmony_ci bool isDetach = nativeValue->IsDetachedArraybuffer(vm, isArrayBuffer); 344033eb0b6dSopenharmony_ci if (isArrayBuffer) { 344133eb0b6dSopenharmony_ci *result = isDetach; 344233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 344333eb0b6dSopenharmony_ci } else { 344433eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 344533eb0b6dSopenharmony_ci } 344633eb0b6dSopenharmony_ci} 344733eb0b6dSopenharmony_ci 344833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_all_property_names( 344933eb0b6dSopenharmony_ci napi_env env, napi_value object, napi_key_collection_mode key_mode, 345033eb0b6dSopenharmony_ci napi_key_filter key_filter, napi_key_conversion key_conversion, napi_value* result) 345133eb0b6dSopenharmony_ci{ 345233eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 345333eb0b6dSopenharmony_ci CHECK_ARG(env, object); 345433eb0b6dSopenharmony_ci CHECK_ARG(env, result); 345533eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(object); 345633eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 345733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 345833eb0b6dSopenharmony_ci CHECK_AND_CONVERT_TO_OBJECT(env, vm, nativeValue, obj); 345933eb0b6dSopenharmony_ci uint32_t filter = NATIVE_DEFAULT; 346033eb0b6dSopenharmony_ci if (key_filter & napi_key_writable) { 346133eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_WRITABLE); 346233eb0b6dSopenharmony_ci } 346333eb0b6dSopenharmony_ci if (key_filter & napi_key_enumerable) { 346433eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_ENUMERABLE); 346533eb0b6dSopenharmony_ci } 346633eb0b6dSopenharmony_ci if (key_filter & napi_key_configurable) { 346733eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_CONFIGURABLE); 346833eb0b6dSopenharmony_ci } 346933eb0b6dSopenharmony_ci if (key_filter & napi_key_skip_strings) { 347033eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_KEY_SKIP_STRINGS); 347133eb0b6dSopenharmony_ci } 347233eb0b6dSopenharmony_ci if (key_filter & napi_key_skip_symbols) { 347333eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_KEY_SKIP_SYMBOLS); 347433eb0b6dSopenharmony_ci } 347533eb0b6dSopenharmony_ci 347633eb0b6dSopenharmony_ci switch (key_mode) { 347733eb0b6dSopenharmony_ci case napi_key_include_prototypes: 347833eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_KEY_INCLUDE_PROTOTYPES); 347933eb0b6dSopenharmony_ci break; 348033eb0b6dSopenharmony_ci case napi_key_own_only: 348133eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_KEY_OWN_ONLY); 348233eb0b6dSopenharmony_ci break; 348333eb0b6dSopenharmony_ci default: 348433eb0b6dSopenharmony_ci *result = nullptr; 348533eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s invalid arg", __func__); 348633eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 348733eb0b6dSopenharmony_ci } 348833eb0b6dSopenharmony_ci 348933eb0b6dSopenharmony_ci switch (key_conversion) { 349033eb0b6dSopenharmony_ci case napi_key_keep_numbers: 349133eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_KEY_KEEP_NUMBERS); 349233eb0b6dSopenharmony_ci break; 349333eb0b6dSopenharmony_ci case napi_key_numbers_to_strings: 349433eb0b6dSopenharmony_ci filter = static_cast<uint32_t>(filter | NATIVE_KEY_NUMBERS_TO_STRINGS); 349533eb0b6dSopenharmony_ci break; 349633eb0b6dSopenharmony_ci default: 349733eb0b6dSopenharmony_ci *result = nullptr; 349833eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s invalid arg", __func__); 349933eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 350033eb0b6dSopenharmony_ci } 350133eb0b6dSopenharmony_ci Local<panda::ArrayRef> arrayVal = obj->GetAllPropertyNames(vm, filter); 350233eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 350333eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 350433eb0b6dSopenharmony_ci} 350533eb0b6dSopenharmony_ci 350633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) 350733eb0b6dSopenharmony_ci{ 350833eb0b6dSopenharmony_ci CHECK_ENV(env); 350933eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 351033eb0b6dSopenharmony_ci 351133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(arraybuffer); 351233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 351333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 351433eb0b6dSopenharmony_ci 351533eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 351633eb0b6dSopenharmony_ci bool isArrayBuffer = false; 351733eb0b6dSopenharmony_ci nativeValue->DetachedArraybuffer(vm, isArrayBuffer); 351833eb0b6dSopenharmony_ci if (!isArrayBuffer) { 351933eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 352033eb0b6dSopenharmony_ci } 352133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 352233eb0b6dSopenharmony_ci} 352333eb0b6dSopenharmony_ci 352433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_type_tag_object(napi_env env, napi_value js_object, const napi_type_tag* type_tag) 352533eb0b6dSopenharmony_ci{ 352633eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 352733eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 352833eb0b6dSopenharmony_ci CHECK_ARG(env, type_tag); 352933eb0b6dSopenharmony_ci 353033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 353133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 353233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 353333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 353433eb0b6dSopenharmony_ci auto obj = nativeValue->ToEcmaObject(vm); 353533eb0b6dSopenharmony_ci NapiTypeTag* typeTag = (NapiTypeTag*)type_tag; 353633eb0b6dSopenharmony_ci const char name[] = "ACENAPI_TYPETAG"; 353733eb0b6dSopenharmony_ci bool hasPribate = false; 353833eb0b6dSopenharmony_ci bool result = true; 353933eb0b6dSopenharmony_ci Local<panda::StringRef> key = StringRef::NewFromUtf8(vm, name); 354033eb0b6dSopenharmony_ci hasPribate = obj->Has(vm, key); 354133eb0b6dSopenharmony_ci if (!hasPribate) { 354233eb0b6dSopenharmony_ci constexpr int bigintMod = 2; // 2 : used for even number judgment 354333eb0b6dSopenharmony_ci int sign_bit = 0; 354433eb0b6dSopenharmony_ci size_t word_count = 2; 354533eb0b6dSopenharmony_ci bool sign = false; 354633eb0b6dSopenharmony_ci if ((sign_bit % bigintMod) == 1) { 354733eb0b6dSopenharmony_ci sign = true; 354833eb0b6dSopenharmony_ci } 354933eb0b6dSopenharmony_ci uint32_t size = (uint32_t)word_count; 355033eb0b6dSopenharmony_ci Local<panda::JSValueRef> value = panda::BigIntRef::CreateBigWords(vm, sign, size, 355133eb0b6dSopenharmony_ci reinterpret_cast<const uint64_t*>(typeTag)); 355233eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::NewFromUtf8(vm, name); 355333eb0b6dSopenharmony_ci result = obj->Set(vm, key, value); 355433eb0b6dSopenharmony_ci } 355533eb0b6dSopenharmony_ci if (!result) { 355633eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s invalid arg", __func__); 355733eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 355833eb0b6dSopenharmony_ci } 355933eb0b6dSopenharmony_ci 356033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 356133eb0b6dSopenharmony_ci} 356233eb0b6dSopenharmony_ci 356333eb0b6dSopenharmony_cibool BigIntGetWordsArray(const EcmaVM* vm, Local<panda::BigIntRef> &value, int* signBit, 356433eb0b6dSopenharmony_ci size_t* wordCount, uint64_t* words) 356533eb0b6dSopenharmony_ci{ 356633eb0b6dSopenharmony_ci if (wordCount == nullptr) { 356733eb0b6dSopenharmony_ci return false; 356833eb0b6dSopenharmony_ci } 356933eb0b6dSopenharmony_ci size_t size = static_cast<size_t>(value->GetWordsArraySize(vm)); 357033eb0b6dSopenharmony_ci if (signBit == nullptr && words == nullptr) { 357133eb0b6dSopenharmony_ci *wordCount = size; 357233eb0b6dSopenharmony_ci return true; 357333eb0b6dSopenharmony_ci } else if (signBit != nullptr && words != nullptr) { 357433eb0b6dSopenharmony_ci if (size > *wordCount) { 357533eb0b6dSopenharmony_ci size = *wordCount; 357633eb0b6dSopenharmony_ci } 357733eb0b6dSopenharmony_ci bool sign = false; 357833eb0b6dSopenharmony_ci value->GetWordsArray(vm, &sign, size, words); 357933eb0b6dSopenharmony_ci if (sign) { 358033eb0b6dSopenharmony_ci *signBit = 1; 358133eb0b6dSopenharmony_ci } else { 358233eb0b6dSopenharmony_ci *signBit = 0; 358333eb0b6dSopenharmony_ci } 358433eb0b6dSopenharmony_ci *wordCount = size; 358533eb0b6dSopenharmony_ci return true; 358633eb0b6dSopenharmony_ci } 358733eb0b6dSopenharmony_ci return false; 358833eb0b6dSopenharmony_ci} 358933eb0b6dSopenharmony_ci 359033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_check_object_type_tag(napi_env env, 359133eb0b6dSopenharmony_ci napi_value js_object, 359233eb0b6dSopenharmony_ci const napi_type_tag* type_tag, 359333eb0b6dSopenharmony_ci bool* result) 359433eb0b6dSopenharmony_ci{ 359533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 359633eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 359733eb0b6dSopenharmony_ci CHECK_ARG(env, type_tag); 359833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 359933eb0b6dSopenharmony_ci 360033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 360133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 360233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 360333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 360433eb0b6dSopenharmony_ci auto obj = nativeValue->ToEcmaObject(vm); 360533eb0b6dSopenharmony_ci NapiTypeTag* typeTag = (NapiTypeTag*)type_tag; 360633eb0b6dSopenharmony_ci *result = false; 360733eb0b6dSopenharmony_ci const char name[] = "ACENAPI_TYPETAG"; 360833eb0b6dSopenharmony_ci 360933eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::NewFromUtf8(vm, name); 361033eb0b6dSopenharmony_ci *result = obj->Has(vm, key); 361133eb0b6dSopenharmony_ci if (*result) { 361233eb0b6dSopenharmony_ci Local<panda::StringRef> key = panda::StringRef::NewFromUtf8(vm, name); 361333eb0b6dSopenharmony_ci Local<panda::JSValueRef> object = obj->Get(vm, key); 361433eb0b6dSopenharmony_ci if (object->IsBigInt(vm)) { 361533eb0b6dSopenharmony_ci int sign; 361633eb0b6dSopenharmony_ci size_t size = 2; // 2: Indicates that the number of elements is 2 361733eb0b6dSopenharmony_ci NapiTypeTag tag; 361833eb0b6dSopenharmony_ci Local<panda::BigIntRef> bigintObj = object->ToBigInt(vm); 361933eb0b6dSopenharmony_ci BigIntGetWordsArray(vm, bigintObj, &sign, &size, reinterpret_cast<uint64_t*>(&tag)); 362033eb0b6dSopenharmony_ci if (sign == 0 && ((size == 1) || (size == 2))) { // 2: Indicates that the number of elements is 2 362133eb0b6dSopenharmony_ci *result = (tag.lower == typeTag->lower && tag.upper == typeTag->upper); 362233eb0b6dSopenharmony_ci } 362333eb0b6dSopenharmony_ci } 362433eb0b6dSopenharmony_ci } 362533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 362633eb0b6dSopenharmony_ci} 362733eb0b6dSopenharmony_ci 362833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_date(napi_env env, double time, napi_value* result) 362933eb0b6dSopenharmony_ci{ 363033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 363133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 363233eb0b6dSopenharmony_ci 363333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 363433eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(DateRef::New(vm, time)); 363533eb0b6dSopenharmony_ci 363633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 363733eb0b6dSopenharmony_ci} 363833eb0b6dSopenharmony_ci 363933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_date_value(napi_env env, napi_value value, double* result) 364033eb0b6dSopenharmony_ci{ 364133eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 364233eb0b6dSopenharmony_ci CHECK_ARG(env, value); 364333eb0b6dSopenharmony_ci CHECK_ARG(env, result); 364433eb0b6dSopenharmony_ci 364533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 364633eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 364733eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 364833eb0b6dSopenharmony_ci 364933eb0b6dSopenharmony_ci auto IsDate_result = nativeValue->IsDate(vm); 365033eb0b6dSopenharmony_ci Local<panda::DateRef> dateObj(nativeValue); 365133eb0b6dSopenharmony_ci if (IsDate_result) { 365233eb0b6dSopenharmony_ci *result = dateObj->GetTime(vm); 365333eb0b6dSopenharmony_ci } else { 365433eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s date expected", __func__); 365533eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_date_expected); 365633eb0b6dSopenharmony_ci } 365733eb0b6dSopenharmony_ci 365833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 365933eb0b6dSopenharmony_ci} 366033eb0b6dSopenharmony_ci 366133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_add_finalizer(napi_env env, 366233eb0b6dSopenharmony_ci napi_value js_object, 366333eb0b6dSopenharmony_ci void* native_object, 366433eb0b6dSopenharmony_ci napi_finalize finalize_cb, 366533eb0b6dSopenharmony_ci void* finalize_hint, 366633eb0b6dSopenharmony_ci napi_ref* result) 366733eb0b6dSopenharmony_ci{ 366833eb0b6dSopenharmony_ci CHECK_ENV(env); 366933eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 367033eb0b6dSopenharmony_ci CHECK_ARG(env, finalize_cb); 367133eb0b6dSopenharmony_ci 367233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(js_object); 367333eb0b6dSopenharmony_ci auto callback = reinterpret_cast<NapiNativeFinalize>(finalize_cb); 367433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 367533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 367633eb0b6dSopenharmony_ci 367733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 367833eb0b6dSopenharmony_ci NativeReference* reference = nullptr; 367933eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 368033eb0b6dSopenharmony_ci if (result != nullptr) { 368133eb0b6dSopenharmony_ci reference = engine->CreateReference(js_object, 1, false, callback, native_object, finalize_hint); 368233eb0b6dSopenharmony_ci *result = reinterpret_cast<napi_ref>(reference); 368333eb0b6dSopenharmony_ci } else { 368433eb0b6dSopenharmony_ci reference = engine->CreateReference(js_object, 0, true, callback, native_object, finalize_hint); 368533eb0b6dSopenharmony_ci } 368633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 368733eb0b6dSopenharmony_ci} 368833eb0b6dSopenharmony_ci 368933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_bigint_words(napi_env env, 369033eb0b6dSopenharmony_ci int sign_bit, 369133eb0b6dSopenharmony_ci size_t word_count, 369233eb0b6dSopenharmony_ci const uint64_t* words, 369333eb0b6dSopenharmony_ci napi_value* result) 369433eb0b6dSopenharmony_ci{ 369533eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 369633eb0b6dSopenharmony_ci CHECK_ARG(env, words); 369733eb0b6dSopenharmony_ci CHECK_ARG(env, result); 369833eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, word_count <= INT_MAX, napi_invalid_arg); 369933eb0b6dSopenharmony_ci 370033eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 370133eb0b6dSopenharmony_ci constexpr int bigintMod = 2; // 2 : used for even number judgment 370233eb0b6dSopenharmony_ci bool sign = false; 370333eb0b6dSopenharmony_ci if ((sign_bit % bigintMod) == 1) { 370433eb0b6dSopenharmony_ci sign = true; 370533eb0b6dSopenharmony_ci } 370633eb0b6dSopenharmony_ci uint32_t size = (uint32_t)word_count; 370733eb0b6dSopenharmony_ci Local<panda::JSValueRef> value = panda::BigIntRef::CreateBigWords(vm, sign, size, words); 370833eb0b6dSopenharmony_ci 370933eb0b6dSopenharmony_ci if (panda::JSNApi::HasPendingException(vm)) { 371033eb0b6dSopenharmony_ci HILOG_ERROR("%{public}s pending exception", __func__); 371133eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_pending_exception); 371233eb0b6dSopenharmony_ci } 371333eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(value); 371433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 371533eb0b6dSopenharmony_ci} 371633eb0b6dSopenharmony_ci 371733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_value_bigint_words(napi_env env, 371833eb0b6dSopenharmony_ci napi_value value, 371933eb0b6dSopenharmony_ci int* sign_bit, 372033eb0b6dSopenharmony_ci size_t* word_count, 372133eb0b6dSopenharmony_ci uint64_t* words) 372233eb0b6dSopenharmony_ci{ 372333eb0b6dSopenharmony_ci CHECK_ENV(env); 372433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 372533eb0b6dSopenharmony_ci CHECK_ARG(env, word_count); 372633eb0b6dSopenharmony_ci 372733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 372833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 372933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 373033eb0b6dSopenharmony_ci 373133eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsBigInt(vm), napi_object_expected); 373233eb0b6dSopenharmony_ci auto BigintObj = nativeValue->ToBigInt(vm); 373333eb0b6dSopenharmony_ci if (word_count == nullptr) { 373433eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 373533eb0b6dSopenharmony_ci } 373633eb0b6dSopenharmony_ci size_t size = static_cast<size_t>(BigintObj->GetWordsArraySize(vm)); 373733eb0b6dSopenharmony_ci if (sign_bit == nullptr && words == nullptr) { 373833eb0b6dSopenharmony_ci *word_count = size; 373933eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_ok); 374033eb0b6dSopenharmony_ci } else if (sign_bit != nullptr && words != nullptr) { 374133eb0b6dSopenharmony_ci if (size > *word_count) { 374233eb0b6dSopenharmony_ci size = *word_count; 374333eb0b6dSopenharmony_ci } 374433eb0b6dSopenharmony_ci bool sign = false; 374533eb0b6dSopenharmony_ci BigintObj->GetWordsArray(vm, &sign, size, words); 374633eb0b6dSopenharmony_ci if (sign) { 374733eb0b6dSopenharmony_ci *sign_bit = 1; 374833eb0b6dSopenharmony_ci } else { 374933eb0b6dSopenharmony_ci *sign_bit = 0; 375033eb0b6dSopenharmony_ci } 375133eb0b6dSopenharmony_ci *word_count = size; 375233eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_ok); 375333eb0b6dSopenharmony_ci } 375433eb0b6dSopenharmony_ci 375533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 375633eb0b6dSopenharmony_ci} 375733eb0b6dSopenharmony_ci 375833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_run_script_path(napi_env env, const char* path, napi_value* result) 375933eb0b6dSopenharmony_ci{ 376033eb0b6dSopenharmony_ci NAPI_PREAMBLE(env); 376133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 376233eb0b6dSopenharmony_ci 376333eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 376433eb0b6dSopenharmony_ci std::string pathStr(path); 376533eb0b6dSopenharmony_ci if (engine->IsApplicationApiVersionAPI11Plus()) { 376633eb0b6dSopenharmony_ci pathStr = panda::JSNApi::NormalizePath(path); 376733eb0b6dSopenharmony_ci } 376833eb0b6dSopenharmony_ci HILOG_DEBUG("napi_run_script_path path: %{public}s", pathStr.c_str()); 376933eb0b6dSopenharmony_ci if (engine->IsRestrictedWorkerThread()) { 377033eb0b6dSopenharmony_ci *result = engine->RunScriptInRestrictedThread(pathStr.c_str()); 377133eb0b6dSopenharmony_ci } else { 377233eb0b6dSopenharmony_ci *result = engine->RunScript(pathStr.c_str()); 377333eb0b6dSopenharmony_ci } 377433eb0b6dSopenharmony_ci return GET_RETURN_STATUS(env); 377533eb0b6dSopenharmony_ci} 377633eb0b6dSopenharmony_ci 377733eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_big_int64_array(napi_env env, napi_value value, bool* result) 377833eb0b6dSopenharmony_ci{ 377933eb0b6dSopenharmony_ci CHECK_ENV(env); 378033eb0b6dSopenharmony_ci CHECK_ARG(env, value); 378133eb0b6dSopenharmony_ci CHECK_ARG(env, result); 378233eb0b6dSopenharmony_ci 378333eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 378433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 378533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 378633eb0b6dSopenharmony_ci 378733eb0b6dSopenharmony_ci *result = nativeValue->IsBigInt64Array(vm); 378833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 378933eb0b6dSopenharmony_ci} 379033eb0b6dSopenharmony_ci 379133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_big_uint64_array(napi_env env, napi_value value, bool* result) 379233eb0b6dSopenharmony_ci{ 379333eb0b6dSopenharmony_ci CHECK_ENV(env); 379433eb0b6dSopenharmony_ci CHECK_ARG(env, value); 379533eb0b6dSopenharmony_ci CHECK_ARG(env, result); 379633eb0b6dSopenharmony_ci 379733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 379833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 379933eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 380033eb0b6dSopenharmony_ci 380133eb0b6dSopenharmony_ci *result = nativeValue->IsBigUint64Array(vm); 380233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 380333eb0b6dSopenharmony_ci} 380433eb0b6dSopenharmony_ci 380533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_shared_array_buffer(napi_env env, napi_value value, bool* result) 380633eb0b6dSopenharmony_ci{ 380733eb0b6dSopenharmony_ci CHECK_ENV(env); 380833eb0b6dSopenharmony_ci CHECK_ARG(env, value); 380933eb0b6dSopenharmony_ci CHECK_ARG(env, result); 381033eb0b6dSopenharmony_ci 381133eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 381233eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 381333eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 381433eb0b6dSopenharmony_ci 381533eb0b6dSopenharmony_ci *result = nativeValue->IsSharedArrayBuffer(vm); 381633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 381733eb0b6dSopenharmony_ci} 381833eb0b6dSopenharmony_ci 381933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_stack_trace(napi_env env, std::string& stack) 382033eb0b6dSopenharmony_ci{ 382133eb0b6dSopenharmony_ci CHECK_ENV(env); 382233eb0b6dSopenharmony_ci 382333eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 382433eb0b6dSopenharmony_ci [[maybe_unused]] auto vm = engine->GetEcmaVm(); 382533eb0b6dSopenharmony_ci std::string rawStack; 382633eb0b6dSopenharmony_ci#if !defined(PREVIEW) && !defined(IOS_PLATFORM) 382733eb0b6dSopenharmony_ci DFXJSNApi::BuildJsStackTrace(vm, rawStack); 382833eb0b6dSopenharmony_ci stack = engine->ExecuteTranslateBySourceMap(rawStack); 382933eb0b6dSopenharmony_ci#else 383033eb0b6dSopenharmony_ci HILOG_WARN("GetStacktrace env get stack failed"); 383133eb0b6dSopenharmony_ci#endif 383233eb0b6dSopenharmony_ci 383333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 383433eb0b6dSopenharmony_ci} 383533eb0b6dSopenharmony_ci 383633eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_hybrid_stack_trace(napi_env env, std::string& stack) 383733eb0b6dSopenharmony_ci{ 383833eb0b6dSopenharmony_ci CHECK_ENV(env); 383933eb0b6dSopenharmony_ci 384033eb0b6dSopenharmony_ci#if defined(OHOS_PLATFORM) && !defined(is_arkui_x) 384133eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 384233eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 384333eb0b6dSopenharmony_ci stack = DumpHybridStack(vm); 384433eb0b6dSopenharmony_ci#else 384533eb0b6dSopenharmony_ci HILOG_WARN("GetHybridStacktrace env get hybrid stack failed"); 384633eb0b6dSopenharmony_ci#endif 384733eb0b6dSopenharmony_ci return napi_clear_last_error(env); 384833eb0b6dSopenharmony_ci} 384933eb0b6dSopenharmony_ci 385033eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_object_get_keys(napi_env env, napi_value data, napi_value* result) 385133eb0b6dSopenharmony_ci{ 385233eb0b6dSopenharmony_ci CHECK_ENV(env); 385333eb0b6dSopenharmony_ci 385433eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(data); 385533eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 385633eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 385733eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsObject(vm), napi_object_expected); 385833eb0b6dSopenharmony_ci auto obj = nativeValue->ToEcmaObject(vm); 385933eb0b6dSopenharmony_ci Local<panda::ArrayRef> arrayVal = obj->GetOwnEnumerablePropertyNames(vm); 386033eb0b6dSopenharmony_ci 386133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(arrayVal); 386233eb0b6dSopenharmony_ci return napi_clear_last_error(env); 386333eb0b6dSopenharmony_ci} 386433eb0b6dSopenharmony_ci 386533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_queue_async_work_with_qos(napi_env env, napi_async_work work, napi_qos_t qos) 386633eb0b6dSopenharmony_ci{ 386733eb0b6dSopenharmony_ci CHECK_ENV(env); 386833eb0b6dSopenharmony_ci CHECK_ARG(env, work); 386933eb0b6dSopenharmony_ci 387033eb0b6dSopenharmony_ci auto asyncWork = reinterpret_cast<NativeAsyncWork*>(work); 387133eb0b6dSopenharmony_ci asyncWork->QueueWithQos(qos); 387233eb0b6dSopenharmony_ci return napi_status::napi_ok; 387333eb0b6dSopenharmony_ci} 387433eb0b6dSopenharmony_ci 387533eb0b6dSopenharmony_civoid* DetachFuncCallback(void* engine, void* object, void* hint, void* detachData) 387633eb0b6dSopenharmony_ci{ 387733eb0b6dSopenharmony_ci if (detachData == nullptr || (engine == nullptr || object ==nullptr)) { 387833eb0b6dSopenharmony_ci HILOG_ERROR("DetachFuncCallback params has nullptr"); 387933eb0b6dSopenharmony_ci return nullptr; 388033eb0b6dSopenharmony_ci } 388133eb0b6dSopenharmony_ci DetachCallback detach = reinterpret_cast<DetachCallback>(detachData); 388233eb0b6dSopenharmony_ci void* detachVal = detach(reinterpret_cast<NativeEngine*>(engine), object, hint); 388333eb0b6dSopenharmony_ci return detachVal; 388433eb0b6dSopenharmony_ci} 388533eb0b6dSopenharmony_ci 388633eb0b6dSopenharmony_ciLocal<panda::JSValueRef> AttachFuncCallback(void* engine, void* buffer, void* hint, void* attachData) 388733eb0b6dSopenharmony_ci{ 388833eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(engine)->GetEcmaVm(); 388933eb0b6dSopenharmony_ci if (attachData == nullptr || (engine == nullptr || buffer ==nullptr)) { 389033eb0b6dSopenharmony_ci HILOG_ERROR("AttachFuncCallback params has nullptr"); 389133eb0b6dSopenharmony_ci return panda::JSValueRef::Undefined(vm); 389233eb0b6dSopenharmony_ci } 389333eb0b6dSopenharmony_ci EscapeLocalScope scope(vm); 389433eb0b6dSopenharmony_ci Local<panda::JSValueRef> result = panda::JSValueRef::Undefined(vm); 389533eb0b6dSopenharmony_ci NapiAttachCallback attach = reinterpret_cast<NapiAttachCallback>(attachData); 389633eb0b6dSopenharmony_ci napi_value attachVal = attach(reinterpret_cast<napi_env>(engine), buffer, hint); 389733eb0b6dSopenharmony_ci if (attachVal == nullptr) { 389833eb0b6dSopenharmony_ci HILOG_WARN("AttachFunc return nullptr"); 389933eb0b6dSopenharmony_ci } else { 390033eb0b6dSopenharmony_ci result = LocalValueFromJsValue(attachVal); 390133eb0b6dSopenharmony_ci } 390233eb0b6dSopenharmony_ci return scope.Escape(result); 390333eb0b6dSopenharmony_ci} 390433eb0b6dSopenharmony_ci 390533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_coerce_to_native_binding_object(napi_env env, 390633eb0b6dSopenharmony_ci napi_value js_object, 390733eb0b6dSopenharmony_ci napi_native_binding_detach_callback detach_cb, 390833eb0b6dSopenharmony_ci napi_native_binding_attach_callback attach_cb, 390933eb0b6dSopenharmony_ci void* native_object, 391033eb0b6dSopenharmony_ci void* hint) 391133eb0b6dSopenharmony_ci{ 391233eb0b6dSopenharmony_ci CHECK_ENV(env); 391333eb0b6dSopenharmony_ci CHECK_ARG(env, js_object); 391433eb0b6dSopenharmony_ci CHECK_ARG(env, detach_cb); 391533eb0b6dSopenharmony_ci CHECK_ARG(env, attach_cb); 391633eb0b6dSopenharmony_ci CHECK_ARG(env, native_object); 391733eb0b6dSopenharmony_ci 391833eb0b6dSopenharmony_ci auto jsValue = LocalValueFromJsValue(js_object); 391933eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 392033eb0b6dSopenharmony_ci auto vm = engine->GetEcmaVm(); 392133eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 392233eb0b6dSopenharmony_ci 392333eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, jsValue->IsObject(vm), napi_object_expected); 392433eb0b6dSopenharmony_ci auto obj = jsValue->ToEcmaObject(vm); 392533eb0b6dSopenharmony_ci 392633eb0b6dSopenharmony_ci panda::JSNApi::NativeBindingInfo* data = panda::JSNApi::NativeBindingInfo::CreateNewInstance(); 392733eb0b6dSopenharmony_ci if (data == nullptr) { 392833eb0b6dSopenharmony_ci HILOG_ERROR("data is nullptr"); 392933eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_invalid_arg); 393033eb0b6dSopenharmony_ci } 393133eb0b6dSopenharmony_ci data->env = env; 393233eb0b6dSopenharmony_ci data->nativeValue = native_object; 393333eb0b6dSopenharmony_ci data->attachFunc = reinterpret_cast<void*>(AttachFuncCallback); 393433eb0b6dSopenharmony_ci data->attachData = reinterpret_cast<void*>(attach_cb); 393533eb0b6dSopenharmony_ci data->detachFunc = reinterpret_cast<void*>(DetachFuncCallback); 393633eb0b6dSopenharmony_ci data->detachData = reinterpret_cast<void*>(detach_cb); 393733eb0b6dSopenharmony_ci data->hint = hint; 393833eb0b6dSopenharmony_ci 393933eb0b6dSopenharmony_ci size_t nativeBindingSize = 7 * sizeof(void *); // 7 : params num 394033eb0b6dSopenharmony_ci Local<panda::NativePointerRef> value = panda::NativePointerRef::NewConcurrent(vm, data, 394133eb0b6dSopenharmony_ci [](void* env, void* data, void* info) { 394233eb0b6dSopenharmony_ci auto externalInfo = reinterpret_cast<panda::JSNApi::NativeBindingInfo*>(data); 394333eb0b6dSopenharmony_ci delete externalInfo; 394433eb0b6dSopenharmony_ci }, nullptr, nativeBindingSize); 394533eb0b6dSopenharmony_ci 394633eb0b6dSopenharmony_ci bool res = obj->ConvertToNativeBindingObject(vm, value); 394733eb0b6dSopenharmony_ci if (res) { 394833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 394933eb0b6dSopenharmony_ci } 395033eb0b6dSopenharmony_ci return napi_status::napi_generic_failure; 395133eb0b6dSopenharmony_ci} 395233eb0b6dSopenharmony_ci 395333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_print_string(napi_env env, napi_value value, std::string& result) 395433eb0b6dSopenharmony_ci{ 395533eb0b6dSopenharmony_ci CHECK_ENV(env); 395633eb0b6dSopenharmony_ci CHECK_ARG(env, value); 395733eb0b6dSopenharmony_ci 395833eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 395933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 396033eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 396133eb0b6dSopenharmony_ci 396233eb0b6dSopenharmony_ci if (nativeValue->IsString(vm)) { 396333eb0b6dSopenharmony_ci Local<panda::StringRef> stringVal(nativeValue); 396433eb0b6dSopenharmony_ci result = stringVal->ToString(vm); 396533eb0b6dSopenharmony_ci } 396633eb0b6dSopenharmony_ci return napi_clear_last_error(env); 396733eb0b6dSopenharmony_ci} 396833eb0b6dSopenharmony_ci 396933eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_run_event_loop(napi_env env, napi_event_mode mode) 397033eb0b6dSopenharmony_ci{ 397133eb0b6dSopenharmony_ci CHECK_ENV(env); 397233eb0b6dSopenharmony_ci 397333eb0b6dSopenharmony_ci if (mode < napi_event_mode_default || mode > napi_event_mode_nowait) { 397433eb0b6dSopenharmony_ci HILOG_ERROR("invalid mode %{public}d", static_cast<int32_t>(mode)); 397533eb0b6dSopenharmony_ci return napi_status::napi_invalid_arg; 397633eb0b6dSopenharmony_ci } 397733eb0b6dSopenharmony_ci 397833eb0b6dSopenharmony_ci auto nativeEngine = reinterpret_cast<NativeEngine*>(env); 397933eb0b6dSopenharmony_ci auto result = nativeEngine->RunEventLoop(mode); 398033eb0b6dSopenharmony_ci if (result != napi_status::napi_ok) { 398133eb0b6dSopenharmony_ci HILOG_ERROR("failed due to error %{public}d", static_cast<int32_t>(result)); 398233eb0b6dSopenharmony_ci return napi_set_last_error(env, result); 398333eb0b6dSopenharmony_ci } 398433eb0b6dSopenharmony_ci 398533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 398633eb0b6dSopenharmony_ci} 398733eb0b6dSopenharmony_ci 398833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_stop_event_loop(napi_env env) 398933eb0b6dSopenharmony_ci{ 399033eb0b6dSopenharmony_ci CHECK_ENV(env); 399133eb0b6dSopenharmony_ci 399233eb0b6dSopenharmony_ci auto nativeEngine = reinterpret_cast<NativeEngine*>(env); 399333eb0b6dSopenharmony_ci auto result = nativeEngine->StopEventLoop(); 399433eb0b6dSopenharmony_ci if (result != napi_status::napi_ok) { 399533eb0b6dSopenharmony_ci HILOG_ERROR("stop event loop failed due to error %{public}d", static_cast<int32_t>(result)); 399633eb0b6dSopenharmony_ci return napi_set_last_error(env, result); 399733eb0b6dSopenharmony_ci } 399833eb0b6dSopenharmony_ci return napi_clear_last_error(env); 399933eb0b6dSopenharmony_ci} 400033eb0b6dSopenharmony_ci 400133eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_create_ark_runtime(napi_env* env) 400233eb0b6dSopenharmony_ci{ 400333eb0b6dSopenharmony_ci if (NativeCreateEnv::g_createNapiEnvCallback == nullptr) { 400433eb0b6dSopenharmony_ci HILOG_ERROR("invalid create callback"); 400533eb0b6dSopenharmony_ci return napi_status::napi_invalid_arg; 400633eb0b6dSopenharmony_ci } 400733eb0b6dSopenharmony_ci napi_status result = NativeCreateEnv::g_createNapiEnvCallback(env); 400833eb0b6dSopenharmony_ci if (result == napi_ok) { 400933eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(*env)->GetEcmaVm(); 401033eb0b6dSopenharmony_ci panda::JSNApi::SetExecuteBufferMode(vm); 401133eb0b6dSopenharmony_ci } 401233eb0b6dSopenharmony_ci return result; 401333eb0b6dSopenharmony_ci} 401433eb0b6dSopenharmony_ci 401533eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_destroy_ark_runtime(napi_env* env) 401633eb0b6dSopenharmony_ci{ 401733eb0b6dSopenharmony_ci if (NativeCreateEnv::g_destroyNapiEnvCallback == nullptr) { 401833eb0b6dSopenharmony_ci HILOG_ERROR("invalid destroy callback"); 401933eb0b6dSopenharmony_ci return napi_status::napi_invalid_arg; 402033eb0b6dSopenharmony_ci } 402133eb0b6dSopenharmony_ci return NativeCreateEnv::g_destroyNapiEnvCallback(env); 402233eb0b6dSopenharmony_ci} 402333eb0b6dSopenharmony_ci 402433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_is_concurrent_function(napi_env env, napi_value value, bool* result) 402533eb0b6dSopenharmony_ci{ 402633eb0b6dSopenharmony_ci CHECK_ENV(env); 402733eb0b6dSopenharmony_ci CHECK_ARG(env, value); 402833eb0b6dSopenharmony_ci CHECK_ARG(env, result); 402933eb0b6dSopenharmony_ci 403033eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(value); 403133eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 403233eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 403333eb0b6dSopenharmony_ci 403433eb0b6dSopenharmony_ci *result = nativeValue->IsConcurrentFunction(vm); 403533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 403633eb0b6dSopenharmony_ci} 403733eb0b6dSopenharmony_ci 403833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_call_threadsafe_function_with_priority(napi_threadsafe_function func, 403933eb0b6dSopenharmony_ci void *data, 404033eb0b6dSopenharmony_ci napi_task_priority priority, 404133eb0b6dSopenharmony_ci bool isTail) 404233eb0b6dSopenharmony_ci{ 404333eb0b6dSopenharmony_ci CHECK_ENV(func); 404433eb0b6dSopenharmony_ci 404533eb0b6dSopenharmony_ci if (priority < napi_priority_immediate || priority > napi_priority_idle) { 404633eb0b6dSopenharmony_ci HILOG_ERROR("invalid priority %{public}d", static_cast<int32_t>(priority)); 404733eb0b6dSopenharmony_ci return napi_status::napi_invalid_arg; 404833eb0b6dSopenharmony_ci } 404933eb0b6dSopenharmony_ci auto safeAsyncWork = reinterpret_cast<NativeSafeAsyncWork*>(func); 405033eb0b6dSopenharmony_ci int32_t innerPriority = static_cast<int32_t>(priority); 405133eb0b6dSopenharmony_ci auto res = safeAsyncWork->PostTask(data, innerPriority, isTail); 405233eb0b6dSopenharmony_ci if (res != napi_ok) { 405333eb0b6dSopenharmony_ci HILOG_ERROR("post task failed due to error %{public}d", res); 405433eb0b6dSopenharmony_ci } 405533eb0b6dSopenharmony_ci return res; 405633eb0b6dSopenharmony_ci} 405733eb0b6dSopenharmony_ci 405833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_send_event(napi_env env, const std::function<void()> cb, napi_event_priority priority) 405933eb0b6dSopenharmony_ci{ 406033eb0b6dSopenharmony_ci CHECK_ENV(env); 406133eb0b6dSopenharmony_ci 406233eb0b6dSopenharmony_ci if (priority < napi_eprio_vip || priority > napi_eprio_idle) { 406333eb0b6dSopenharmony_ci HILOG_ERROR("invalid priority %{public}d", static_cast<int32_t>(priority)); 406433eb0b6dSopenharmony_ci return napi_status::napi_invalid_arg; 406533eb0b6dSopenharmony_ci } 406633eb0b6dSopenharmony_ci NativeEngine *eng = reinterpret_cast<NativeEngine *>(env); 406733eb0b6dSopenharmony_ci if (NativeEngine::IsAlive(eng)) { 406833eb0b6dSopenharmony_ci return eng->SendEvent(cb, priority); 406933eb0b6dSopenharmony_ci } else { 407033eb0b6dSopenharmony_ci return napi_status::napi_closing; 407133eb0b6dSopenharmony_ci } 407233eb0b6dSopenharmony_ci} 407333eb0b6dSopenharmony_ci 407433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_open_fast_native_scope(napi_env env, napi_fast_native_scope* scope) 407533eb0b6dSopenharmony_ci{ 407633eb0b6dSopenharmony_ci CHECK_ENV(env); 407733eb0b6dSopenharmony_ci CHECK_ARG(env, scope); 407833eb0b6dSopenharmony_ci 407933eb0b6dSopenharmony_ci auto engine = reinterpret_cast<NativeEngine*>(env); 408033eb0b6dSopenharmony_ci *scope = reinterpret_cast<napi_fast_native_scope>(new panda::JsiFastNativeScope(engine->GetEcmaVm())); 408133eb0b6dSopenharmony_ci return napi_clear_last_error(env); 408233eb0b6dSopenharmony_ci} 408333eb0b6dSopenharmony_ci 408433eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_close_fast_native_scope(napi_env env, napi_fast_native_scope scope) 408533eb0b6dSopenharmony_ci{ 408633eb0b6dSopenharmony_ci CHECK_ENV(env); 408733eb0b6dSopenharmony_ci CHECK_ARG(env, scope); 408833eb0b6dSopenharmony_ci 408933eb0b6dSopenharmony_ci delete reinterpret_cast<panda::JsiFastNativeScope*>(scope); 409033eb0b6dSopenharmony_ci return napi_clear_last_error(env); 409133eb0b6dSopenharmony_ci} 409233eb0b6dSopenharmony_ci 409333eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_get_shared_array_buffer_info(napi_env env, 409433eb0b6dSopenharmony_ci napi_value arraybuffer, 409533eb0b6dSopenharmony_ci void** data, 409633eb0b6dSopenharmony_ci size_t* byte_length) 409733eb0b6dSopenharmony_ci{ 409833eb0b6dSopenharmony_ci CHECK_ENV(env); 409933eb0b6dSopenharmony_ci CHECK_ARG(env, arraybuffer); 410033eb0b6dSopenharmony_ci CHECK_ARG(env, byte_length); 410133eb0b6dSopenharmony_ci 410233eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(arraybuffer); 410333eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 410433eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScope(vm); 410533eb0b6dSopenharmony_ci if (LIKELY(nativeValue->IsSharedArrayBuffer(vm))) { 410633eb0b6dSopenharmony_ci Local<panda::ArrayBufferRef> res(nativeValue); 410733eb0b6dSopenharmony_ci if (data != nullptr) { 410833eb0b6dSopenharmony_ci *data = res->GetBuffer(vm); 410933eb0b6dSopenharmony_ci } 411033eb0b6dSopenharmony_ci *byte_length = res->ByteLength(vm); 411133eb0b6dSopenharmony_ci } else { 411233eb0b6dSopenharmony_ci return napi_set_last_error(env, napi_arraybuffer_expected); 411333eb0b6dSopenharmony_ci } 411433eb0b6dSopenharmony_ci 411533eb0b6dSopenharmony_ci return napi_clear_last_error(env); 411633eb0b6dSopenharmony_ci} 411733eb0b6dSopenharmony_ci 411833eb0b6dSopenharmony_ciNAPI_EXTERN napi_status napi_encode(napi_env env, napi_value src, napi_value* result) 411933eb0b6dSopenharmony_ci{ 412033eb0b6dSopenharmony_ci CHECK_ENV(env); 412133eb0b6dSopenharmony_ci CHECK_ARG(env, src); 412233eb0b6dSopenharmony_ci CHECK_ARG(env, result); 412333eb0b6dSopenharmony_ci 412433eb0b6dSopenharmony_ci auto vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm(); 412533eb0b6dSopenharmony_ci panda::JsiFastNativeScope fastNativeScoper(vm); 412633eb0b6dSopenharmony_ci 412733eb0b6dSopenharmony_ci auto nativeValue = LocalValueFromJsValue(src); 412833eb0b6dSopenharmony_ci RETURN_STATUS_IF_FALSE(env, nativeValue->IsString(vm), napi_string_expected); 412933eb0b6dSopenharmony_ci Local<panda::StringRef> stringVal(nativeValue); 413033eb0b6dSopenharmony_ci Local<TypedArrayRef> typedArray = stringVal->EncodeIntoUint8Array(vm); 413133eb0b6dSopenharmony_ci *result = JsValueFromLocalValue(typedArray); 413233eb0b6dSopenharmony_ci 413333eb0b6dSopenharmony_ci return napi_clear_last_error(env); 413433eb0b6dSopenharmony_ci} 4135