support bigint napi for ArkNativeEngine

Signed-off-by: changjiaxing <changjiaxing1@huawei.com>
This commit is contained in:
changjiaxing
2022-03-11 12:07:30 +08:00
parent 00711c40f4
commit 65049a8d96
5 changed files with 158 additions and 3 deletions
+1
View File
@@ -44,6 +44,7 @@ template("ace_napi_impl") {
"ark_native_reference.cpp",
"native_value/ark_native_array.cpp",
"native_value/ark_native_array_buffer.cpp",
"native_value/ark_native_big_int.cpp",
"native_value/ark_native_boolean.cpp",
"native_value/ark_native_data_view.cpp",
"native_value/ark_native_date.cpp",
+14 -3
View File
@@ -26,6 +26,7 @@
#include "native_value/ark_native_array.h"
#include "native_value/ark_native_array_buffer.h"
#include "native_value/ark_native_big_int.h"
#include "native_value/ark_native_boolean.h"
#include "native_value/ark_native_data_view.h"
#include "native_value/ark_native_external.h"
@@ -53,6 +54,7 @@ using panda::NativePointerRef;
using panda::SymbolRef;
using panda::IntegerRef;
using panda::DateRef;
using panda::BigIntRef;
static constexpr auto PANDA_MAIN_FUNCTION = "_GLOBAL::func_main_0";
ArkNativeEngine::ArkNativeEngine(EcmaVM* vm, void* jsEngine) : NativeEngine(jsEngine), vm_(vm), topScope_(vm)
@@ -257,12 +259,12 @@ NativeValue* ArkNativeEngine::CreateNumber(double value)
NativeValue* ArkNativeEngine::CreateBigInt(int64_t value)
{
return nullptr;
return new ArkNativeBigInt(this, value);
}
NativeValue* ArkNativeEngine::CreateBigInt(uint64_t value)
{
return nullptr;
return new ArkNativeBigInt(this, value, true);
}
NativeValue* ArkNativeEngine::CreateString(const char* value, size_t length)
@@ -789,7 +791,16 @@ NativeValue* ArkNativeEngine::CreateDate(double value)
NativeValue* ArkNativeEngine::CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words)
{
return nullptr;
constexpr int bigintMod = 2; // 2 : used for even number judgment
bool sign = false;
if ((sign_bit % bigintMod) == 1) {
sign = true;
}
uint32_t size = (uint32_t)word_count;
Local<JSValueRef> value = BigIntRef::CreateBigWords(vm_, sign, size, words);
return new ArkNativeBigInt(this, value);
}
bool ArkNativeEngine::TriggerFatalException(NativeValue* error)
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2022 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_native_big_int.h"
using panda::BigIntRef;
ArkNativeBigInt::ArkNativeBigInt(ArkNativeEngine* engine, Local<JSValueRef> value) : ArkNativeValue(engine, value)
{}
ArkNativeBigInt::ArkNativeBigInt(ArkNativeEngine* engine, int64_t value)
: ArkNativeBigInt(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
{
auto vm = engine->GetEcmaVm();
LocalScope scope(vm);
Local<BigIntRef> object = BigIntRef::New(vm, value);
value_ = Global<BigIntRef>(vm, object);
}
ArkNativeBigInt::ArkNativeBigInt(ArkNativeEngine* engine, uint64_t value, bool isUnit64)
: ArkNativeBigInt(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
{
auto vm = engine->GetEcmaVm();
LocalScope scope(vm);
Local<BigIntRef> object = BigIntRef::New(vm, value);
value_ = Global<BigIntRef>(vm, object);
}
ArkNativeBigInt::~ArkNativeBigInt() {}
void* ArkNativeBigInt::GetInterface(int interfaceId)
{
return (NativeBigint::INTERFACE_ID == interfaceId) ? (NativeBigint*)this : nullptr;
}
void ArkNativeBigInt::Uint64Value(uint64_t* cValue, bool* lossless)
{
auto vm = engine_->GetEcmaVm();
LocalScope scope(vm);
Global<BigIntRef> value = value_;
value->BigIntToUint64(vm, cValue, lossless);
}
void ArkNativeBigInt::Int64Value(int64_t* cValue, bool* lossless)
{
auto vm = engine_->GetEcmaVm();
LocalScope scope(vm);
Global<BigIntRef> value = value_;
value->BigIntToInt64(vm, cValue, lossless);
}
bool ArkNativeBigInt::GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words)
{
auto vm = engine_->GetEcmaVm();
LocalScope scope(vm);
Global<BigIntRef> value = value_;
if (wordCount == nullptr) {
return false;
}
size_t size = static_cast<size_t>(value->GetWordsArraySize());
if (signBit == nullptr && words == nullptr) {
*wordCount = size;
return true;
} else if (signBit != nullptr && words != nullptr) {
if (size > *wordCount) {
size = *wordCount;
}
bool sign = false;
value->GetWordsArray(&sign, size, words);
if (sign) {
*signBit = 1;
} else {
*signBit = 0;
}
*wordCount = size;
return true;
}
return false;
}
ArkNativeBigInt::operator int64_t()
{
int64_t cValue = 0;
bool lossless = true;
Int64Value(&cValue, &lossless);
return cValue;
}
ArkNativeBigInt::operator uint64_t()
{
uint64_t cValue = 0;
bool lossless = true;
Uint64Value(&cValue, &lossless);
return cValue;
}
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022 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.
*/
#ifndef FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_BIGINT_H
#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_BIGINT_H
#include "ark_native_value.h"
class ArkNativeBigInt : public ArkNativeValue, public NativeBigint {
public:
ArkNativeBigInt(ArkNativeEngine* engine, Local<JSValueRef> value);
ArkNativeBigInt(ArkNativeEngine* engine, int64_t value);
ArkNativeBigInt(ArkNativeEngine* engine, uint64_t value, bool isUnit64);
~ArkNativeBigInt() override;
void* GetInterface(int interfaceId) override;
virtual operator int64_t() override;
virtual operator uint64_t() override;
virtual void Uint64Value(uint64_t* cValue, bool* lossless = nullptr) override;
virtual void Int64Value(int64_t* cValue, bool* lossless = nullptr) override;
virtual bool GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words) override;
};
#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_BIGINT_H */
@@ -70,6 +70,8 @@ NativeValueType ArkNativeValue::TypeOf()
result = NATIVE_UNDEFINED;
} else if (value->IsSymbol()) {
result = NATIVE_SYMBOL;
} else if (value->IsBigInt()) {
result = NATIVE_BIGINT;
} else if (value->IsObject()) {
result = NATIVE_OBJECT;
} else {