diff options
author | Linus Groh <mail@linusgroh.de> | 2022-12-06 22:03:52 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-07 16:43:06 +0000 |
commit | 5db38d7ba1a8caa5138dd65cc06be0c0e5a568e4 (patch) | |
tree | 6968f34445aceedd484c1b71eef06e7914d5e6c6 /Userland | |
parent | 07f1aad3dd81dfbd047f34b32bc82a49e07f79e5 (diff) | |
download | serenity-5db38d7ba1a8caa5138dd65cc06be0c0e5a568e4.zip |
LibJS: Replace standalone js_bigint() with BigInt::create()
Three standalone Cell creation functions remain in the JS namespace:
- js_bigint()
- js_string()
- js_symbol()
All of them are leftovers from early iterations when LibJS still took
inspiration from JSC, which itself has jsString(). Nowadays, we pretty
much exclusively use static create() functions to construct types
allocated on the JS heap, and there's no reason to not do the same for
these.
Also change the return type from BigInt* to NonnullGCPtr<BigInt> while
we're here.
This is patch 1/3, replacement of js_string() and js_symbol() follow.
Diffstat (limited to 'Userland')
18 files changed, 81 insertions, 84 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 46c75331af..c0f8364304 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -2817,7 +2817,7 @@ Completion UpdateExpression::execute(Interpreter& interpreter) const else { // a. Assert: Type(oldValue) is BigInt. // b. Let newValue be BigInt::add(oldValue, 1ℤ). - new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })); + new_value = BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })); } break; case UpdateOp::Decrement: @@ -2830,7 +2830,7 @@ Completion UpdateExpression::execute(Interpreter& interpreter) const else { // a. Assert: Type(oldValue) is BigInt. // b. Let newValue be BigInt::subtract(oldValue, 1ℤ). - new_value = js_bigint(interpreter.heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })); + new_value = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })); } break; default: @@ -3483,19 +3483,20 @@ Completion NumericLiteral::execute(Interpreter& interpreter) const Completion BigIntLiteral::execute(Interpreter& interpreter) const { InterpreterNodeScope node_scope { interpreter, *this }; + auto& vm = interpreter.vm(); // 1. Return the NumericValue of NumericLiteral as defined in 12.8.3. Crypto::SignedBigInteger integer; if (m_value[0] == '0' && m_value.length() >= 3) { if (m_value[1] == 'x' || m_value[1] == 'X') { - return Value { js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(16, m_value.substring(2, m_value.length() - 3))) }; + return Value { BigInt::create(vm, Crypto::SignedBigInteger::from_base(16, m_value.substring(2, m_value.length() - 3))) }; } else if (m_value[1] == 'o' || m_value[1] == 'O') { - return Value { js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(8, m_value.substring(2, m_value.length() - 3))) }; + return Value { BigInt::create(vm, Crypto::SignedBigInteger::from_base(8, m_value.substring(2, m_value.length() - 3))) }; } else if (m_value[1] == 'b' || m_value[1] == 'B') { - return Value { js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(2, m_value.substring(2, m_value.length() - 3))) }; + return Value { BigInt::create(vm, Crypto::SignedBigInteger::from_base(2, m_value.substring(2, m_value.length() - 3))) }; } } - return Value { js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1))) }; + return Value { BigInt::create(vm, Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1))) }; } // 13.2.3.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-literals-runtime-semantics-evaluation diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 5866a4dcc2..a500b297eb 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -169,7 +169,8 @@ JS_ENUMERATE_COMMON_UNARY_OPS(JS_DEFINE_COMMON_UNARY_OP) ThrowCompletionOr<void> NewBigInt::execute_impl(Bytecode::Interpreter& interpreter) const { - interpreter.accumulator() = js_bigint(interpreter.vm().heap(), m_bigint); + auto& vm = interpreter.vm(); + interpreter.accumulator() = BigInt::create(vm, m_bigint); return {}; } @@ -706,7 +707,7 @@ ThrowCompletionOr<void> Increment::execute_impl(Bytecode::Interpreter& interpret if (old_value.is_number()) interpreter.accumulator() = Value(old_value.as_double() + 1); else - interpreter.accumulator() = js_bigint(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })); + interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 })); return {}; } @@ -718,7 +719,7 @@ ThrowCompletionOr<void> Decrement::execute_impl(Bytecode::Interpreter& interpret if (old_value.is_number()) interpreter.accumulator() = Value(old_value.as_double() - 1); else - interpreter.accumulator() = js_bigint(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })); + interpreter.accumulator() = BigInt::create(vm, old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 })); return {}; } diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h index 34f4655654..09fc873947 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h @@ -111,10 +111,10 @@ static Value raw_bytes_to_numeric(VM& vm, ByteBuffer raw_value, bool is_little_e if constexpr (sizeof(UnderlyingBufferDataType) == 8) { if constexpr (IsSigned<UnderlyingBufferDataType>) { static_assert(IsSame<UnderlyingBufferDataType, i64>); - return js_bigint(vm, Crypto::SignedBigInteger { int_value }); + return BigInt::create(vm, Crypto::SignedBigInteger { int_value }); } else { static_assert(IsOneOf<UnderlyingBufferDataType, u64, double>); - return js_bigint(vm, Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { int_value } }); + return BigInt::create(vm, Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { int_value } }); } } else { return Value(int_value); diff --git a/Userland/Libraries/LibJS/Runtime/BigInt.cpp b/Userland/Libraries/LibJS/Runtime/BigInt.cpp index acddf5e9e9..86691727e2 100644 --- a/Userland/Libraries/LibJS/Runtime/BigInt.cpp +++ b/Userland/Libraries/LibJS/Runtime/BigInt.cpp @@ -11,20 +11,15 @@ namespace JS { -BigInt::BigInt(Crypto::SignedBigInteger big_integer) - : m_big_integer(move(big_integer)) +NonnullGCPtr<BigInt> BigInt::create(VM& vm, Crypto::SignedBigInteger big_integer) { - VERIFY(!m_big_integer.is_invalid()); + return *vm.heap().allocate_without_realm<BigInt>(move(big_integer)); } -BigInt* js_bigint(Heap& heap, Crypto::SignedBigInteger big_integer) -{ - return heap.allocate_without_realm<BigInt>(move(big_integer)); -} - -BigInt* js_bigint(VM& vm, Crypto::SignedBigInteger big_integer) +BigInt::BigInt(Crypto::SignedBigInteger big_integer) + : m_big_integer(move(big_integer)) { - return js_bigint(vm.heap(), move(big_integer)); + VERIFY(!m_big_integer.is_invalid()); } // 21.2.1.1.1 NumberToBigInt ( number ), https://tc39.es/ecma262/#sec-numbertobigint @@ -37,7 +32,7 @@ ThrowCompletionOr<BigInt*> number_to_bigint(VM& vm, Value number) return vm.throw_completion<RangeError>(ErrorType::BigIntFromNonIntegral); // 2. Return the BigInt value that represents ℝ(number). - return js_bigint(vm, Crypto::SignedBigInteger { number.as_double() }); + return BigInt::create(vm, Crypto::SignedBigInteger { number.as_double() }).ptr(); } } diff --git a/Userland/Libraries/LibJS/Runtime/BigInt.h b/Userland/Libraries/LibJS/Runtime/BigInt.h index 8c58a58df6..8a853933fa 100644 --- a/Userland/Libraries/LibJS/Runtime/BigInt.h +++ b/Userland/Libraries/LibJS/Runtime/BigInt.h @@ -16,6 +16,8 @@ class BigInt final : public Cell { JS_CELL(BigInt, Cell); public: + [[nodiscard]] static NonnullGCPtr<BigInt> create(VM&, Crypto::SignedBigInteger); + virtual ~BigInt() override = default; Crypto::SignedBigInteger const& big_integer() const { return m_big_integer; } @@ -27,8 +29,6 @@ private: Crypto::SignedBigInteger m_big_integer; }; -BigInt* js_bigint(Heap&, Crypto::SignedBigInteger); -BigInt* js_bigint(VM&, Crypto::SignedBigInteger); ThrowCompletionOr<BigInt*> number_to_bigint(VM&, Value); } diff --git a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp index 3142f41fd7..c8bf3c2ead 100644 --- a/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/BigIntConstructor.cpp @@ -80,11 +80,11 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n) // NOTE: Some of the below conditionals are non-standard, but are to protect SignedBigInteger from // allocating an absurd amount of memory if `bits - 1` overflows to NumericLimits<size_t>::max. if ((bits == 0) && (mod >= BIGINT_ONE)) - return js_bigint(vm, mod.minus(bits_shift_left)); + return BigInt::create(vm, mod.minus(bits_shift_left)); if ((bits > 0) && (mod >= BIGINT_ONE.shift_left(bits - 1))) - return js_bigint(vm, mod.minus(bits_shift_left)); + return BigInt::create(vm, mod.minus(bits_shift_left)); - return js_bigint(vm, mod); + return BigInt::create(vm, mod); } // 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn @@ -99,7 +99,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n) // 3. Return the BigInt value that represents ℝ(bigint) modulo 2bits. // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to // drop the most significant bits. - return js_bigint(vm, modulo(bigint->big_integer(), BIGINT_ONE.shift_left(bits))); + return BigInt::create(vm, modulo(bigint->big_integer(), BIGINT_ONE.shift_left(bits))); } } diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 627454334e..5d5decc61d 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -1194,7 +1194,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_temporal_instant) // 2. Let ns be ? NumberToBigInt(t) × ℤ(10^6). auto* ns = TRY(number_to_bigint(vm, Value(t))); - ns = js_bigint(vm, ns->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 })); + ns = BigInt::create(vm, ns->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 })); // 3. Return ! CreateTemporalInstant(ns). return MUST(Temporal::create_temporal_instant(vm, *ns)); diff --git a/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.cpp b/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.cpp index 8cfc51906c..ca4705baa8 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.cpp @@ -308,7 +308,7 @@ Value MathematicalValue::to_value(VM& vm) const return Value(value); }, [&](Crypto::SignedBigInteger const& value) { - return Value(js_bigint(vm, value)); + return Value(BigInt::create(vm, value)); }, [](auto symbol) { switch (symbol) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp index a67171b5a9..ad9be9e7ab 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp @@ -1642,7 +1642,7 @@ ThrowCompletionOr<DurationRecord> adjust_rounded_duration_days(VM& vm, double ye } // 10. Set timeRemainderNs to ! RoundTemporalInstant(ℤ(timeRemainderNs - dayLengthNs), increment, unit, roundingMode). - time_remainder_ns = round_temporal_instant(vm, *js_bigint(vm, time_remainder_ns.minus(day_length_ns)), increment, unit, rounding_mode)->big_integer(); + time_remainder_ns = round_temporal_instant(vm, BigInt::create(vm, time_remainder_ns.minus(day_length_ns)), increment, unit, rounding_mode)->big_integer(); // 11. Let adjustedDateDuration be ? AddDuration(years, months, weeks, days, 0, 0, 0, 0, 0, 0, 0, 0, 0, direction, 0, 0, 0, 0, 0, 0, relativeTo). auto adjusted_date_duration = TRY(add_duration(vm, years, months, weeks, days, 0, 0, 0, 0, 0, 0, 0, 0, 0, direction, 0, 0, 0, 0, 0, 0, &relative_to)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp index 4215d9ca6a..534a8c84d1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp @@ -143,7 +143,7 @@ ThrowCompletionOr<BigInt*> parse_temporal_instant(VM& vm, DeprecatedString const } // 9. Return result. - return js_bigint(vm, move(result_ns)); + return BigInt::create(vm, move(result_ns)).ptr(); } // 8.5.5 CompareEpochNanoseconds ( epochNanosecondsOne, epochNanosecondsTwo ), https://tc39.es/proposal-temporal/#sec-temporal-compareepochnanoseconds @@ -167,7 +167,7 @@ ThrowCompletionOr<BigInt*> add_instant(VM& vm, BigInt const& epoch_nanoseconds, VERIFY(hours == trunc(hours) && minutes == trunc(minutes) && seconds == trunc(seconds) && milliseconds == trunc(milliseconds) && microseconds == trunc(microseconds) && nanoseconds == trunc(nanoseconds)); // 1. Let result be epochNanoseconds + ℤ(nanoseconds) + ℤ(microseconds) × 1000ℤ + ℤ(milliseconds) × 10^6ℤ + ℤ(seconds) × 10^9ℤ + ℤ(minutes) × 60ℤ × 10^9ℤ + ℤ(hours) × 3600ℤ × 10^9ℤ. - auto* result = js_bigint(vm, + auto result = BigInt::create(vm, epoch_nanoseconds.big_integer() .plus(Crypto::SignedBigInteger { nanoseconds }) .plus(Crypto::SignedBigInteger { microseconds }.multiplied_by(Crypto::SignedBigInteger { 1'000 })) @@ -181,7 +181,7 @@ ThrowCompletionOr<BigInt*> add_instant(VM& vm, BigInt const& epoch_nanoseconds, return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 3. Return result. - return result; + return result.ptr(); } // 8.5.7 DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-differenceinstant @@ -191,7 +191,7 @@ BigInt* difference_instant(VM& vm, BigInt const& nanoseconds1, BigInt const& nan // 2. Assert: Type(ns2) is BigInt. // 3. Return ! RoundTemporalInstant(ns2 - ns1, roundingIncrement, smallestUnit, roundingMode). - return round_temporal_instant(vm, *js_bigint(vm, nanoseconds2.big_integer().minus(nanoseconds1.big_integer())), rounding_increment, smallest_unit, rounding_mode); + return round_temporal_instant(vm, BigInt::create(vm, nanoseconds2.big_integer().minus(nanoseconds1.big_integer())), rounding_increment, smallest_unit, rounding_mode); } // 8.5.8 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant @@ -235,7 +235,7 @@ BigInt* round_temporal_instant(VM& vm, BigInt const& nanoseconds, u64 increment, } // 8. Return RoundNumberToIncrementAsIfPositive(ℝ(ns), incrementNs, roundingMode). - return js_bigint(vm, round_number_to_increment_as_if_positive(nanoseconds.big_integer(), increment_nanoseconds, rounding_mode)); + return BigInt::create(vm, round_number_to_increment_as_if_positive(nanoseconds.big_integer(), increment_nanoseconds, rounding_mode)); } // 8.5.9 TemporalInstantToString ( instant, timeZone, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporalinstanttostring diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp index 9f2b34e617..ab97a62de1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp @@ -72,7 +72,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from) // 1. If Type(item) is Object and item has an [[InitializedTemporalInstant]] internal slot, then if (item.is_object() && is<Instant>(item.as_object())) { // a. Return ! CreateTemporalInstant(item.[[Nanoseconds]]). - return MUST(create_temporal_instant(vm, *js_bigint(vm, static_cast<Instant&>(item.as_object()).nanoseconds().big_integer()))); + return MUST(create_temporal_instant(vm, BigInt::create(vm, static_cast<Instant&>(item.as_object()).nanoseconds().big_integer()))); } // 2. Return ? ToTemporalInstant(item). @@ -89,14 +89,14 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds) auto* epoch_seconds = TRY(number_to_bigint(vm, epoch_seconds_value)); // 3. Let epochNanoseconds be epochSeconds × 10^9ℤ. - auto* epoch_nanoseconds = js_bigint(vm, epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 })); + auto epoch_nanoseconds = BigInt::create(vm, epoch_seconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000'000 })); // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception. - if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) + if (!is_valid_epoch_nanoseconds(epoch_nanoseconds)) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 5. Return ! CreateTemporalInstant(epochNanoseconds). - return MUST(create_temporal_instant(vm, *epoch_nanoseconds)); + return MUST(create_temporal_instant(vm, epoch_nanoseconds)); } // 8.2.4 Temporal.Instant.fromEpochMilliseconds ( epochMilliseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmilliseconds @@ -109,14 +109,14 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds) auto* epoch_milliseconds = TRY(number_to_bigint(vm, epoch_milliseconds_value)); // 3. Let epochNanoseconds be epochMilliseconds × 10^6ℤ. - auto* epoch_nanoseconds = js_bigint(vm, epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 })); + auto epoch_nanoseconds = BigInt::create(vm, epoch_milliseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 })); // 4. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception. - if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) + if (!is_valid_epoch_nanoseconds(epoch_nanoseconds)) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 5. Return ! CreateTemporalInstant(epochNanoseconds). - return MUST(create_temporal_instant(vm, *epoch_nanoseconds)); + return MUST(create_temporal_instant(vm, epoch_nanoseconds)); } // 8.2.5 Temporal.Instant.fromEpochMicroseconds ( epochMicroseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochmicroseconds @@ -126,14 +126,14 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_microseconds) auto* epoch_microseconds = TRY(vm.argument(0).to_bigint(vm)); // 2. Let epochNanoseconds be epochMicroseconds × 1000ℤ. - auto* epoch_nanoseconds = js_bigint(vm, epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 })); + auto epoch_nanoseconds = BigInt::create(vm, epoch_microseconds->big_integer().multiplied_by(Crypto::UnsignedBigInteger { 1'000 })); // 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception. - if (!is_valid_epoch_nanoseconds(*epoch_nanoseconds)) + if (!is_valid_epoch_nanoseconds(epoch_nanoseconds)) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 4. Return ! CreateTemporalInstant(epochNanoseconds). - return MUST(create_temporal_instant(vm, *epoch_nanoseconds)); + return MUST(create_temporal_instant(vm, epoch_nanoseconds)); } // 8.2.6 Temporal.Instant.fromEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochnanoseconds diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp index 5d530b3aaa..37d40bd369 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantPrototype.cpp @@ -101,7 +101,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_microseconds_getter) auto [us, _] = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000 }); // 5. Return ℤ(µs). - return js_bigint(vm, move(us)); + return BigInt::create(vm, move(us)); } // 8.3.6 get Temporal.Instant.prototype.epochNanoseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochnanoseconds diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp index 786d15c895..2b27e1be6e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Now.cpp @@ -170,7 +170,7 @@ BigInt* system_utc_epoch_nanoseconds(VM& vm) // if an overflow occurs during seconds -> nanoseconds conversion. // 3. Return ℤ(ns). - return js_bigint(vm, move(ns)); + return BigInt::create(vm, move(ns)); } // 2.3.3 SystemInstant ( ), https://tc39.es/proposal-temporal/#sec-temporal-systeminstant diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp index 4b57afa5c0..cfc480264b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp @@ -99,7 +99,7 @@ ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM& vm, DeprecatedString ISODateTime get_iso_parts_from_epoch(VM& vm, Crypto::SignedBigInteger const& epoch_nanoseconds) { // 1. Assert: ! IsValidEpochNanoseconds(ℤ(epochNanoseconds)) is true. - VERIFY(is_valid_epoch_nanoseconds(*js_bigint(vm, epoch_nanoseconds))); + VERIFY(is_valid_epoch_nanoseconds(BigInt::create(vm, epoch_nanoseconds))); // 2. Let remainderNs be epochNanoseconds modulo 10^6. auto remainder_ns_bigint = modulo(epoch_nanoseconds, Crypto::UnsignedBigInteger { 1'000'000 }); @@ -480,24 +480,24 @@ ThrowCompletionOr<Instant*> disambiguate_possible_instants(VM& vm, MarkedVector< auto epoch_nanoseconds = get_utc_epoch_nanoseconds(date_time.iso_year(), date_time.iso_month(), date_time.iso_day(), date_time.iso_hour(), date_time.iso_minute(), date_time.iso_second(), date_time.iso_millisecond(), date_time.iso_microsecond(), date_time.iso_nanosecond()); // 8. Let dayBeforeNs be epochNanoseconds - ℤ(nsPerDay). - auto* day_before_ns = js_bigint(vm, epoch_nanoseconds.minus(ns_per_day_bigint)); + auto day_before_ns = BigInt::create(vm, epoch_nanoseconds.minus(ns_per_day_bigint)); // 9. If ! IsValidEpochNanoseconds(dayBeforeNs) is false, throw a RangeError exception. - if (!is_valid_epoch_nanoseconds(*day_before_ns)) + if (!is_valid_epoch_nanoseconds(day_before_ns)) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 10. Let dayBefore be ! CreateTemporalInstant(dayBeforeNs). - auto* day_before = MUST(create_temporal_instant(vm, *day_before_ns)); + auto* day_before = MUST(create_temporal_instant(vm, day_before_ns)); // 11. Let dayAfterNs be epochNanoseconds + ℤ(nsPerDay). - auto* day_after_ns = js_bigint(vm, epoch_nanoseconds.plus(ns_per_day_bigint)); + auto day_after_ns = BigInt::create(vm, epoch_nanoseconds.plus(ns_per_day_bigint)); // 12. If ! IsValidEpochNanoseconds(dayAfterNs) is false, throw a RangeError exception. - if (!is_valid_epoch_nanoseconds(*day_after_ns)) + if (!is_valid_epoch_nanoseconds(day_after_ns)) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 13. Let dayAfter be ! CreateTemporalInstant(dayAfterNs). - auto* day_after = MUST(create_temporal_instant(vm, *day_after_ns)); + auto* day_after = MUST(create_temporal_instant(vm, day_after_ns)); // 14. Let offsetBefore be ? GetOffsetNanosecondsFor(timeZone, dayBefore). auto offset_before = TRY(get_offset_nanoseconds_for(vm, time_zone, *day_before)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp index 2a3c1fce57..289d811667 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZonePrototype.cpp @@ -164,8 +164,8 @@ JS_DEFINE_NATIVE_FUNCTION(TimeZonePrototype::get_possible_instants_for) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // b. Let instant be ! CreateTemporalInstant(epochNanoseconds). - auto* epoch_nanoseconds_bigint = js_bigint(vm, move(epoch_nanoseconds)); - auto* instant = MUST(create_temporal_instant(vm, *epoch_nanoseconds_bigint)); + auto epoch_nanoseconds_bigint = BigInt::create(vm, move(epoch_nanoseconds)); + auto* instant = MUST(create_temporal_instant(vm, epoch_nanoseconds_bigint)); // c. Append instant to possibleInstants. possible_instants.append(instant); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp index d50f8e4465..da2514b655 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp @@ -69,7 +69,7 @@ ThrowCompletionOr<BigInt const*> interpret_iso_date_time_offset(VM& vm, i32 year return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // d. Return epochNanoseconds. - return js_bigint(vm, move(epoch_nanoseconds)); + return BigInt::create(vm, move(epoch_nanoseconds)).ptr(); } // 5. Assert: offsetBehaviour is option. @@ -471,7 +471,7 @@ ThrowCompletionOr<NanosecondsToDaysResult> nanoseconds_to_days(VM& vm, Crypto::S auto& start_ns = relative_to.nanoseconds().big_integer(); // 6. Let startInstant be ! CreateTemporalInstant(ℤ(startNs)). - auto* start_instant = MUST(create_temporal_instant(vm, *js_bigint(vm, start_ns))); + auto* start_instant = MUST(create_temporal_instant(vm, BigInt::create(vm, start_ns))); // 7. Let startDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(relativeTo.[[TimeZone]], startInstant, relativeTo.[[Calendar]]). auto* start_date_time = TRY(builtin_time_zone_get_plain_date_time_for(vm, &relative_to.time_zone(), *start_instant, relative_to.calendar())); @@ -479,14 +479,14 @@ ThrowCompletionOr<NanosecondsToDaysResult> nanoseconds_to_days(VM& vm, Crypto::S // 8. Let endNs be startNs + nanoseconds. auto end_ns = start_ns.plus(nanoseconds); - auto* end_ns_bigint = js_bigint(vm, end_ns); + auto end_ns_bigint = BigInt::create(vm, end_ns); // 9. If ! IsValidEpochNanoseconds(ℤ(endNs)) is false, throw a RangeError exception. - if (!is_valid_epoch_nanoseconds(*end_ns_bigint)) + if (!is_valid_epoch_nanoseconds(end_ns_bigint)) return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds); // 10. Let endInstant be ! CreateTemporalInstant(ℤ(endNs)). - auto* end_instant = MUST(create_temporal_instant(vm, *end_ns_bigint)); + auto* end_instant = MUST(create_temporal_instant(vm, end_ns_bigint)); // 11. Let endDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(relativeTo.[[TimeZone]], endInstant, relativeTo.[[Calendar]]). auto* end_date_time = TRY(builtin_time_zone_get_plain_date_time_for(vm, &relative_to.time_zone(), *end_instant, relative_to.calendar())); @@ -498,7 +498,7 @@ ThrowCompletionOr<NanosecondsToDaysResult> nanoseconds_to_days(VM& vm, Crypto::S auto days = date_difference.days; // 14. Let intermediateNs be ℝ(? AddZonedDateTime(ℤ(startNs), relativeTo.[[TimeZone]], relativeTo.[[Calendar]], 0, 0, 0, days, 0, 0, 0, 0, 0, 0)). - auto intermediate_ns = TRY(add_zoned_date_time(vm, *js_bigint(vm, start_ns), &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, days, 0, 0, 0, 0, 0, 0))->big_integer(); + auto intermediate_ns = TRY(add_zoned_date_time(vm, BigInt::create(vm, start_ns), &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, days, 0, 0, 0, 0, 0, 0))->big_integer(); // 15. If sign is 1, then if (sign == 1) { @@ -508,7 +508,7 @@ ThrowCompletionOr<NanosecondsToDaysResult> nanoseconds_to_days(VM& vm, Crypto::S days--; // ii. Set intermediateNs to ℝ(? AddZonedDateTime(ℤ(startNs), relativeTo.[[TimeZone]], relativeTo.[[Calendar]], 0, 0, 0, days, 0, 0, 0, 0, 0, 0)). - intermediate_ns = TRY(add_zoned_date_time(vm, *js_bigint(vm, start_ns), &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, days, 0, 0, 0, 0, 0, 0))->big_integer(); + intermediate_ns = TRY(add_zoned_date_time(vm, BigInt::create(vm, start_ns), &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, days, 0, 0, 0, 0, 0, 0))->big_integer(); } } @@ -519,7 +519,7 @@ ThrowCompletionOr<NanosecondsToDaysResult> nanoseconds_to_days(VM& vm, Crypto::S // 18. Repeat, while done is false, while (true) { // a. Let oneDayFartherNs be ℝ(? AddZonedDateTime(ℤ(intermediateNs), relativeTo.[[TimeZone]], relativeTo.[[Calendar]], 0, 0, 0, sign, 0, 0, 0, 0, 0, 0)). - auto one_day_farther_ns = TRY(add_zoned_date_time(vm, *js_bigint(vm, intermediate_ns), &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, sign, 0, 0, 0, 0, 0, 0))->big_integer(); + auto one_day_farther_ns = TRY(add_zoned_date_time(vm, BigInt::create(vm, intermediate_ns), &relative_to.time_zone(), relative_to.calendar(), 0, 0, 0, sign, 0, 0, 0, 0, 0, 0))->big_integer(); // b. Set dayLengthNs to oneDayFartherNs - intermediateNs. day_length_ns = one_day_farther_ns.minus(intermediate_ns); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 5b4fa31ec4..71a6f65127 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp @@ -391,7 +391,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::epoch_microseconds_getter) auto us = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000 }).quotient; // 5. Return ℤ(µs). - return js_bigint(vm, move(us)); + return BigInt::create(vm, move(us)); } // 6.3.18 get Temporal.ZonedDateTime.prototype.epochNanoseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.epochnanoseconds diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index cd00c14e06..6513492394 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -608,7 +608,7 @@ ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const return vm.throw_completion<TypeError>(ErrorType::Convert, "null", "BigInt"); case BOOLEAN_TAG: { auto value = primitive.as_bool() ? 1 : 0; - return js_bigint(vm, Crypto::SignedBigInteger { value }); + return BigInt::create(vm, Crypto::SignedBigInteger { value }).ptr(); } case BIGINT_TAG: return &primitive.as_bigint(); @@ -695,7 +695,7 @@ static Optional<BigInt*> string_to_bigint(VM& vm, StringView string) bigint.negate(); // 6. Return ℤ(mv). - return js_bigint(vm, move(bigint)); + return BigInt::create(vm, move(bigint)); } // 7.1.15 ToBigInt64 ( argument ), https://tc39.es/ecma262/#sec-tobigint64 @@ -1012,7 +1012,7 @@ ThrowCompletionOr<Value> bitwise_and(VM& vm, Value lhs, Value rhs) return Value(TRY(lhs_numeric.to_i32(vm)) & TRY(rhs_numeric.to_i32(vm))); } if (both_bigint(lhs_numeric, rhs_numeric)) - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer())); return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND"); } @@ -1031,7 +1031,7 @@ ThrowCompletionOr<Value> bitwise_or(VM& vm, Value lhs, Value rhs) return Value(TRY(lhs_numeric.to_i32(vm)) | TRY(rhs_numeric.to_i32(vm))); } if (both_bigint(lhs_numeric, rhs_numeric)) - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer())); return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise OR"); } @@ -1050,7 +1050,7 @@ ThrowCompletionOr<Value> bitwise_xor(VM& vm, Value lhs, Value rhs) return Value(TRY(lhs_numeric.to_i32(vm)) ^ TRY(rhs_numeric.to_i32(vm))); } if (both_bigint(lhs_numeric, rhs_numeric)) - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer())); return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR"); } @@ -1060,7 +1060,7 @@ ThrowCompletionOr<Value> bitwise_not(VM& vm, Value lhs) auto lhs_numeric = TRY(lhs.to_numeric(vm)); if (lhs_numeric.is_number()) return Value(~TRY(lhs_numeric.to_i32(vm))); - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().bitwise_not())); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_not()); } // 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator @@ -1079,10 +1079,10 @@ ThrowCompletionOr<Value> unary_minus(VM& vm, Value lhs) return Value(-lhs_numeric.as_double()); } if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) - return Value(js_bigint(vm, BIGINT_ZERO)); + return BigInt::create(vm, BIGINT_ZERO); auto big_integer_negated = lhs_numeric.as_bigint().big_integer(); big_integer_negated.negate(); - return Value(js_bigint(vm, big_integer_negated)); + return BigInt::create(vm, big_integer_negated); } // 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator @@ -1113,12 +1113,12 @@ ThrowCompletionOr<Value> left_shift(VM& vm, Value lhs, Value rhs) // For positive initial values and no remainder just return quotient if (division_result.remainder.is_zero() || !big_integer.is_negative()) - return js_bigint(vm, division_result.quotient); + return BigInt::create(vm, division_result.quotient); // For negative round "down" to the next negative number - return js_bigint(vm, division_result.quotient.minus(Crypto::SignedBigInteger { 1 })); + return BigInt::create(vm, division_result.quotient.minus(Crypto::SignedBigInteger { 1 })); } // 2. Return the BigInt value that represents ℝ(x) × 2^y. - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor))); + return Value(BigInt::create(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor))); } return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "left-shift"); } @@ -1140,7 +1140,7 @@ ThrowCompletionOr<Value> right_shift(VM& vm, Value lhs, Value rhs) if (both_bigint(lhs_numeric, rhs_numeric)) { auto rhs_negated = rhs_numeric.as_bigint().big_integer(); rhs_negated.negate(); - return left_shift(vm, lhs, js_bigint(vm, rhs_negated)); + return left_shift(vm, lhs, BigInt::create(vm, rhs_negated)); } return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "right-shift"); } @@ -1190,7 +1190,7 @@ ThrowCompletionOr<Value> add(VM& vm, Value lhs, Value rhs) if (both_number(lhs_numeric, rhs_numeric)) return Value(lhs_numeric.as_double() + rhs_numeric.as_double()); if (both_bigint(lhs_numeric, rhs_numeric)) - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer())); return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "addition"); } @@ -1206,7 +1206,7 @@ ThrowCompletionOr<Value> sub(VM& vm, Value lhs, Value rhs) return Value(interm); } if (both_bigint(lhs_numeric, rhs_numeric)) - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer())); return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "subtraction"); } @@ -1218,7 +1218,7 @@ ThrowCompletionOr<Value> mul(VM& vm, Value lhs, Value rhs) if (both_number(lhs_numeric, rhs_numeric)) return Value(lhs_numeric.as_double() * rhs_numeric.as_double()); if (both_bigint(lhs_numeric, rhs_numeric)) - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer())); return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "multiplication"); } @@ -1232,7 +1232,7 @@ ThrowCompletionOr<Value> div(VM& vm, Value lhs, Value rhs) if (both_bigint(lhs_numeric, rhs_numeric)) { if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) return vm.throw_completion<RangeError>(ErrorType::DivisionByZero); - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient)); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient); } return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "division"); } @@ -1253,7 +1253,7 @@ ThrowCompletionOr<Value> mod(VM& vm, Value lhs, Value rhs) if (both_bigint(lhs_numeric, rhs_numeric)) { if (rhs_numeric.as_bigint().big_integer() == BIGINT_ZERO) return vm.throw_completion<RangeError>(ErrorType::DivisionByZero); - return Value(js_bigint(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder)); + return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder); } return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "modulo"); } @@ -1320,7 +1320,7 @@ ThrowCompletionOr<Value> exp(VM& vm, Value lhs, Value rhs) if (both_bigint(lhs_numeric, rhs_numeric)) { if (rhs_numeric.as_bigint().big_integer().is_negative()) return vm.throw_completion<RangeError>(ErrorType::NegativeExponent); - return Value(js_bigint(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()))); + return BigInt::create(vm, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer())); } return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "exponentiation"); } |