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 "jerryscript_native_big_int.h"
17
JerryScriptNativeBigInt(JerryScriptNativeEngine * engine,jerry_value_t value)18 JerryScriptNativeBigInt::JerryScriptNativeBigInt(JerryScriptNativeEngine* engine, jerry_value_t value)
19 : JerryScriptNativeValue(engine, value)
20 {}
21
JerryScriptNativeBigInt(JerryScriptNativeEngine * engine,int64_t value)22 JerryScriptNativeBigInt::JerryScriptNativeBigInt(JerryScriptNativeEngine* engine, int64_t value)
23 : JerryScriptNativeBigInt(engine, CreateBigintByInt(value))
24 {}
25
26 #if JERRY_API_MINOR_VERSION > 3 // jerryscript2.3: 3, jerryscript2.4: 4
JerryScriptNativeBigInt(JerryScriptNativeEngine * engine,uint64_t value,bool isUnit64)27 JerryScriptNativeBigInt::JerryScriptNativeBigInt(JerryScriptNativeEngine* engine, uint64_t value, bool isUnit64)
28 : JerryScriptNativeBigInt(engine, jerry_create_bigint(&value, 1, false))
29 {}
30 #endif
31
~JerryScriptNativeBigInt()32 JerryScriptNativeBigInt::~JerryScriptNativeBigInt() {}
33
GetInterface(int interfaceId)34 void* JerryScriptNativeBigInt::GetInterface(int interfaceId)
35 {
36 return (NativeBigint::INTERFACE_ID == interfaceId) ? (NativeBigint*)this : nullptr;
37 }
38
Uint64Value(uint64_t * cValue,bool * lossless)39 void JerryScriptNativeBigInt::Uint64Value(uint64_t* cValue, bool* lossless)
40 {
41 #if JERRY_API_MINOR_VERSION > 3 // jerryscript2.3: 3, jerryscript2.4: 4
42 size_t size = (size_t)jerry_get_bigint_size_in_digits(value_);
43 if (size == 1) {
44 *lossless = true;
45 } else {
46 *lossless = false;
47 }
48 bool sign = false;
49 jerry_get_bigint_digits(value_, cValue, 1, &sign);
50 #endif
51 }
Int64Value(int64_t * cValue,bool * lossless)52 void JerryScriptNativeBigInt::Int64Value(int64_t* cValue, bool* lossless)
53 {
54 #if JERRY_API_MINOR_VERSION > 3 // jerryscript2.3: 3, jerryscript2.4: 4
55 size_t size = (size_t)jerry_get_bigint_size_in_digits(value_);
56 if (size == 1) {
57 *lossless = true;
58 } else {
59 *lossless = false;
60 }
61 bool sign = false;
62 uint64_t uintValue = 0;
63 jerry_get_bigint_digits(value_, &uintValue, 1, &sign);
64 auto intValue = static_cast<int64_t>(uintValue);
65 if (sign) {
66 *cValue = -intValue;
67 } else {
68 *cValue = intValue;
69 }
70 #endif
71 }
72
operator int64_t()73 JerryScriptNativeBigInt::operator int64_t()
74 {
75 int64_t cValue = 0;
76 bool lossless = true;
77 Int64Value(&cValue, &lossless);
78 return cValue;
79 }
80
operator uint64_t()81 JerryScriptNativeBigInt::operator uint64_t()
82 {
83 uint64_t cValue = 0;
84 bool lossless = true;
85 Uint64Value(&cValue, &lossless);
86 return cValue;
87 }
88
GetWordsArray(int * signBit,size_t * wordCount,uint64_t * words)89 bool JerryScriptNativeBigInt::GetWordsArray(int* signBit, size_t* wordCount, uint64_t* words)
90 {
91 if (wordCount == nullptr) {
92 return false;
93 }
94 #if JERRY_API_MINOR_VERSION > 3 // jerryscript2.3: 3, jerryscript2.4: 4
95 size_t size = (size_t)jerry_get_bigint_size_in_digits(value_);
96
97 if (signBit == nullptr && words == nullptr) {
98 *wordCount = size;
99 return true;
100 } else if (signBit != nullptr && words != nullptr) {
101 if (size > *wordCount) {
102 size = *wordCount;
103 }
104 bool sign = false;
105 jerry_get_bigint_digits(value_, words, size, &sign);
106 if (sign) {
107 *signBit = 1;
108 } else {
109 *signBit = 0;
110 }
111 *wordCount = size;
112 return true;
113 }
114 return false;
115 #else
116 return true;
117 #endif
118 }
119
CreateBigintByInt(int64_t value)120 jerry_value_t JerryScriptNativeBigInt::CreateBigintByInt(int64_t value)
121 {
122 #if JERRY_API_MINOR_VERSION > 3 // jerryscript2.3: 3, jerryscript2.4: 4
123 bool isHaveSign = false;
124 isHaveSign = value < 0;
125 auto uintValue = value < 0 ? static_cast<uint64_t>(-value) : static_cast<uint64_t>(value);
126 return jerry_create_bigint(&uintValue, 1, isHaveSign);
127 #else
128 jerry_value_t temp = 0;
129 return temp;
130 #endif
131 }
132