From 01beb40ebd542e230b6b865409c79e53cffde9bf Mon Sep 17 00:00:00 2001 From: tuxiaohang Date: Tue, 26 Nov 2024 20:56:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9napi=5Fhas=5Fown=5Fproperty?= =?UTF-8?q?=E7=AD=89=E6=8E=A5=E5=8F=A3=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: https://gitee.com/openharmony/arkui_napi/issues/IBFBOZ Signed-off-by: tuxiaohang Change-Id: Ia92f2a568d1b87c6d7b72ef6382e65a1fc66e006 --- native_engine/native_api.cpp | 27 +++++++++++++++++++++++---- native_engine/native_engine.cpp | 7 +++++++ native_engine/native_engine.h | 2 ++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/native_engine/native_api.cpp b/native_engine/native_api.cpp index 3abaff9a..fd16c37a 100644 --- a/native_engine/native_api.cpp +++ b/native_engine/native_api.cpp @@ -57,6 +57,7 @@ using panda::ecmascript::EcmaVM; static constexpr size_t MAX_BYTE_LENGTH = 2097152; static constexpr size_t ONEMIB_BYTE_SIZE = 1048576; static constexpr size_t SMALL_STRING_SIZE = 16; +static constexpr int32_t API_VERSION_FOURTEEN = 16; class HandleScopeWrapper { public: @@ -512,7 +513,12 @@ NAPI_EXTERN napi_status napi_create_type_error(napi_env env, napi_value code, na auto msgValue = LocalValueFromJsValue(msg); RETURN_STATUS_IF_FALSE(env, msgValue->IsString(vm), napi_invalid_arg); - Local errorVal = panda::Exception::Error(vm, msgValue); + Local errorVal; + if (reinterpret_cast(env)->GetRealApiVersion() < API_VERSION_FOURTEEN) { + errorVal = panda::Exception::Error(vm, msgValue); + } else { + errorVal = panda::Exception::TypeError(vm, msgValue); + } if (code != nullptr) { Local codeKey = panda::StringRef::NewFromUtf8(vm, "code"); Local errorObj(errorVal); @@ -540,7 +546,12 @@ NAPI_EXTERN napi_status napi_create_range_error(napi_env env, napi_value code, n auto msgValue = LocalValueFromJsValue(msg); RETURN_STATUS_IF_FALSE(env, msgValue->IsString(vm), napi_invalid_arg); - Local errorVal = panda::Exception::Error(vm, msgValue); + Local errorVal; + if (reinterpret_cast(env)->GetRealApiVersion() < API_VERSION_FOURTEEN) { + errorVal = panda::Exception::Error(vm, msgValue); + } else { + errorVal = panda::Exception::RangeError(vm, msgValue); + } if (code != nullptr) { Local codeKey = panda::StringRef::NewFromUtf8(vm, "code"); Local errorObj(errorVal); @@ -932,8 +943,16 @@ NAPI_EXTERN napi_status napi_has_own_property(napi_env env, napi_value object, n auto vm = reinterpret_cast(env)->GetEcmaVm(); panda::JsiFastNativeScope fastNativeScope(vm); - auto hasResult = JSNApi::NapiHasOwnProperty(vm, reinterpret_cast(object), - reinterpret_cast(key)); + Local hasResult; + + if (reinterpret_cast(env)->GetRealApiVersion() < API_VERSION_FOURTEEN) { + hasResult = JSNApi::NapiHasProperty(vm, reinterpret_cast(object), + reinterpret_cast(key)); + } else { + hasResult = JSNApi::NapiHasOwnProperty(vm, reinterpret_cast(object), + reinterpret_cast(key)); + } + RETURN_STATUS_IF_FALSE(env, NapiStatusValidationCheck(hasResult), napi_object_expected); if (result) { *result = hasResult->BooleaValue(vm); diff --git a/native_engine/native_engine.cpp b/native_engine/native_engine.cpp index 9af9544f..d97a3ddd 100644 --- a/native_engine/native_engine.cpp +++ b/native_engine/native_engine.cpp @@ -28,6 +28,7 @@ constexpr size_t NAME_BUFFER_SIZE = 64; static constexpr auto PANDA_MAIN_FUNCTION = "_GLOBAL::func_main_0"; static constexpr int32_t API11 = 11; +static constexpr int32_t API_VERSION_MASK = 1000; using panda::JSValueRef; using panda::Local; @@ -658,6 +659,7 @@ NativeEngine* NativeEngine::GetHostEngine() const void NativeEngine::SetApiVersion(int32_t apiVersion) { apiVersion_ = apiVersion; + realApiVersion_ = apiVersion % API_VERSION_MASK; } int32_t NativeEngine::GetApiVersion() @@ -665,6 +667,11 @@ int32_t NativeEngine::GetApiVersion() return apiVersion_; } +int32_t NativeEngine::GetRealApiVersion() +{ + return realApiVersion_; +} + bool NativeEngine::IsApplicationApiVersionAPI11Plus() { return apiVersion_ > API11; diff --git a/native_engine/native_engine.h b/native_engine/native_engine.h index f9a87788..e821d7eb 100644 --- a/native_engine/native_engine.h +++ b/native_engine/native_engine.h @@ -363,6 +363,7 @@ public: virtual NativeEngine* GetHostEngine() const; virtual void SetApiVersion(int32_t apiVersion); virtual int32_t GetApiVersion(); + virtual int32_t GetRealApiVersion(); virtual bool IsApplicationApiVersionAPI11Plus(); virtual napi_status AddCleanupHook(CleanupCallback fun, void* arg); @@ -605,6 +606,7 @@ private: uv_sem_t uvSem_; // Application's sdk version int32_t apiVersion_ = 8; + int32_t realApiVersion_ = 8; // the old worker api use before api9, the new worker api start with api9 enum JSThreadType { MAIN_THREAD, WORKER_THREAD, TASKPOOL_THREAD, RESTRICTEDWORKER_THREAD, NATIVE_THREAD }; -- Gitee