1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ark_native_big_int.h"
17 using panda::BigIntRef;
18
ArkNativeBigInt(ArkNativeEngine * engine,Local<JSValueRef> value)19 ArkNativeBigInt::ArkNativeBigInt(ArkNativeEngine* engine, Local<JSValueRef> value) : ArkNativeValue(engine, value)
20 {}
21
ArkNativeBigInt(ArkNativeEngine * engine,int64_t value)22 ArkNativeBigInt::ArkNativeBigInt(ArkNativeEngine* engine, int64_t value)
23 : ArkNativeBigInt(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
24 {
25 auto vm = engine->GetEcmaVm();
26 LocalScope scope(vm);
27 Local<BigIntRef> object = BigIntRef::New(vm, value);
28 value_ = Global<BigIntRef>(vm, object);
29 }
30
ArkNativeBigInt(ArkNativeEngine * engine,uint64_t value,bool isUnit64)31 ArkNativeBigInt::ArkNativeBigInt(ArkNativeEngine* engine, uint64_t value, bool isUnit64)
32 : ArkNativeBigInt(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
33 {
34 auto vm = engine->GetEcmaVm();
35 LocalScope scope(vm);
36 Local<BigIntRef> object = BigIntRef::New(vm, value);
37 value_ = Global<BigIntRef>(vm, object);
38 }
39
~ArkNativeBigInt()40 ArkNativeBigInt::~ArkNativeBigInt() {}
41
GetInterface(int interfaceId)42 void* ArkNativeBigInt::GetInterface(int interfaceId)
43 {
44 return (NativeBigint::INTERFACE_ID == interfaceId) ? (NativeBigint*)this : nullptr;
45 }
46
Uint64Value(uint64_t * cValue,bool * lossless)47 void ArkNativeBigInt::Uint64Value(uint64_t* cValue, bool* lossless)
48 {
49 auto vm = engine_->GetEcmaVm();
50 LocalScope scope(vm);
51 Global<BigIntRef> value = value_;
52 value->BigIntToUint64(vm, cValue, lossless);
53 }
54
Int64Value(int64_t * cValue,bool * lossless)55 void ArkNativeBigInt::Int64Value(int64_t* cValue, bool* lossless)
56 {
57 auto vm = engine_->GetEcmaVm();
58 LocalScope scope(vm);
59 Global<BigIntRef> value = value_;
60 value->BigIntToInt64(vm, cValue, lossless);
61 }
62
GetWordsArray(int * signBit,size_t * wordCount,uint64_t * words)63 bool ArkNativeBigInt::GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words)
64 {
65 auto vm = engine_->GetEcmaVm();
66 LocalScope scope(vm);
67 Global<BigIntRef> value = value_;
68 if (wordCount == nullptr) {
69 return false;
70 }
71 size_t size = static_cast<size_t>(value->GetWordsArraySize());
72 if (signBit == nullptr && words == nullptr) {
73 *wordCount = size;
74 return true;
75 } else if (signBit != nullptr && words != nullptr) {
76 if (size > *wordCount) {
77 size = *wordCount;
78 }
79 bool sign = false;
80 value->GetWordsArray(&sign, size, words);
81 if (sign) {
82 *signBit = 1;
83 } else {
84 *signBit = 0;
85 }
86 *wordCount = size;
87 return true;
88 }
89 return false;
90 }
91
operator int64_t()92 ArkNativeBigInt::operator int64_t()
93 {
94 int64_t cValue = 0;
95 bool lossless = true;
96 Int64Value(&cValue, &lossless);
97 return cValue;
98 }
99
operator uint64_t()100 ArkNativeBigInt::operator uint64_t()
101 {
102 uint64_t cValue = 0;
103 bool lossless = true;
104 Uint64Value(&cValue, &lossless);
105 return cValue;
106 }