1 /*
2 * Copyright (c) 2021 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 "quickjs_native_big_int.h"
17
QuickJSNativeBigInt(QuickJSNativeEngine * engine,JSValue value)18 QuickJSNativeBigInt::QuickJSNativeBigInt(QuickJSNativeEngine* engine, JSValue value) : QuickJSNativeValue(engine, value)
19 {}
20
QuickJSNativeBigInt(QuickJSNativeEngine * engine,int64_t value)21 QuickJSNativeBigInt::QuickJSNativeBigInt(QuickJSNativeEngine* engine, int64_t value)
22 : QuickJSNativeBigInt(engine, JS_NewBigInt64(engine->GetContext(), value))
23 {}
24
QuickJSNativeBigInt(QuickJSNativeEngine * engine,uint64_t value,bool isUnit64)25 QuickJSNativeBigInt::QuickJSNativeBigInt(QuickJSNativeEngine* engine, uint64_t value, bool isUnit64)
26 : QuickJSNativeBigInt(engine, JS_NewBigUint64(engine->GetContext(), value))
27 {}
28
~QuickJSNativeBigInt()29 QuickJSNativeBigInt::~QuickJSNativeBigInt() {}
30
GetInterface(int interfaceId)31 void* QuickJSNativeBigInt::GetInterface(int interfaceId)
32 {
33 return (NativeBigint::INTERFACE_ID == interfaceId) ? (NativeBigint*)this : nullptr;
34 }
35
Uint64Value(uint64_t * cValue,bool * lossless)36 void QuickJSNativeBigInt::Uint64Value(uint64_t* cValue, bool* lossless)
37 {
38 JS_ToUInt64WithBigInt(engine_->GetContext(), this->value_, cValue, lossless);
39 }
Int64Value(int64_t * cValue,bool * lossless)40 void QuickJSNativeBigInt::Int64Value(int64_t* cValue, bool* lossless)
41 {
42 JS_ToInt64WithBigInt(engine_->GetContext(), this->value_, cValue, lossless);
43 }
44
GetWordsArray(int * signBit,size_t * wordCount,uint64_t * words)45 bool QuickJSNativeBigInt::GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words)
46 {
47 return JS_GetBigIntWords(engine_->GetContext(), this->value_, signBit, wordCount, words);
48 }
49
operator int64_t()50 QuickJSNativeBigInt::operator int64_t()
51 {
52 int64_t cValue = 0;
53 bool lossless = true;
54 Int64Value(&cValue, &lossless);
55 return cValue;
56 }
57
operator uint64_t()58 QuickJSNativeBigInt::operator uint64_t()
59 {
60 uint64_t cValue = 0;
61 bool lossless = true;
62 Uint64Value(&cValue, &lossless);
63 return cValue;
64 }
65