-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathBigIntConstructor.cpp
More file actions
342 lines (278 loc) · 13.5 KB
/
Copy pathBigIntConstructor.cpp
File metadata and controls
342 lines (278 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (C) 2017 Caio Lima <ticaiolima@gmail.com>
* Copyright (C) 2017-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "BigIntConstructor.h"
#include "BigIntPrototype.h"
#include "JSBigInt.h"
#include "JSCInlines.h"
#include "ParseInt.h"
namespace JSC {
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorFuncAsUintN);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorFuncAsIntN);
} // namespace JSC
#include "BigIntConstructor.lut.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(BigIntConstructor);
const ClassInfo BigIntConstructor::s_info = { "Function"_s, &Base::s_info, &bigIntConstructorTable, nullptr, CREATE_METHOD_TABLE(BigIntConstructor) };
/* Source for BigIntConstructor.lut.h
@begin bigIntConstructorTable
asUintN bigIntConstructorFuncAsUintN DontEnum|Function 2
asIntN bigIntConstructorFuncAsIntN DontEnum|Function 2
@end
*/
static JSC_DECLARE_HOST_FUNCTION(callBigIntConstructor);
static JSC_DECLARE_HOST_FUNCTION(constructBigIntConstructor);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorAbs);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorCbrt);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorMax);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorMin);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorPow);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorSign);
static JSC_DECLARE_HOST_FUNCTION(bigIntConstructorSqrt);
BigIntConstructor::BigIntConstructor(VM& vm, Structure* structure)
: InternalFunction(vm, structure, callBigIntConstructor, constructBigIntConstructor)
{
}
void BigIntConstructor::finishCreation(VM& vm, BigIntPrototype* bigIntPrototype)
{
Base::finishCreation(vm, 1, "BigInt"_s, PropertyAdditionMode::WithoutStructureTransition);
ASSERT(inherits(info()));
putDirectWithoutTransition(vm, vm.propertyNames->prototype, bigIntPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
if (Options::useBigIntMathMethods()) {
JSGlobalObject* globalObject = this->globalObject();
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("abs"_s, bigIntConstructorAbs, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("cbrt"_s, bigIntConstructorCbrt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("max"_s, bigIntConstructorMax, static_cast<unsigned>(PropertyAttribute::DontEnum), 2, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("min"_s, bigIntConstructorMin, static_cast<unsigned>(PropertyAttribute::DontEnum), 2, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("pow"_s, bigIntConstructorPow, static_cast<unsigned>(PropertyAttribute::DontEnum), 2, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("sign"_s, bigIntConstructorSign, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ImplementationVisibility::Public);
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("sqrt"_s, bigIntConstructorSqrt, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, ImplementationVisibility::Public);
}
}
// ------------------------------ Functions ---------------------------
JSC_DEFINE_HOST_FUNCTION(callBigIntConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue value = callFrame->argument(0);
JSValue primitive = value.toPrimitive(globalObject, PreferNumber);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (primitive.isInt32()) {
#if USE(BIGINT32)
return JSValue::encode(jsBigInt32(primitive.asInt32()));
#else
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::createFrom(globalObject, primitive.asInt32())));
#endif
}
if (primitive.isDouble()) {
double number = primitive.asDouble();
if (!isInteger(number))
return throwVMError(globalObject, scope, createRangeError(globalObject, "Not an integer"_s));
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::makeHeapBigIntOrBigInt32(globalObject, primitive.asDouble())));
}
RELEASE_AND_RETURN(scope, JSValue::encode(primitive.toBigInt(globalObject)));
}
JSC_DEFINE_HOST_FUNCTION(constructBigIntConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
return throwVMError(globalObject, scope, createNotAConstructorError(globalObject, callFrame->jsCallee()));
}
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorFuncAsUintN, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
uint64_t numberOfBits = callFrame->argument(0).toIndex(globalObject, "number of bits"_s);
RETURN_IF_EXCEPTION(scope, { });
JSValue bigInt = callFrame->argument(1).toBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, { });
#if USE(BIGINT32)
if (bigInt.isBigInt32())
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::asUintN(globalObject, numberOfBits, bigInt.bigInt32AsInt32())));
#endif
ASSERT(bigInt.isHeapBigInt());
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::asUintN(globalObject, numberOfBits, bigInt.asHeapBigInt())));
}
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorFuncAsIntN, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
uint64_t numberOfBits = callFrame->argument(0).toIndex(globalObject, "number of bits"_s);
RETURN_IF_EXCEPTION(scope, { });
JSValue bigInt = callFrame->argument(1).toBigInt(globalObject);
RETURN_IF_EXCEPTION(scope, { });
#if USE(BIGINT32)
if (bigInt.isBigInt32())
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::asIntN(globalObject, numberOfBits, bigInt.bigInt32AsInt32())));
#endif
ASSERT(bigInt.isHeapBigInt());
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::asIntN(globalObject, numberOfBits, bigInt.asHeapBigInt())));
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.abs
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorAbs, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue bigIntValue = callFrame->argument(0);
if (!bigIntValue.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.abs requires the argument to be a BigInt"_s);
return { };
}
#if USE(BIGINT32)
if (bigIntValue.isBigInt32())
return JSValue::encode(jsBigInt32(std::abs(bigIntValue.bigInt32AsInt32())));
#endif
JSBigInt* bigInt = bigIntValue.asHeapBigInt();
if (bigInt->sign())
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::unaryMinus(globalObject, bigInt)));
return JSValue::encode(bigInt);
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.cbrt
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorCbrt, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue bigIntValue = callFrame->argument(0);
if (!bigIntValue.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.cbrt requires the argument to be a BigInt"_s);
return { };
}
#if USE(BIGINT32)
if (bigIntValue.isBigInt32())
return JSValue::encode(jsBigInt32(static_cast<int32_t>(std::cbrt(static_cast<double>(bigIntValue.bigInt32AsInt32())))));
#endif
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::cbrt(globalObject, bigIntValue.asHeapBigInt())));
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.max
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorMax, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue result = callFrame->argument(0);
if (!result.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.max requires every argument to be a BigInt"_s);
return { };
}
for (unsigned i = 1, count = callFrame->argumentCount(); i < count; ++i) {
JSValue value = callFrame->uncheckedArgument(i);
if (!value.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.max requires every argument to be a BigInt"_s);
return { };
}
if (JSBigInt::compare(value, result) == JSBigInt::ComparisonResult::GreaterThan)
result = value;
}
RELEASE_AND_RETURN(scope, JSValue::encode(result));
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.min
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorMin, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue result = callFrame->argument(0);
if (!result.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.min requires every argument to be a BigInt"_s);
return { };
}
for (unsigned i = 1, count = callFrame->argumentCount(); i < count; ++i) {
JSValue value = callFrame->uncheckedArgument(i);
if (!value.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.min requires every argument to be a BigInt"_s);
return { };
}
if (JSBigInt::compare(value, result) == JSBigInt::ComparisonResult::LessThan)
result = value;
}
RELEASE_AND_RETURN(scope, JSValue::encode(result));
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.pow
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorPow, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue base = callFrame->argument(0);
if (!base.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.pow requires the first argument to be a BigInt"_s);
return { };
}
JSValue exponent = callFrame->argument(1);
if (!exponent.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.pow requires the second argument to be a BigInt"_s);
return { };
}
RELEASE_AND_RETURN(scope, JSValue::encode(jsPow(globalObject, base, exponent)));
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.sign
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorSign, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue bigIntValue = callFrame->argument(0);
if (!bigIntValue.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.sign requires the argument to be a BigInt"_s);
return { };
}
#if USE(BIGINT32)
if (bigIntValue.isBigInt32()) {
int32_t value = bigIntValue.bigInt32AsInt32();
if (!value)
return JSValue::encode(bigIntValue);
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::makeHeapBigIntOrBigInt32(globalObject, static_cast<int64_t>(value < 0 ? -1 : 1))));
}
#endif
JSBigInt* bigInt = bigIntValue.asHeapBigInt();
if (bigInt->isZero())
return JSValue::encode(bigIntValue);
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::makeHeapBigIntOrBigInt32(globalObject, static_cast<int64_t>(bigInt->sign() ? -1 : 1))));
}
// https://tc39.es/proposal-bigint-math/#sec-bigint.sqrt
JSC_DEFINE_HOST_FUNCTION(bigIntConstructorSqrt, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue bigIntValue = callFrame->argument(0);
if (!bigIntValue.isBigInt()) {
throwTypeError(globalObject, scope, "BigInt.sqrt requires the argument to be a positive BigInt"_s);
return { };
}
#if USE(BIGINT32)
if (bigIntValue.isBigInt32()) {
int32_t value = bigIntValue.bigInt32AsInt32();
if (value < 0) {
throwRangeError(globalObject, scope, "BigInt.sqrt requires the argument to be a positive BigInt"_s);
return { };
}
return JSValue::encode(jsBigInt32(static_cast<int32_t>(std::sqrt(static_cast<double>(value)))));
}
#endif
JSBigInt* bigInt = bigIntValue.asHeapBigInt();
if (bigInt->sign()) {
throwRangeError(globalObject, scope, "BigInt.sqrt requires the argument to be a positive BigInt"_s);
return { };
}
RELEASE_AND_RETURN(scope, JSValue::encode(JSBigInt::sqrt(globalObject, bigInt)));
}
} // namespace JSC