diff --git a/interfaces/inner_api/cjffi/ark_interop/ark_interop_bigint.cpp b/interfaces/inner_api/cjffi/ark_interop/ark_interop_bigint.cpp index cf8a58eb302ad4ec59af2d138d28310a0d946320..3108cbdf38449de534c5448a7fe8c3f90c9f4dc8 100644 --- a/interfaces/inner_api/cjffi/ark_interop/ark_interop_bigint.cpp +++ b/interfaces/inner_api/cjffi/ark_interop/ark_interop_bigint.cpp @@ -1,114 +1,115 @@ -/* -* Copyright (c) 2024 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "ark_interop_napi.h" -#include "ark_interop_external.h" -#include "ark_interop_internal.h" -#include "ark_interop_macro.h" -#include "ark_interop_log.h" - -ARKTS_Value ARKTS_CreateBigInt(ARKTS_Env env, int64_t value) -{ - ARKTS_ASSERT_P(env, "env is null"); - - auto vm = P_CAST(env, panda::EcmaVM*); - auto result = panda::BigIntRef::New(vm, value); - return ARKTS_FromHandle(result); -} - -static bool ReverseBytes(uint8_t dst[], size_t size) -{ - auto powOf2 = (size & 1) == 0; - if (!powOf2) { - return false; - } - auto half = size >> 1; - for (size_t i = 0;i < half; ++i) { - auto temp = dst[i]; - dst[i] = dst[size - i - 1]; - dst[size - i - 1] = temp; - } - return true; -} - -constexpr int WORD_BYTES = sizeof(uint64_t) / sizeof(uint8_t); -constexpr int BYTE_BITS = 8; - -ARKTS_Value ARKTS_CreateBigIntWithBytes(ARKTS_Env env, bool isNegative, int64_t size, const uint8_t bytes[]) -{ - ARKTS_ASSERT_P(env, "env is null"); - ARKTS_ASSERT_P(size != 0 && size <= int64_t(0xFFFF'FFFF) * WORD_BYTES, "size is invalid"); - ARKTS_ASSERT_P(bytes, "bytes is null"); - - auto vm = P_CAST(env, panda::EcmaVM*); - - auto firstBytes = size % WORD_BYTES; - auto wholeU64Cnt = size / WORD_BYTES; - auto totalCnt = firstBytes ? wholeU64Cnt + 1 : wholeU64Cnt; - - std::vector u64v; - u64v.resize(totalCnt); - - int64_t wordStart = 0; - int64_t wordEnd = firstBytes ? firstBytes : WORD_BYTES; - for (int64_t index = totalCnt - 1;index >= 0; --index) { - uint64_t value = 0; - for (auto i = wordStart;i < wordEnd; ++i) { - value = (value << BYTE_BITS) | bytes[i]; - } - u64v[index] = value; - - wordStart = wordEnd; - wordEnd += WORD_BYTES; - } - - auto result = panda::BigIntRef::CreateBigWords(vm, isNegative, totalCnt, u64v.data()); - return ARKTS_FromHandle(result); -} - -bool ARKTS_IsBigInt(ARKTS_Env env, ARKTS_Value value) -{ - ARKTS_ASSERT_F(env, "env is null"); - auto tag = BIT_CAST(value, panda::JSValueRef); - if (!tag.IsHeapObject()) { - return false; - } - tag = *P_CAST(value, panda::JSValueRef*); - auto vm = P_CAST(env, panda::EcmaVM*); - return tag.IsBigInt(vm); -} - -int64_t ARKTS_BigIntGetByteSize(ARKTS_Env env, ARKTS_Value value) -{ - ARKTS_ASSERT_I(ARKTS_IsBigInt(env, value), "value is not bigint"); - auto vm = P_CAST(env, panda::EcmaVM*); - - auto bigint = P_CAST(value, panda::BigIntRef*); - return bigint->GetWordsArraySize(vm) * WORD_BYTES; -} - -void ARKTS_BigIntReadBytes(ARKTS_Env env, ARKTS_Value value, bool* isNegative, int64_t byteCount, uint8_t bytes[]) -{ - ARKTS_ASSERT_V(env, "env is null"); - ARKTS_ASSERT_V(bytes, "bytes is null"); - ARKTS_ASSERT_V(ARKTS_IsBigInt(env, value), "value is not bigint"); - auto vm = P_CAST(env, panda::EcmaVM*); - - auto bigint = BIT_CAST(value, panda::Local); - auto u64cnt = bigint->GetWordsArraySize(vm); - ARKTS_ASSERT_V(byteCount >= u64cnt * WORD_BYTES, "byteCount not enough"); - bigint->GetWordsArray(vm, isNegative, u64cnt, reinterpret_cast(bytes)); - ARKTS_ASSERT_V(ReverseBytes(bytes, byteCount), "ReverseBytes failed"); +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "ark_interop_napi.h" +#include "ark_interop_external.h" +#include "ark_interop_internal.h" +#include "ark_interop_macro.h" +#include "ark_interop_log.h" + +ARKTS_Value ARKTS_CreateBigInt(ARKTS_Env env, int64_t value) +{ + ARKTS_ASSERT_P(env, "env is null"); + + auto vm = P_CAST(env, panda::EcmaVM*); + auto result = panda::BigIntRef::New(vm, value); + return ARKTS_FromHandle(result); +} + +static bool ReverseBytes(uint8_t dst[], size_t size) +{ + auto powOf2 = (size & 1) == 0; + if (!powOf2) { + return false; + } + auto half = size >> 1; + for (size_t i = 0;i < half; ++i) { + auto temp = dst[i]; + dst[i] = dst[size - i - 1]; + dst[size - i - 1] = temp; + } + return true; +} + +constexpr int WORD_BYTES = sizeof(uint64_t) / sizeof(uint8_t); +constexpr int BYTE_BITS = 8; + +ARKTS_Value ARKTS_CreateBigIntWithBytes(ARKTS_Env env, bool isNegative, int64_t size, const uint8_t bytes[]) +{ + ARKTS_ASSERT_P(env, "env is null"); + ARKTS_ASSERT_P(size != 0 && size <= int64_t(0xFFFF'FFFF) * WORD_BYTES, "size is invalid"); + ARKTS_ASSERT_P(bytes, "bytes is null"); + + auto vm = P_CAST(env, panda::EcmaVM*); + + auto firstBytes = size % WORD_BYTES; + auto wholeU64Cnt = size / WORD_BYTES; + auto totalCnt = firstBytes ? wholeU64Cnt + 1 : wholeU64Cnt; + + std::vector u64v; + u64v.resize(totalCnt); + + int64_t wordStart = 0; + int64_t wordEnd = firstBytes ? firstBytes : WORD_BYTES; + for (int64_t index = totalCnt - 1;index >= 0; --index) { + uint64_t value = 0; + for (auto i = wordStart;i < wordEnd; ++i) { + value = (value << BYTE_BITS) | bytes[i]; + } + u64v[index] = value; + + wordStart = wordEnd; + wordEnd += WORD_BYTES; + } + + auto result = panda::BigIntRef::CreateBigWords(vm, isNegative, totalCnt, u64v.data()); + return ARKTS_FromHandle(result); +} + +bool ARKTS_IsBigInt(ARKTS_Env env, ARKTS_Value value) +{ + ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); + auto tag = BIT_CAST(value, panda::JSValueRef); + if (!tag.IsHeapObject()) { + return false; + } + tag = *P_CAST(value, panda::JSValueRef*); + auto vm = P_CAST(env, panda::EcmaVM*); + return tag.IsBigInt(vm); +} + +int64_t ARKTS_BigIntGetByteSize(ARKTS_Env env, ARKTS_Value value) +{ + ARKTS_ASSERT_I(ARKTS_IsBigInt(env, value), "value is not bigint"); + auto vm = P_CAST(env, panda::EcmaVM*); + + auto bigint = P_CAST(value, panda::BigIntRef*); + return bigint->GetWordsArraySize(vm) * WORD_BYTES; +} + +void ARKTS_BigIntReadBytes(ARKTS_Env env, ARKTS_Value value, bool* isNegative, int64_t byteCount, uint8_t bytes[]) +{ + ARKTS_ASSERT_V(env, "env is null"); + ARKTS_ASSERT_V(bytes, "bytes is null"); + ARKTS_ASSERT_V(ARKTS_IsBigInt(env, value), "value is not bigint"); + auto vm = P_CAST(env, panda::EcmaVM*); + + auto bigint = BIT_CAST(value, panda::Local); + auto u64cnt = bigint->GetWordsArraySize(vm); + ARKTS_ASSERT_V(byteCount >= u64cnt * WORD_BYTES, "byteCount not enough"); + bigint->GetWordsArray(vm, isNegative, u64cnt, reinterpret_cast(bytes)); + ARKTS_ASSERT_V(ReverseBytes(bytes, byteCount), "ReverseBytes failed"); } \ No newline at end of file diff --git a/interfaces/inner_api/cjffi/ark_interop/ark_interop_napi.cpp b/interfaces/inner_api/cjffi/ark_interop/ark_interop_napi.cpp index 2f2eca1c91fe8b251fb36cbb8cf5b70709870176..ce53d5811976f296ba7cb69a36c0962fd348f85e 100644 --- a/interfaces/inner_api/cjffi/ark_interop/ark_interop_napi.cpp +++ b/interfaces/inner_api/cjffi/ark_interop/ark_interop_napi.cpp @@ -293,6 +293,8 @@ ARKTS_Value ARKTS_CreateFunc(ARKTS_Env env, int64_t lambdaId) bool ARKTS_IsClass(ARKTS_Env env, ARKTS_Value value) { + ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto tag = BIT_CAST(value, JSValueRef); if (!tag.IsHeapObject()) { return false; @@ -474,6 +476,7 @@ ARKTS_Value ARKTS_GetElement(ARKTS_Env env, ARKTS_Value array, uint32_t index) bool ARKTS_IsArray(ARKTS_Env env, ARKTS_Value value) { ARKTS_ASSERT_F(env, "env is NULL"); + ARKTS_ASSERT_F(value, "value is null"); auto v = BIT_CAST(value, JSValueRef); if (!v.IsHeapObject()) { return false; @@ -508,6 +511,7 @@ ARKTS_Value ARKTS_CreateArrayBufferWithData(ARKTS_Env env, void* buffer, int32_t bool ARKTS_IsArrayBuffer(ARKTS_Env env, ARKTS_Value value) { ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto vm = P_CAST(env, EcmaVM*); panda::JsiFastNativeScope fastNativeScope(vm); auto tag = BIT_CAST(value, JSValueRef); @@ -585,6 +589,7 @@ ARKTS_Value ARKTS_CreateExternal(ARKTS_Env env, void* data) bool ARKTS_IsExternal(ARKTS_Env env, ARKTS_Value value) { ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto prime = BIT_CAST(value, JSValueRef); if (!prime.IsHeapObject()) { return false; @@ -652,6 +657,7 @@ void ARKTS_PromiseCapabilityReject(ARKTS_Env env, ARKTS_Promise prom, ARKTS_Valu bool ARKTS_IsPromise(ARKTS_Env env, ARKTS_Value value) { ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto v = BIT_CAST(value, JSValueRef); if (!v.IsHeapObject()) { return false; diff --git a/interfaces/inner_api/cjffi/ark_interop/ark_interop_object.cpp b/interfaces/inner_api/cjffi/ark_interop/ark_interop_object.cpp index a92afe708272301e81504634a4ce47ff98c81360..19684b5ca536c5bb485f96fa99feb38e34543836 100644 --- a/interfaces/inner_api/cjffi/ark_interop/ark_interop_object.cpp +++ b/interfaces/inner_api/cjffi/ark_interop/ark_interop_object.cpp @@ -46,6 +46,7 @@ ARKTS_Value ARKTS_CreateObject(ARKTS_Env env) bool ARKTS_IsHeapObject(ARKTS_Value value) { + ARKTS_ASSERT_F(value, "value is null"); auto v = BIT_CAST(value, JSValueRef); return v.IsHeapObject(); } @@ -53,6 +54,7 @@ bool ARKTS_IsHeapObject(ARKTS_Value value) bool ARKTS_IsObject(ARKTS_Env env, ARKTS_Value value) { ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto vm = P_CAST(env, EcmaVM*); panda::JsiFastNativeScope fastNativeScope(vm); auto v = BIT_CAST(value, JSValueRef); diff --git a/interfaces/inner_api/cjffi/ark_interop/ark_interop_string.cpp b/interfaces/inner_api/cjffi/ark_interop/ark_interop_string.cpp index ca02417a7c045324d513f8c46647b0ad4dcdc9f7..0ed4b045a34fc93b86635d394d356e8facee365c 100644 --- a/interfaces/inner_api/cjffi/ark_interop/ark_interop_string.cpp +++ b/interfaces/inner_api/cjffi/ark_interop/ark_interop_string.cpp @@ -44,6 +44,8 @@ ARKTS_Value ARKTS_CreateUtf8(ARKTS_Env env, const char* value, int32_t size) bool ARKTS_IsString(ARKTS_Env env, ARKTS_Value value) { + ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto vm = P_CAST(env, EcmaVM*); panda::JsiFastNativeScope fastNativeScope(vm); auto v = BIT_CAST(value, JSValueRef); @@ -57,6 +59,7 @@ bool ARKTS_IsString(ARKTS_Env env, ARKTS_Value value) int32_t ARKTS_GetValueUtf8Size(ARKTS_Env env, ARKTS_Value value) { ARKTS_ASSERT_I(env, "env is null"); + ARKTS_ASSERT_I(value, "value is null"); auto vm = P_CAST(env, EcmaVM*); panda::JsiFastNativeScope fastNativeScope(vm); ARKTS_ASSERT_I(ARKTS_IsString(env, value), "not a string"); @@ -66,9 +69,11 @@ int32_t ARKTS_GetValueUtf8Size(ARKTS_Env env, ARKTS_Value value) int32_t ARKTS_GetValueUtf8(ARKTS_Env env, ARKTS_Value value, int32_t capacity, char* buffer) { + ARKTS_ASSERT_I(env, "env is null"); auto vm = P_CAST(env, EcmaVM*); panda::JsiFastNativeScope fastNativeScope(vm); ARKTS_ASSERT_I(ARKTS_IsString(env, value), "not a string"); + ARKTS_ASSERT_I(capacity == 0 || buffer, "buffer is null when length not 0"); auto v = BIT_CAST(value, Local); return v->WriteUtf8(vm, buffer, capacity, true); } diff --git a/interfaces/inner_api/cjffi/ark_interop/ark_interop_symbol.cpp b/interfaces/inner_api/cjffi/ark_interop/ark_interop_symbol.cpp index 707581c37b93e57b54732c7f026e0f1aaff98438..5c65e8f2d1d4be989c6d15e933309f78d479599c 100644 --- a/interfaces/inner_api/cjffi/ark_interop/ark_interop_symbol.cpp +++ b/interfaces/inner_api/cjffi/ark_interop/ark_interop_symbol.cpp @@ -38,6 +38,8 @@ ARKTS_Value ARKTS_CreateSymbol(ARKTS_Env env, const char* description, int32_t l bool ARKTS_IsSymbol(ARKTS_Env env, ARKTS_Value value) { + ARKTS_ASSERT_F(env, "env is null"); + ARKTS_ASSERT_F(value, "value is null"); auto tag = BIT_CAST(value, JSValueRef); if (!tag.IsHeapObject()) { return false; @@ -49,7 +51,6 @@ bool ARKTS_IsSymbol(ARKTS_Env env, ARKTS_Value value) const char* ARKTS_GetSymbolDesc(ARKTS_Env env, ARKTS_Value value) { - ARKTS_ASSERT_P(env, "env is null"); ARKTS_ASSERT_P(ARKTS_IsSymbol(env, value), "value is not a symbol"); auto vm = P_CAST(env, EcmaVM*);