2020-03-07 19:42:11 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-05-02 20:54:39 +02:00
|
|
|
|
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
2022-08-23 20:24:59 +02:00
|
|
|
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-07 19:42:11 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2021-02-17 11:18:51 +01:00
|
|
|
|
#include <AK/AllOf.h>
|
2022-02-17 11:52:45 -08:00
|
|
|
|
#include <AK/Assertions.h>
|
2022-01-31 10:23:51 -05:00
|
|
|
|
#include <AK/CharacterTypes.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
|
#include <AK/DeprecatedString.h>
|
2022-10-12 02:23:50 +02:00
|
|
|
|
#include <AK/FloatingPointStringConversions.h>
|
2020-05-15 13:39:24 +02:00
|
|
|
|
#include <AK/StringBuilder.h>
|
2022-10-26 23:16:59 -04:00
|
|
|
|
#include <AK/StringFloatingPointConversions.h>
|
2020-05-28 17:19:59 +02:00
|
|
|
|
#include <AK/Utf8View.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
|
|
|
|
#include <LibCrypto/NumberTheory/ModularFunctions.h>
|
2022-01-23 02:12:26 -07:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-05-21 11:14:23 -07:00
|
|
|
|
#include <LibJS/Runtime/Accessor.h>
|
2020-03-29 15:20:09 +01:00
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-06-06 01:14:10 +01:00
|
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
|
|
|
|
#include <LibJS/Runtime/BigIntObject.h>
|
2020-04-06 22:51:16 -05:00
|
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
2020-07-11 15:27:28 -07:00
|
|
|
|
#include <LibJS/Runtime/BoundFunction.h>
|
2021-10-12 17:49:01 +01:00
|
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-03-28 22:48:35 +01:00
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
2021-06-27 21:48:34 +02:00
|
|
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
2020-09-27 17:24:14 +02:00
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
|
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
2020-04-04 23:13:13 +02:00
|
|
|
|
#include <LibJS/Runtime/NumberObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
2021-06-08 21:53:36 +01:00
|
|
|
|
#include <LibJS/Runtime/ProxyObject.h>
|
2021-01-01 17:46:39 +01:00
|
|
|
|
#include <LibJS/Runtime/RegExpObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2022-02-19 22:47:05 +02:00
|
|
|
|
#include <LibJS/Runtime/StringPrototype.h>
|
2020-04-29 23:25:21 -07:00
|
|
|
|
#include <LibJS/Runtime/SymbolObject.h>
|
2021-08-09 16:45:43 +02:00
|
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-04-05 13:40:00 +01:00
|
|
|
|
#include <math.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
|
static inline bool same_type_for_equality(Value const& lhs, Value const& rhs)
|
2021-03-21 18:03:00 +01:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
// If the top two bytes are identical then either:
|
|
|
|
|
// both are NaN boxed Values with the same type
|
|
|
|
|
// or they are doubles which happen to have the same top bytes.
|
|
|
|
|
if ((lhs.encoded() & TAG_EXTRACTION) == (rhs.encoded() & TAG_EXTRACTION))
|
2021-03-21 18:03:00 +01:00
|
|
|
|
return true;
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
2021-03-21 18:03:00 +01:00
|
|
|
|
if (lhs.is_number() && rhs.is_number())
|
|
|
|
|
return true;
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
|
|
|
|
// One of the Values is not a number and they do not have the same tag
|
2021-03-21 18:03:00 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
|
ALWAYS_INLINE bool both_number(Value const& lhs, Value const& rhs)
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
|
|
|
|
return lhs.is_number() && rhs.is_number();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
|
ALWAYS_INLINE bool both_bigint(Value const& lhs, Value const& rhs)
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
|
|
|
|
return lhs.is_bigint() && rhs.is_bigint();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 6.1.6.1.20 Number::toString ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-tostring
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// Implementation for radix = 10
|
2022-12-04 18:02:33 +00:00
|
|
|
|
DeprecatedString number_to_string(double d, NumberToStringMode mode)
|
2020-12-26 11:30:14 +01:00
|
|
|
|
{
|
2022-10-26 23:16:59 -04:00
|
|
|
|
auto convert_to_decimal_digits_array = [](auto x, auto& digits, auto& length) {
|
|
|
|
|
for (; x; x /= 10)
|
|
|
|
|
digits[length++] = x % 10 | '0';
|
|
|
|
|
for (i32 i = 0; 2 * i + 1 < length; ++i)
|
|
|
|
|
swap(digits[i], digits[length - i - 1]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 1. If x is NaN, return "NaN".
|
2020-12-26 11:30:14 +01:00
|
|
|
|
if (isnan(d))
|
|
|
|
|
return "NaN";
|
2022-10-26 23:16:59 -04:00
|
|
|
|
|
|
|
|
|
// 2. If x is +0𝔽 or -0𝔽, return "0".
|
2020-12-26 11:30:14 +01:00
|
|
|
|
if (d == +0.0 || d == -0.0)
|
|
|
|
|
return "0";
|
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 4. If x is +∞𝔽, return "Infinity".
|
|
|
|
|
if (isinf(d)) {
|
|
|
|
|
if (d > 0)
|
|
|
|
|
return "Infinity";
|
|
|
|
|
else
|
|
|
|
|
return "-Infinity";
|
2020-12-26 11:30:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
StringBuilder builder;
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 5. Let n, k, and s be integers such that k ≥ 1, radix ^ (k - 1) ≤ s < radix ^ k,
|
|
|
|
|
// 𝔽(s × radix ^ (n - k)) is x, and k is as small as possible. Note that k is the number of
|
|
|
|
|
// digits in the representation of s using radix radix, that s is not divisible by radix, and
|
|
|
|
|
// that the least significant digit of s is not necessarily uniquely determined by these criteria.
|
|
|
|
|
//
|
|
|
|
|
// Note: guarantees provided by convert_floating_point_to_decimal_exponential_form satisfy
|
|
|
|
|
// requirements of NOTE 2.
|
|
|
|
|
auto [sign, mantissa, exponent] = convert_floating_point_to_decimal_exponential_form(d);
|
|
|
|
|
i32 k = 0;
|
|
|
|
|
AK::Array<char, 20> mantissa_digits;
|
|
|
|
|
convert_to_decimal_digits_array(mantissa, mantissa_digits, k);
|
|
|
|
|
|
|
|
|
|
i32 n = exponent + k; // s = mantissa
|
|
|
|
|
|
|
|
|
|
// 3. If x < -0𝔽, return the string-concatenation of "-" and Number::toString(-x, radix).
|
|
|
|
|
if (sign)
|
|
|
|
|
builder.append('-');
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-11-04 11:33:26 -04:00
|
|
|
|
// Non-standard: Intl needs number-to-string conversions for extremely large numbers without any
|
|
|
|
|
// exponential formatting, as it will handle such formatting itself in a locale-aware way.
|
|
|
|
|
bool force_no_exponent = mode == NumberToStringMode::WithoutExponent;
|
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 6. If radix ≠ 10 or n is in the inclusive interval from -5 to 21, then
|
2022-11-04 11:33:26 -04:00
|
|
|
|
if ((n >= -5 && n <= 21) || force_no_exponent) {
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// a. If n ≥ k, then
|
|
|
|
|
if (n >= k) {
|
|
|
|
|
// i. Return the string-concatenation of:
|
|
|
|
|
// the code units of the k digits of the representation of s using radix radix
|
|
|
|
|
builder.append(mantissa_digits.data(), k);
|
|
|
|
|
// n - k occurrences of the code unit 0x0030 (DIGIT ZERO)
|
|
|
|
|
builder.append_repeated('0', n - k);
|
|
|
|
|
// b. Else if n > 0, then
|
|
|
|
|
} else if (n > 0) {
|
|
|
|
|
// i. Return the string-concatenation of:
|
|
|
|
|
// the code units of the most significant n digits of the representation of s using radix radix
|
|
|
|
|
builder.append(mantissa_digits.data(), n);
|
|
|
|
|
// the code unit 0x002E (FULL STOP)
|
|
|
|
|
builder.append('.');
|
|
|
|
|
// the code units of the remaining k - n digits of the representation of s using radix radix
|
|
|
|
|
builder.append(mantissa_digits.data() + n, k - n);
|
|
|
|
|
// c. Else,
|
|
|
|
|
} else {
|
|
|
|
|
// i. Assert: n ≤ 0.
|
|
|
|
|
VERIFY(n <= 0);
|
|
|
|
|
// ii. Return the string-concatenation of:
|
|
|
|
|
// the code unit 0x0030 (DIGIT ZERO)
|
|
|
|
|
builder.append('0');
|
|
|
|
|
// the code unit 0x002E (FULL STOP)
|
|
|
|
|
builder.append('.');
|
|
|
|
|
// -n occurrences of the code unit 0x0030 (DIGIT ZERO)
|
|
|
|
|
builder.append_repeated('0', -n);
|
|
|
|
|
// the code units of the k digits of the representation of s using radix radix
|
|
|
|
|
builder.append(mantissa_digits.data(), k);
|
|
|
|
|
}
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return builder.to_deprecated_string();
|
2020-12-26 11:30:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 7. NOTE: In this case, the input will be represented using scientific E notation, such as 1.2e+3.
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 9. If n < 0, then
|
|
|
|
|
// a. Let exponentSign be the code unit 0x002D (HYPHEN-MINUS).
|
|
|
|
|
// 10. Else,
|
|
|
|
|
// a. Let exponentSign be the code unit 0x002B (PLUS SIGN).
|
|
|
|
|
char exponent_sign = n < 0 ? '-' : '+';
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
AK::Array<char, 5> exponent_digits;
|
|
|
|
|
i32 exponent_length = 0;
|
|
|
|
|
convert_to_decimal_digits_array(abs(n - 1), exponent_digits, exponent_length);
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 11. If k is 1, then
|
|
|
|
|
if (k == 1) {
|
|
|
|
|
// a. Return the string-concatenation of:
|
|
|
|
|
// the code unit of the single digit of s
|
|
|
|
|
builder.append(mantissa_digits[0]);
|
|
|
|
|
// the code unit 0x0065 (LATIN SMALL LETTER E)
|
2020-12-26 11:30:14 +01:00
|
|
|
|
builder.append('e');
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// exponentSign
|
|
|
|
|
builder.append(exponent_sign);
|
|
|
|
|
// the code units of the decimal representation of abs(n - 1)
|
|
|
|
|
builder.append(exponent_digits.data(), exponent_length);
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return builder.to_deprecated_string();
|
2020-12-26 11:30:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// 12. Return the string-concatenation of:
|
|
|
|
|
// the code unit of the most significant digit of the decimal representation of s
|
|
|
|
|
builder.append(mantissa_digits[0]);
|
|
|
|
|
// the code unit 0x002E (FULL STOP)
|
2020-12-26 11:30:14 +01:00
|
|
|
|
builder.append('.');
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// the code units of the remaining k - 1 digits of the decimal representation of s
|
|
|
|
|
builder.append(mantissa_digits.data() + 1, k - 1);
|
|
|
|
|
// the code unit 0x0065 (LATIN SMALL LETTER E)
|
2020-12-26 11:30:14 +01:00
|
|
|
|
builder.append('e');
|
2022-10-26 23:16:59 -04:00
|
|
|
|
// exponentSign
|
|
|
|
|
builder.append(exponent_sign);
|
|
|
|
|
// the code units of the decimal representation of abs(n - 1)
|
|
|
|
|
builder.append(exponent_digits.data(), exponent_length);
|
2020-12-26 11:30:14 +01:00
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return builder.to_deprecated_string();
|
2020-12-26 11:30:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<bool> Value::is_array(VM& vm) const
|
2020-03-26 12:02:18 +01:00
|
|
|
|
{
|
2022-12-09 23:55:04 +00:00
|
|
|
|
// 1. If argument is not an Object, return false.
|
2021-06-08 21:53:36 +01:00
|
|
|
|
if (!is_object())
|
|
|
|
|
return false;
|
2022-12-09 23:55:04 +00:00
|
|
|
|
|
|
|
|
|
auto const& object = as_object();
|
|
|
|
|
|
|
|
|
|
// 2. If argument is an Array exotic object, return true.
|
2021-07-05 18:58:51 +01:00
|
|
|
|
if (is<Array>(object))
|
2021-06-08 21:53:36 +01:00
|
|
|
|
return true;
|
2022-12-09 23:55:04 +00:00
|
|
|
|
|
|
|
|
|
// 3. If argument is a Proxy exotic object, then
|
2021-06-08 21:53:36 +01:00
|
|
|
|
if (is<ProxyObject>(object)) {
|
2022-12-09 23:55:04 +00:00
|
|
|
|
auto const& proxy = static_cast<ProxyObject const&>(object);
|
|
|
|
|
|
|
|
|
|
// a. If argument.[[ProxyHandler]] is null, throw a TypeError exception.
|
2021-09-23 20:05:34 +03:00
|
|
|
|
if (proxy.is_revoked())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ProxyRevoked);
|
2022-12-09 23:55:04 +00:00
|
|
|
|
|
|
|
|
|
// b. Let target be argument.[[ProxyTarget]].
|
|
|
|
|
auto const& target = proxy.target();
|
|
|
|
|
|
|
|
|
|
// c. Return ? IsArray(target).
|
|
|
|
|
return Value(&target).is_array(vm);
|
2021-06-08 21:53:36 +01:00
|
|
|
|
}
|
2022-12-09 23:55:04 +00:00
|
|
|
|
|
|
|
|
|
// 4. Return false.
|
2021-06-08 21:53:36 +01:00
|
|
|
|
return false;
|
2020-03-26 12:02:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 11:01:00 -07:00
|
|
|
|
Array& Value::as_array()
|
|
|
|
|
{
|
2021-07-05 18:58:51 +01:00
|
|
|
|
VERIFY(is_object() && is<Array>(as_object()));
|
2022-02-25 01:26:52 +01:00
|
|
|
|
return static_cast<Array&>(as_object());
|
2020-06-10 11:01:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.3 IsCallable ( argument ), https://tc39.es/ecma262/#sec-iscallable
|
2020-05-06 11:52:53 +01:00
|
|
|
|
bool Value::is_function() const
|
|
|
|
|
{
|
2022-12-09 23:55:11 +00:00
|
|
|
|
// 1. If argument is not an Object, return false.
|
|
|
|
|
// 2. If argument has a [[Call]] internal method, return true.
|
|
|
|
|
// 3. Return false.
|
2020-05-06 11:52:53 +01:00
|
|
|
|
return is_object() && as_object().is_function();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
|
FunctionObject& Value::as_function()
|
2020-05-06 11:52:53 +01:00
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY(is_function());
|
2021-06-27 21:48:34 +02:00
|
|
|
|
return static_cast<FunctionObject&>(as_object());
|
2020-05-06 11:52:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 09:43:34 +02:00
|
|
|
|
FunctionObject const& Value::as_function() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_function());
|
|
|
|
|
return static_cast<FunctionObject const&>(as_object());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.4 IsConstructor ( argument ), https://tc39.es/ecma262/#sec-isconstructor
|
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
|
|
|
|
bool Value::is_constructor() const
|
2020-12-25 12:52:19 +11:00
|
|
|
|
{
|
2021-09-25 09:52:49 +02:00
|
|
|
|
// 1. If Type(argument) is not Object, return false.
|
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
|
|
|
|
if (!is_function())
|
|
|
|
|
return false;
|
2021-09-25 09:52:49 +02:00
|
|
|
|
|
|
|
|
|
// 2. If argument has a [[Construct]] internal method, return true.
|
|
|
|
|
if (as_function().has_constructor())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// 3. Return false.
|
|
|
|
|
return false;
|
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
|
|
|
|
}
|
2020-12-25 12:52:19 +11:00
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.8 IsRegExp ( argument ), https://tc39.es/ecma262/#sec-isregexp
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<bool> Value::is_regexp(VM& vm) const
|
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
|
|
|
|
{
|
2022-12-09 23:55:28 +00:00
|
|
|
|
// 1. If argument is not an Object, return false.
|
2020-12-25 12:52:19 +11:00
|
|
|
|
if (!is_object())
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-12-09 23:55:28 +00:00
|
|
|
|
// 2. Let matcher be ? Get(argument, @@match).
|
2021-10-02 23:52:27 +01:00
|
|
|
|
auto matcher = TRY(as_object().get(*vm.well_known_symbol_match()));
|
2022-12-09 23:55:28 +00:00
|
|
|
|
|
|
|
|
|
// 3. If matcher is not undefined, return ToBoolean(matcher).
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
|
if (!matcher.is_undefined())
|
2020-12-25 12:52:19 +11:00
|
|
|
|
return matcher.to_boolean();
|
|
|
|
|
|
2022-12-09 23:55:28 +00:00
|
|
|
|
// 4. If argument has a [[RegExpMatcher]] internal slot, return true.
|
|
|
|
|
// 5. Return false.
|
2021-01-01 17:46:39 +01:00
|
|
|
|
return is<RegExpObject>(as_object());
|
2020-12-25 12:52:19 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.5.3 The typeof Operator, https://tc39.es/ecma262/#sec-typeof-operator
|
2022-12-04 18:02:33 +00:00
|
|
|
|
DeprecatedString Value::typeof() const
|
2021-04-02 20:33:03 +02:00
|
|
|
|
{
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 9. If val is a Number, return "number".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_number())
|
|
|
|
|
return "number";
|
|
|
|
|
|
|
|
|
|
switch (m_value.tag) {
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 4. If val is undefined, return "undefined".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case UNDEFINED_TAG:
|
2021-04-02 20:33:03 +02:00
|
|
|
|
return "undefined";
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 5. If val is null, return "object".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case NULL_TAG:
|
2021-04-02 20:33:03 +02:00
|
|
|
|
return "object";
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 6. If val is a String, return "string".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case STRING_TAG:
|
2021-04-02 20:33:03 +02:00
|
|
|
|
return "string";
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 7. If val is a Symbol, return "symbol".
|
|
|
|
|
case SYMBOL_TAG:
|
|
|
|
|
return "symbol";
|
|
|
|
|
// 8. If val is a Boolean, return "boolean".
|
|
|
|
|
case BOOLEAN_TAG:
|
|
|
|
|
return "boolean";
|
|
|
|
|
// 10. If val is a BigInt, return "bigint".
|
|
|
|
|
case BIGINT_TAG:
|
|
|
|
|
return "bigint";
|
|
|
|
|
// 11. Assert: val is an Object.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case OBJECT_TAG:
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// B.3.6.3 Changes to the typeof Operator, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-typeof
|
|
|
|
|
// 12. If val has an [[IsHTMLDDA]] internal slot, return "undefined".
|
2021-06-20 17:08:29 +01:00
|
|
|
|
if (as_object().is_htmldda())
|
|
|
|
|
return "undefined";
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 13. If val has a [[Call]] internal slot, return "function".
|
2021-04-02 20:33:03 +02:00
|
|
|
|
if (is_function())
|
|
|
|
|
return "function";
|
2022-12-09 23:55:47 +00:00
|
|
|
|
// 14. Return "object".
|
2021-04-02 20:33:03 +02:00
|
|
|
|
return "object";
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
|
DeprecatedString Value::to_string_without_side_effects() const
|
2020-03-07 19:42:11 +01:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_double())
|
2022-11-04 11:33:26 -04:00
|
|
|
|
return number_to_string(m_value.as_double);
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
|
|
|
|
switch (m_value.tag) {
|
|
|
|
|
case UNDEFINED_TAG:
|
2020-03-07 23:11:17 +01:00
|
|
|
|
return "undefined";
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case NULL_TAG:
|
2020-06-04 21:36:52 +01:00
|
|
|
|
return "null";
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BOOLEAN_TAG:
|
|
|
|
|
return as_bool() ? "true" : "false";
|
|
|
|
|
case INT32_TAG:
|
2022-12-04 18:02:33 +00:00
|
|
|
|
return DeprecatedString::number(as_i32());
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case STRING_TAG:
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return as_string().deprecated_string();
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case SYMBOL_TAG:
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return as_symbol().to_deprecated_string();
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BIGINT_TAG:
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return as_bigint().to_deprecated_string();
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case OBJECT_TAG:
|
2022-12-04 18:02:33 +00:00
|
|
|
|
return DeprecatedString::formatted("[object {}]", as_object().class_name());
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case ACCESSOR_TAG:
|
2020-05-21 11:14:23 -07:00
|
|
|
|
return "<accessor>";
|
2020-06-04 21:36:52 +01:00
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-04 21:36:52 +01:00
|
|
|
|
}
|
2020-05-15 13:39:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<PrimitiveString*> Value::to_primitive_string(VM& vm)
|
2020-05-15 13:39:24 +02:00
|
|
|
|
{
|
|
|
|
|
if (is_string())
|
|
|
|
|
return &as_string();
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto string = TRY(to_string(vm));
|
2022-12-06 22:17:27 +00:00
|
|
|
|
return PrimitiveString::create(vm, string).ptr();
|
2020-05-15 13:39:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.17 ToString ( argument ), https://tc39.es/ecma262/#sec-tostring
|
2022-12-04 18:02:33 +00:00
|
|
|
|
ThrowCompletionOr<DeprecatedString> Value::to_string(VM& vm) const
|
2020-05-15 13:39:24 +02:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_double())
|
2022-11-04 11:33:26 -04:00
|
|
|
|
return number_to_string(m_value.as_double);
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
|
|
|
|
switch (m_value.tag) {
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 1. If argument is a String, return argument.
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
return as_string().deprecated_string();
|
|
|
|
|
// 2. If argument is a Symbol, throw a TypeError exception.
|
|
|
|
|
case SYMBOL_TAG:
|
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "string");
|
|
|
|
|
// 3. If argument is undefined, return "undefined".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case UNDEFINED_TAG:
|
2021-10-20 19:17:45 +01:00
|
|
|
|
return "undefined"sv;
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 4. If argument is null, return "null".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case NULL_TAG:
|
2021-10-20 19:17:45 +01:00
|
|
|
|
return "null"sv;
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 5. If argument is true, return "true".
|
|
|
|
|
// 6. If argument is false, return "false".
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BOOLEAN_TAG:
|
|
|
|
|
return as_bool() ? "true"sv : "false"sv;
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 7. If argument is a Number, return Number::toString(argument, 10).
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case INT32_TAG:
|
2022-12-04 18:02:33 +00:00
|
|
|
|
return DeprecatedString::number(as_i32());
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 8. If argument is a BigInt, return BigInt::toString(argument, 10).
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BIGINT_TAG:
|
|
|
|
|
return as_bigint().big_integer().to_base(10);
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 9. Assert: argument is an Object.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case OBJECT_TAG: {
|
2022-12-09 23:56:05 +00:00
|
|
|
|
// 10. Let primValue be ? ToPrimitive(argument, string).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto primitive_value = TRY(to_primitive(vm, PreferredType::String));
|
2022-12-09 23:56:05 +00:00
|
|
|
|
|
|
|
|
|
// 11. Assert: primValue is not an Object.
|
|
|
|
|
VERIFY(!primitive_value.is_object());
|
|
|
|
|
|
|
|
|
|
// 12. Return ? ToString(primValue).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return primitive_value.to_string(vm);
|
2020-04-29 16:29:26 +01:00
|
|
|
|
}
|
2020-06-04 21:36:52 +01:00
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-04 21:36:52 +01:00
|
|
|
|
}
|
2020-03-07 19:42:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Utf16String> Value::to_utf16_string(VM& vm) const
|
2021-07-20 10:46:53 -04:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_string())
|
|
|
|
|
return as_string().utf16_string();
|
2021-07-20 10:46:53 -04:00
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto utf8_string = TRY(to_string(vm));
|
2021-08-09 09:06:45 -04:00
|
|
|
|
return Utf16String(utf8_string);
|
2021-07-20 10:46:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.2 ToBoolean ( argument ), https://tc39.es/ecma262/#sec-toboolean
|
2020-03-10 08:46:20 +01:00
|
|
|
|
bool Value::to_boolean() const
|
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_double()) {
|
2020-06-04 21:36:52 +01:00
|
|
|
|
if (is_nan())
|
2020-04-06 22:48:47 -05:00
|
|
|
|
return false;
|
2020-06-04 21:36:52 +01:00
|
|
|
|
return m_value.as_double != 0;
|
2022-02-25 01:26:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (m_value.tag) {
|
2022-12-09 23:57:06 +00:00
|
|
|
|
// 1. If argument is a Boolean, return argument.
|
|
|
|
|
case BOOLEAN_TAG:
|
|
|
|
|
return as_bool();
|
|
|
|
|
// 2. If argument is any of undefined, null, +0𝔽, -0𝔽, NaN, 0ℤ, or the empty String, return false.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case UNDEFINED_TAG:
|
|
|
|
|
case NULL_TAG:
|
|
|
|
|
return false;
|
|
|
|
|
case INT32_TAG:
|
|
|
|
|
return as_i32() != 0;
|
|
|
|
|
case STRING_TAG:
|
|
|
|
|
return !as_string().is_empty();
|
|
|
|
|
case BIGINT_TAG:
|
|
|
|
|
return as_bigint().big_integer() != BIGINT_ZERO;
|
|
|
|
|
case OBJECT_TAG:
|
2022-12-09 23:57:06 +00:00
|
|
|
|
// B.3.6.1 Changes to ToBoolean, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-to-boolean
|
|
|
|
|
// 3. If argument is an Object and argument has an [[IsHTMLDDA]] internal slot, return false.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (as_object().is_htmldda())
|
2021-06-20 17:08:29 +01:00
|
|
|
|
return false;
|
2022-12-09 23:57:06 +00:00
|
|
|
|
// 4. Return true.
|
|
|
|
|
return true;
|
|
|
|
|
case SYMBOL_TAG:
|
2020-03-10 08:46:20 +01:00
|
|
|
|
return true;
|
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-10 08:46:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.1 ToPrimitive ( input [ , preferredType ] ), https://tc39.es/ecma262/#sec-toprimitive
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> Value::to_primitive(VM& vm, PreferredType preferred_type) const
|
2020-04-15 09:32:44 +02:00
|
|
|
|
{
|
2022-12-09 23:57:54 +00:00
|
|
|
|
// 1. If input is an Object, then
|
2020-11-03 19:52:21 +00:00
|
|
|
|
if (is_object()) {
|
2022-12-09 23:57:54 +00:00
|
|
|
|
// a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
|
|
|
|
|
auto* exotic_to_primitive = TRY(get_method(vm, *vm.well_known_symbol_to_primitive()));
|
|
|
|
|
|
|
|
|
|
// b. If exoticToPrim is not undefined, then
|
|
|
|
|
if (exotic_to_primitive) {
|
|
|
|
|
auto hint = [&]() -> DeprecatedString {
|
|
|
|
|
switch (preferred_type) {
|
|
|
|
|
// i. If preferredType is not present, let hint be "default".
|
|
|
|
|
case PreferredType::Default:
|
|
|
|
|
return "default";
|
|
|
|
|
// ii. Else if preferredType is string, let hint be "string".
|
|
|
|
|
case PreferredType::String:
|
|
|
|
|
return "string";
|
|
|
|
|
// iii. Else,
|
|
|
|
|
// 1. Assert: preferredType is number.
|
|
|
|
|
// 2. Let hint be "number".
|
|
|
|
|
case PreferredType::Number:
|
|
|
|
|
return "number";
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
// iv. Let result be ? Call(exoticToPrim, input, « hint »).
|
|
|
|
|
auto result = TRY(call(vm, *exotic_to_primitive, *this, PrimitiveString::create(vm, hint)));
|
|
|
|
|
|
|
|
|
|
// v. If result is not an Object, return result.
|
2021-03-02 19:22:36 +01:00
|
|
|
|
if (!result.is_object())
|
|
|
|
|
return result;
|
2022-12-09 23:57:54 +00:00
|
|
|
|
|
|
|
|
|
// vi. Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ToPrimitiveReturnedObject, to_string_without_side_effects(), hint);
|
2021-03-02 19:22:36 +01:00
|
|
|
|
}
|
2022-12-09 23:57:54 +00:00
|
|
|
|
|
|
|
|
|
// c. If preferredType is not present, let preferredType be number.
|
2020-11-03 19:52:21 +00:00
|
|
|
|
if (preferred_type == PreferredType::Default)
|
|
|
|
|
preferred_type = PreferredType::Number;
|
2022-12-09 23:57:54 +00:00
|
|
|
|
|
|
|
|
|
// d. Return ? OrdinaryToPrimitive(input, preferredType).
|
2021-10-12 18:09:14 +01:00
|
|
|
|
return as_object().ordinary_to_primitive(preferred_type);
|
2020-11-03 19:52:21 +00:00
|
|
|
|
}
|
2022-12-09 23:57:54 +00:00
|
|
|
|
|
|
|
|
|
// 2. Return input.
|
2020-04-15 09:32:44 +02:00
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.18 ToObject ( argument ), https://tc39.es/ecma262/#sec-toobject
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Object*> Value::to_object(VM& vm) const
|
2020-03-11 18:58:19 +01:00
|
|
|
|
{
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto& realm = *vm.current_realm();
|
2022-02-25 01:26:52 +01:00
|
|
|
|
VERIFY(!is_empty());
|
2022-12-09 23:58:26 +00:00
|
|
|
|
|
|
|
|
|
// Number
|
|
|
|
|
if (is_number()) {
|
|
|
|
|
// Return a new Number object whose [[NumberData]] internal slot is set to argument. See 21.1 for a description of Number objects.
|
2022-08-16 00:20:49 +01:00
|
|
|
|
return NumberObject::create(realm, as_double());
|
2022-12-09 23:58:26 +00:00
|
|
|
|
}
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
|
|
|
|
switch (m_value.tag) {
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Undefined
|
|
|
|
|
// Null
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case UNDEFINED_TAG:
|
|
|
|
|
case NULL_TAG:
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Throw a TypeError exception.
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ToObjectNullOrUndefined);
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Boolean
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BOOLEAN_TAG:
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Return a new Boolean object whose [[BooleanData]] internal slot is set to argument. See 20.3 for a description of Boolean objects.
|
2022-08-16 00:20:49 +01:00
|
|
|
|
return BooleanObject::create(realm, as_bool());
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// String
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case STRING_TAG:
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Return a new String object whose [[StringData]] internal slot is set to argument. See 22.1 for a description of String objects.
|
2022-08-27 00:54:55 +01:00
|
|
|
|
return StringObject::create(realm, const_cast<JS::PrimitiveString&>(as_string()), *realm.intrinsics().string_prototype());
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Symbol
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case SYMBOL_TAG:
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Return a new Symbol object whose [[SymbolData]] internal slot is set to argument. See 20.4 for a description of Symbol objects.
|
2022-08-16 00:20:49 +01:00
|
|
|
|
return SymbolObject::create(realm, const_cast<JS::Symbol&>(as_symbol()));
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// BigInt
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BIGINT_TAG:
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Return a new BigInt object whose [[BigIntData]] internal slot is set to argument. See 21.2 for a description of BigInt objects.
|
2022-08-16 00:20:49 +01:00
|
|
|
|
return BigIntObject::create(realm, const_cast<JS::BigInt&>(as_bigint()));
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Object
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case OBJECT_TAG:
|
2022-12-09 23:58:26 +00:00
|
|
|
|
// Return argument.
|
2020-06-04 21:36:52 +01:00
|
|
|
|
return &const_cast<Object&>(as_object());
|
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-28 22:48:35 +01:00
|
|
|
|
}
|
2020-03-11 18:58:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
|
2022-08-21 14:00:56 +01:00
|
|
|
|
FLATTEN ThrowCompletionOr<Value> Value::to_numeric(VM& vm) const
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2022-12-09 23:59:02 +00:00
|
|
|
|
// 1. Let primValue be ? ToPrimitive(value, number).
|
|
|
|
|
auto primitive_value = TRY(to_primitive(vm, Value::PreferredType::Number));
|
|
|
|
|
|
|
|
|
|
// 2. If primValue is a BigInt, return primValue.
|
|
|
|
|
if (primitive_value.is_bigint())
|
|
|
|
|
return primitive_value;
|
|
|
|
|
|
|
|
|
|
// 3. Return ? ToNumber(primValue).
|
|
|
|
|
return primitive_value.to_number(vm);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 11:14:04 -05:00
|
|
|
|
constexpr bool is_ascii_number(u32 code_point)
|
|
|
|
|
{
|
|
|
|
|
return is_ascii_digit(code_point) || code_point == '.' || (code_point == 'e' || code_point == 'E') || code_point == '+' || code_point == '-';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct NumberParseResult {
|
|
|
|
|
StringView literal;
|
|
|
|
|
u8 base;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Optional<NumberParseResult> parse_number_text(StringView text)
|
|
|
|
|
{
|
|
|
|
|
NumberParseResult result {};
|
|
|
|
|
|
|
|
|
|
auto check_prefix = [&](auto lower_prefix, auto upper_prefix) {
|
|
|
|
|
if (text.length() <= 2)
|
|
|
|
|
return false;
|
|
|
|
|
if (!text.starts_with(lower_prefix) && !text.starts_with(upper_prefix))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://tc39.es/ecma262/#sec-tonumber-applied-to-the-string-type
|
|
|
|
|
if (check_prefix("0b"sv, "0B"sv)) {
|
|
|
|
|
if (!all_of(text.substring_view(2), is_ascii_binary_digit))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
result.literal = text.substring_view(2);
|
|
|
|
|
result.base = 2;
|
|
|
|
|
} else if (check_prefix("0o"sv, "0O"sv)) {
|
|
|
|
|
if (!all_of(text.substring_view(2), is_ascii_octal_digit))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
result.literal = text.substring_view(2);
|
|
|
|
|
result.base = 8;
|
|
|
|
|
} else if (check_prefix("0x"sv, "0X"sv)) {
|
|
|
|
|
if (!all_of(text.substring_view(2), is_ascii_hex_digit))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
result.literal = text.substring_view(2);
|
|
|
|
|
result.base = 16;
|
|
|
|
|
} else {
|
|
|
|
|
if (!all_of(text, is_ascii_number))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
result.literal = text;
|
|
|
|
|
result.base = 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.1.4.1.1 StringToNumber ( str ), https://tc39.es/ecma262/#sec-stringtonumber
|
2022-10-14 09:22:47 -04:00
|
|
|
|
Optional<Value> string_to_number(StringView string)
|
2022-08-26 11:14:04 -05:00
|
|
|
|
{
|
|
|
|
|
// 1. Let text be StringToCodePoints(str).
|
2022-12-04 18:02:33 +00:00
|
|
|
|
DeprecatedString text = Utf8View(string).trim(whitespace_characters, AK::TrimMode::Both).as_string();
|
2022-08-26 11:14:04 -05:00
|
|
|
|
|
|
|
|
|
// 2. Let literal be ParseText(text, StringNumericLiteral).
|
|
|
|
|
if (text.is_empty())
|
|
|
|
|
return Value(0);
|
|
|
|
|
if (text == "Infinity" || text == "+Infinity")
|
|
|
|
|
return js_infinity();
|
|
|
|
|
if (text == "-Infinity")
|
|
|
|
|
return js_negative_infinity();
|
|
|
|
|
|
|
|
|
|
auto result = parse_number_text(text);
|
|
|
|
|
|
|
|
|
|
// 3. If literal is a List of errors, return NaN.
|
|
|
|
|
if (!result.has_value())
|
|
|
|
|
return js_nan();
|
|
|
|
|
|
|
|
|
|
// 4. Return StringNumericValue of literal.
|
|
|
|
|
if (result->base != 10) {
|
|
|
|
|
auto bigint = Crypto::UnsignedBigInteger::from_base(result->base, result->literal);
|
|
|
|
|
return Value(bigint.to_double());
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 02:23:50 +02:00
|
|
|
|
auto maybe_double = text.to_double(AK::TrimWhitespace::No);
|
|
|
|
|
if (!maybe_double.has_value())
|
2022-08-26 11:14:04 -05:00
|
|
|
|
return js_nan();
|
|
|
|
|
|
2022-10-12 02:23:50 +02:00
|
|
|
|
return Value(*maybe_double);
|
2022-08-26 11:14:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> Value::to_number(VM& vm) const
|
2020-03-15 15:00:18 +01:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
VERIFY(!is_empty());
|
2022-12-09 23:59:28 +00:00
|
|
|
|
|
|
|
|
|
// 1. If argument is a Number, return argument.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_number())
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
|
|
switch (m_value.tag) {
|
2022-12-09 23:59:28 +00:00
|
|
|
|
// 2. If argument is either a Symbol or a BigInt, throw a TypeError exception.
|
|
|
|
|
case SYMBOL_TAG:
|
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "number");
|
|
|
|
|
case BIGINT_TAG:
|
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "BigInt", "number");
|
|
|
|
|
// 3. If argument is undefined, return NaN.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case UNDEFINED_TAG:
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return js_nan();
|
2022-12-09 23:59:28 +00:00
|
|
|
|
// 4. If argument is either null or false, return +0𝔽.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case NULL_TAG:
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return Value(0);
|
2022-12-09 23:59:28 +00:00
|
|
|
|
// 5. If argument is true, return 1𝔽.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BOOLEAN_TAG:
|
|
|
|
|
return Value(as_bool() ? 1 : 0);
|
2022-12-09 23:59:28 +00:00
|
|
|
|
// 6. If argument is a String, return StringToNumber(argument).
|
2022-08-26 11:14:04 -05:00
|
|
|
|
case STRING_TAG:
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return string_to_number(as_string().deprecated_string().view());
|
2022-12-09 23:59:28 +00:00
|
|
|
|
// 7. Assert: argument is an Object.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case OBJECT_TAG: {
|
2022-12-09 23:59:28 +00:00
|
|
|
|
// 8. Let primValue be ? ToPrimitive(argument, number).
|
|
|
|
|
auto primitive_value = TRY(to_primitive(vm, PreferredType::Number));
|
|
|
|
|
|
|
|
|
|
// 9. Assert: primValue is not an Object.
|
|
|
|
|
VERIFY(!primitive_value.is_object());
|
|
|
|
|
|
|
|
|
|
// 10. Return ? ToNumber(primValue).
|
|
|
|
|
return primitive_value.to_number(vm);
|
2020-03-16 00:19:41 +02:00
|
|
|
|
}
|
2020-06-04 21:36:52 +01:00
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-04 21:36:52 +01:00
|
|
|
|
}
|
2020-03-16 00:19:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 10:29:13 +01:00
|
|
|
|
static Optional<BigInt*> string_to_bigint(VM& vm, StringView string);
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.13 ToBigInt ( argument ), https://tc39.es/ecma262/#sec-tobigint
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<BigInt*> Value::to_bigint(VM& vm) const
|
2020-06-06 01:14:10 +01:00
|
|
|
|
{
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// 1. Let prim be ? ToPrimitive(argument, number).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto primitive = TRY(to_primitive(vm, PreferredType::Number));
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// 2. Return the value that prim corresponds to in Table 12.
|
|
|
|
|
|
|
|
|
|
// Number
|
|
|
|
|
if (primitive.is_number()) {
|
|
|
|
|
// Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "number", "BigInt");
|
2022-12-09 23:59:51 +00:00
|
|
|
|
}
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
|
|
|
|
switch (primitive.m_value.tag) {
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Undefined
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case UNDEFINED_TAG:
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "undefined", "BigInt");
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Null
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case NULL_TAG:
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "null", "BigInt");
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Boolean
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BOOLEAN_TAG: {
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Return 1n if prim is true and 0n if prim is false.
|
2020-06-06 01:14:10 +01:00
|
|
|
|
auto value = primitive.as_bool() ? 1 : 0;
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, Crypto::SignedBigInteger { value }).ptr();
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// BigInt
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case BIGINT_TAG:
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Return prim.
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return &primitive.as_bigint();
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case STRING_TAG: {
|
|
|
|
|
// 1. Let n be ! StringToBigInt(prim).
|
2022-12-06 01:12:49 +00:00
|
|
|
|
auto bigint = string_to_bigint(vm, primitive.as_string().deprecated_string());
|
2022-01-31 10:23:51 -05:00
|
|
|
|
|
|
|
|
|
// 2. If n is undefined, throw a SyntaxError exception.
|
|
|
|
|
if (!bigint.has_value())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<SyntaxError>(ErrorType::BigIntInvalidValue, primitive);
|
2022-01-31 10:23:51 -05:00
|
|
|
|
|
|
|
|
|
// 3. Return n.
|
|
|
|
|
return bigint.release_value();
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Symbol
|
2022-02-25 01:26:52 +01:00
|
|
|
|
case SYMBOL_TAG:
|
2022-12-09 23:59:51 +00:00
|
|
|
|
// Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::Convert, "symbol", "BigInt");
|
2020-06-06 01:14:10 +01:00
|
|
|
|
default:
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 10:23:51 -05:00
|
|
|
|
struct BigIntParseResult {
|
|
|
|
|
StringView literal;
|
|
|
|
|
u8 base { 10 };
|
|
|
|
|
bool is_negative { false };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Optional<BigIntParseResult> parse_bigint_text(StringView text)
|
|
|
|
|
{
|
|
|
|
|
BigIntParseResult result {};
|
|
|
|
|
|
|
|
|
|
auto parse_for_prefixed_base = [&](auto lower_prefix, auto upper_prefix, auto validator) {
|
|
|
|
|
if (text.length() <= 2)
|
|
|
|
|
return false;
|
|
|
|
|
if (!text.starts_with(lower_prefix) && !text.starts_with(upper_prefix))
|
|
|
|
|
return false;
|
|
|
|
|
return all_of(text.substring_view(2), validator);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (parse_for_prefixed_base("0b"sv, "0B"sv, is_ascii_binary_digit)) {
|
|
|
|
|
result.literal = text.substring_view(2);
|
|
|
|
|
result.base = 2;
|
|
|
|
|
} else if (parse_for_prefixed_base("0o"sv, "0O"sv, is_ascii_octal_digit)) {
|
|
|
|
|
result.literal = text.substring_view(2);
|
|
|
|
|
result.base = 8;
|
|
|
|
|
} else if (parse_for_prefixed_base("0x"sv, "0X"sv, is_ascii_hex_digit)) {
|
|
|
|
|
result.literal = text.substring_view(2);
|
|
|
|
|
result.base = 16;
|
|
|
|
|
} else {
|
|
|
|
|
if (text.starts_with('-')) {
|
|
|
|
|
text = text.substring_view(1);
|
|
|
|
|
result.is_negative = true;
|
|
|
|
|
} else if (text.starts_with('+')) {
|
|
|
|
|
text = text.substring_view(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!all_of(text, is_ascii_digit))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
result.literal = text;
|
|
|
|
|
result.base = 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.1.14 StringToBigInt ( str ), https://tc39.es/ecma262/#sec-stringtobigint
|
2022-08-30 10:29:13 +01:00
|
|
|
|
static Optional<BigInt*> string_to_bigint(VM& vm, StringView string)
|
2022-01-31 10:23:51 -05:00
|
|
|
|
{
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 1. Let text be StringToCodePoints(str).
|
2022-08-30 10:29:13 +01:00
|
|
|
|
auto text = Utf8View(string).trim(whitespace_characters, AK::TrimMode::Both).as_string();
|
2022-01-31 10:23:51 -05:00
|
|
|
|
|
|
|
|
|
// 2. Let literal be ParseText(text, StringIntegerLiteral).
|
|
|
|
|
auto result = parse_bigint_text(text);
|
|
|
|
|
|
|
|
|
|
// 3. If literal is a List of errors, return undefined.
|
|
|
|
|
if (!result.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// 4. Let mv be the MV of literal.
|
|
|
|
|
// 5. Assert: mv is an integer.
|
|
|
|
|
auto bigint = Crypto::SignedBigInteger::from_base(result->base, result->literal);
|
|
|
|
|
if (result->is_negative && (bigint != BIGINT_ZERO))
|
|
|
|
|
bigint.negate();
|
|
|
|
|
|
|
|
|
|
// 6. Return ℤ(mv).
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, move(bigint));
|
2022-01-31 10:23:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 18:37:57 +00:00
|
|
|
|
// 7.1.15 ToBigInt64 ( argument ), https://tc39.es/ecma262/#sec-tobigint64
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<i64> Value::to_bigint_int64(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:00:07 +00:00
|
|
|
|
// 1. Let n be ? ToBigInt(argument).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto* bigint = TRY(to_bigint(vm));
|
2022-12-10 00:00:07 +00:00
|
|
|
|
|
|
|
|
|
// 2. Let int64bit be ℝ(n) modulo 2^64.
|
|
|
|
|
// 3. If int64bit ≥ 2^63, return ℤ(int64bit - 2^64); otherwise return ℤ(int64bit).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return static_cast<i64>(bigint->big_integer().to_u64());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 18:37:57 +00:00
|
|
|
|
// 7.1.16 ToBigUint64 ( argument ), https://tc39.es/ecma262/#sec-tobiguint64
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<u64> Value::to_bigint_uint64(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:00:17 +00:00
|
|
|
|
// 1. Let n be ? ToBigInt(argument).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto* bigint = TRY(to_bigint(vm));
|
2022-12-10 00:00:17 +00:00
|
|
|
|
|
|
|
|
|
// 2. Let int64bit be ℝ(n) modulo 2^64.
|
|
|
|
|
// 3. Return ℤ(int64bit).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return bigint->big_integer().to_u64();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<double> Value::to_double(VM& vm) const
|
2020-04-08 11:22:20 +02:00
|
|
|
|
{
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return TRY(to_number(vm)).as_double();
|
2020-04-08 11:22:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<PropertyKey> Value::to_property_key(VM& vm) const
|
2021-06-05 15:22:11 +03:00
|
|
|
|
{
|
2022-12-10 00:00:33 +00:00
|
|
|
|
// OPTIMIZATION: Return the value as a numeric PropertyKey, if possible.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_int32() && as_i32() >= 0)
|
2021-10-25 15:37:28 +02:00
|
|
|
|
return PropertyKey { as_i32() };
|
2022-12-10 00:00:33 +00:00
|
|
|
|
|
|
|
|
|
// 1. Let key be ? ToPrimitive(argument, string).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto key = TRY(to_primitive(vm, PreferredType::String));
|
2022-12-10 00:00:33 +00:00
|
|
|
|
|
|
|
|
|
// 2. If key is a Symbol, then
|
|
|
|
|
if (key.is_symbol()) {
|
|
|
|
|
// a. Return key.
|
2021-10-20 19:17:45 +01:00
|
|
|
|
return &key.as_symbol();
|
2022-12-10 00:00:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Return ! ToString(key).
|
|
|
|
|
return MUST(key.to_string(vm));
|
2021-06-05 15:22:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 00:01:05 +00:00
|
|
|
|
// 7.1.6 ToInt32 ( argument ), https://tc39.es/ecma262/#sec-toint32
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<i32> Value::to_i32_slow_case(VM& vm) const
|
2020-05-18 00:28:00 +01:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
VERIFY(!is_int32());
|
2022-12-10 00:01:05 +00:00
|
|
|
|
|
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
double number = TRY(to_number(vm)).as_double();
|
|
|
|
|
|
|
|
|
|
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
|
|
|
if (!isfinite(number) || number == 0)
|
2020-05-23 15:02:14 +01:00
|
|
|
|
return 0;
|
2022-12-10 00:01:05 +00:00
|
|
|
|
|
|
|
|
|
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
|
|
|
auto abs = fabs(number);
|
2021-02-05 09:11:58 +01:00
|
|
|
|
auto int_val = floor(abs);
|
2022-12-10 00:01:05 +00:00
|
|
|
|
if (signbit(number))
|
2021-02-05 09:11:58 +01:00
|
|
|
|
int_val = -int_val;
|
2022-12-10 00:01:05 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let int32bit be int modulo 2^32.
|
2021-06-06 02:33:55 +03:00
|
|
|
|
auto remainder = fmod(int_val, 4294967296.0);
|
|
|
|
|
auto int32bit = remainder >= 0.0 ? remainder : remainder + 4294967296.0; // The notation “x modulo y” computes a value k of the same sign as y
|
2022-12-10 00:01:05 +00:00
|
|
|
|
|
|
|
|
|
// 5. If int32bit ≥ 2^31, return 𝔽(int32bit - 2^32); otherwise return 𝔽(int32bit).
|
2021-02-05 09:11:58 +01:00
|
|
|
|
if (int32bit >= 2147483648.0)
|
|
|
|
|
int32bit -= 4294967296.0;
|
|
|
|
|
return static_cast<i32>(int32bit);
|
2020-05-18 00:28:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 00:01:05 +00:00
|
|
|
|
// 7.1.6 ToInt32 ( argument ), https://tc39.es/ecma262/#sec-toint32
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<i32> Value::to_i32(VM& vm) const
|
2021-10-17 23:33:35 +03:00
|
|
|
|
{
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (is_int32())
|
|
|
|
|
return as_i32();
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return to_i32_slow_case(vm);
|
2021-10-17 23:33:35 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<u32> Value::to_u32(VM& vm) const
|
2021-01-09 21:52:47 +01:00
|
|
|
|
{
|
2022-12-10 00:01:18 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
double number = TRY(to_number(vm)).as_double();
|
|
|
|
|
|
|
|
|
|
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
|
|
|
if (!isfinite(number) || number == 0)
|
2021-01-09 21:52:47 +01:00
|
|
|
|
return 0;
|
2022-12-10 00:01:18 +00:00
|
|
|
|
|
|
|
|
|
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
|
|
|
auto int_val = floor(fabs(number));
|
|
|
|
|
if (signbit(number))
|
2021-02-05 09:11:58 +01:00
|
|
|
|
int_val = -int_val;
|
2022-12-10 00:01:18 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let int32bit be int modulo 2^32.
|
2021-02-05 09:11:58 +01:00
|
|
|
|
auto int32bit = fmod(int_val, NumericLimits<u32>::max() + 1.0);
|
2022-12-10 00:01:18 +00:00
|
|
|
|
|
|
|
|
|
// 5. Return 𝔽(int32bit).
|
2021-05-31 13:05:39 -06:00
|
|
|
|
// Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior
|
|
|
|
|
// Otherwise, negative numbers cause a UBSAN warning.
|
|
|
|
|
return static_cast<u32>(static_cast<i64>(int32bit));
|
2021-01-09 21:52:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 00:13:26 +01:00
|
|
|
|
// 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<i16> Value::to_i16(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:01:31 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
double number = TRY(to_number(vm)).as_double();
|
|
|
|
|
|
|
|
|
|
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
|
|
|
if (!isfinite(number) || number == 0)
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return 0;
|
2022-12-10 00:01:31 +00:00
|
|
|
|
|
|
|
|
|
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
|
|
|
auto abs = fabs(number);
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto int_val = floor(abs);
|
2022-12-10 00:01:31 +00:00
|
|
|
|
if (signbit(number))
|
2021-06-17 00:13:26 +01:00
|
|
|
|
int_val = -int_val;
|
2022-12-10 00:01:31 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let int16bit be int modulo 2^16.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto remainder = fmod(int_val, 65536.0);
|
|
|
|
|
auto int16bit = remainder >= 0.0 ? remainder : remainder + 65536.0; // The notation “x modulo y” computes a value k of the same sign as y
|
2022-12-10 00:01:31 +00:00
|
|
|
|
|
|
|
|
|
// 5. If int16bit ≥ 2^15, return 𝔽(int16bit - 2^16); otherwise return 𝔽(int16bit).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (int16bit >= 32768.0)
|
|
|
|
|
int16bit -= 65536.0;
|
|
|
|
|
return static_cast<i16>(int16bit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.1.9 ToUint16 ( argument ), https://tc39.es/ecma262/#sec-touint16
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<u16> Value::to_u16(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:01:45 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
double number = TRY(to_number(vm)).as_double();
|
|
|
|
|
|
|
|
|
|
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
|
|
|
if (!isfinite(number) || number == 0)
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return 0;
|
2022-12-10 00:01:45 +00:00
|
|
|
|
|
|
|
|
|
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
|
|
|
auto int_val = floor(fabs(number));
|
|
|
|
|
if (signbit(number))
|
2021-06-17 00:13:26 +01:00
|
|
|
|
int_val = -int_val;
|
2022-12-10 00:01:45 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let int16bit be int modulo 2^16.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto int16bit = fmod(int_val, NumericLimits<u16>::max() + 1.0);
|
2021-08-06 20:42:36 +02:00
|
|
|
|
if (int16bit < 0)
|
|
|
|
|
int16bit += NumericLimits<u16>::max() + 1.0;
|
2022-12-10 00:01:45 +00:00
|
|
|
|
|
|
|
|
|
// 5. Return 𝔽(int16bit).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return static_cast<u16>(int16bit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.1.10 ToInt8 ( argument ), https://tc39.es/ecma262/#sec-toint8
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<i8> Value::to_i8(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:01:58 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
double number = TRY(to_number(vm)).as_double();
|
|
|
|
|
|
|
|
|
|
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
|
|
|
if (!isfinite(number) || number == 0)
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return 0;
|
2022-12-10 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
|
|
|
auto abs = fabs(number);
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto int_val = floor(abs);
|
2022-12-10 00:01:58 +00:00
|
|
|
|
if (signbit(number))
|
2021-06-17 00:13:26 +01:00
|
|
|
|
int_val = -int_val;
|
2022-12-10 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let int8bit be int modulo 2^8.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto remainder = fmod(int_val, 256.0);
|
|
|
|
|
auto int8bit = remainder >= 0.0 ? remainder : remainder + 256.0; // The notation “x modulo y” computes a value k of the same sign as y
|
2022-12-10 00:01:58 +00:00
|
|
|
|
|
|
|
|
|
// 5. If int8bit ≥ 2^7, return 𝔽(int8bit - 2^8); otherwise return 𝔽(int8bit).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (int8bit >= 128.0)
|
|
|
|
|
int8bit -= 256.0;
|
|
|
|
|
return static_cast<i8>(int8bit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<u8> Value::to_u8(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:02:10 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
double number = TRY(to_number(vm)).as_double();
|
|
|
|
|
|
|
|
|
|
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
|
|
|
if (!isfinite(number) || number == 0)
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return 0;
|
2022-12-10 00:02:10 +00:00
|
|
|
|
|
|
|
|
|
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
|
|
|
auto int_val = floor(fabs(number));
|
|
|
|
|
if (signbit(number))
|
2021-06-17 00:13:26 +01:00
|
|
|
|
int_val = -int_val;
|
2022-12-10 00:02:10 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let int8bit be int modulo 2^8.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto int8bit = fmod(int_val, NumericLimits<u8>::max() + 1.0);
|
2021-08-06 20:42:36 +02:00
|
|
|
|
if (int8bit < 0)
|
|
|
|
|
int8bit += NumericLimits<u8>::max() + 1.0;
|
2022-12-10 00:02:10 +00:00
|
|
|
|
|
|
|
|
|
// 5. Return 𝔽(int8bit).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return static_cast<u8>(int8bit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7.1.12 ToUint8Clamp ( argument ), https://tc39.es/ecma262/#sec-touint8clamp
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<u8> Value::to_u8_clamp(VM& vm) const
|
2021-06-17 00:13:26 +01:00
|
|
|
|
{
|
2022-12-10 00:02:28 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto number = TRY(to_number(vm));
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 2. If number is NaN, return +0𝔽.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (number.is_nan())
|
|
|
|
|
return 0;
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
2021-06-17 00:13:26 +01:00
|
|
|
|
double value = number.as_double();
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 3. If ℝ(number) ≤ 0, return +0𝔽.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (value <= 0.0)
|
|
|
|
|
return 0;
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 4. If ℝ(number) ≥ 255, return 255𝔽.
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (value >= 255.0)
|
|
|
|
|
return 255;
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 5. Let f be floor(ℝ(number)).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
auto int_val = floor(value);
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 6. If f + 0.5 < ℝ(number), return 𝔽(f + 1).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (int_val + 0.5 < value)
|
|
|
|
|
return static_cast<u8>(int_val + 1.0);
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 7. If ℝ(number) < f + 0.5, return 𝔽(f).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (value < int_val + 0.5)
|
|
|
|
|
return static_cast<u8>(int_val);
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 8. If f is odd, return 𝔽(f + 1).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
if (fmod(int_val, 2.0) == 1.0)
|
|
|
|
|
return static_cast<u8>(int_val + 1.0);
|
2022-12-10 00:02:28 +00:00
|
|
|
|
|
|
|
|
|
// 9. Return 𝔽(f).
|
2021-06-17 00:13:26 +01:00
|
|
|
|
return static_cast<u8>(int_val);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.20 ToLength ( argument ), https://tc39.es/ecma262/#sec-tolength
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<size_t> Value::to_length(VM& vm) const
|
2020-12-02 13:23:51 +00:00
|
|
|
|
{
|
2022-12-10 00:02:48 +00:00
|
|
|
|
// 1. Let len be ? ToIntegerOrInfinity(argument).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto len = TRY(to_integer_or_infinity(vm));
|
2022-12-10 00:02:48 +00:00
|
|
|
|
|
|
|
|
|
// 2. If len ≤ 0, return +0𝔽.
|
2020-12-02 13:23:51 +00:00
|
|
|
|
if (len <= 0)
|
|
|
|
|
return 0;
|
2022-12-10 00:02:48 +00:00
|
|
|
|
|
|
|
|
|
// FIXME: The expected output range is 0 - 2^53-1, but we don't want to overflow the size_t on 32-bit platforms.
|
|
|
|
|
// Convert this to u64 so it works everywhere.
|
2021-08-07 23:47:39 +02:00
|
|
|
|
constexpr double length_limit = sizeof(void*) == 4 ? NumericLimits<size_t>::max() : MAX_ARRAY_LIKE_INDEX;
|
2022-12-10 00:02:48 +00:00
|
|
|
|
|
|
|
|
|
// 3. Return 𝔽(min(len, 2^53 - 1)).
|
2021-08-07 23:47:39 +02:00
|
|
|
|
return min(len, length_limit);
|
2020-12-02 13:23:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.22 ToIndex ( argument ), https://tc39.es/ecma262/#sec-toindex
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<size_t> Value::to_index(VM& vm) const
|
2020-12-02 13:23:51 +00:00
|
|
|
|
{
|
2022-12-10 00:03:10 +00:00
|
|
|
|
// 1. If value is undefined, then
|
|
|
|
|
if (is_undefined()) {
|
|
|
|
|
// a. Return 0.
|
2020-12-02 13:23:51 +00:00
|
|
|
|
return 0;
|
2022-12-10 00:03:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Else,
|
|
|
|
|
// a. Let integer be ? ToIntegerOrInfinity(value).
|
|
|
|
|
auto integer = TRY(to_integer_or_infinity(vm));
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: If the value is negative, ToLength normalizes it to 0, and we fail the SameValue comparison below.
|
|
|
|
|
// Bail out early instead.
|
|
|
|
|
if (integer < 0)
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::InvalidIndex);
|
2022-12-10 00:03:10 +00:00
|
|
|
|
|
|
|
|
|
// b. Let clamped be ! ToLength(𝔽(integer)).
|
|
|
|
|
auto clamped = MUST(Value(integer).to_length(vm));
|
|
|
|
|
|
|
|
|
|
// c. If SameValue(𝔽(integer), clamped) is false, throw a RangeError exception.
|
|
|
|
|
if (integer != clamped)
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::InvalidIndex);
|
2022-12-10 00:03:10 +00:00
|
|
|
|
|
|
|
|
|
// d. Assert: 0 ≤ integer ≤ 2^53 - 1.
|
|
|
|
|
VERIFY(0 <= integer && integer <= MAX_ARRAY_LIKE_INDEX);
|
|
|
|
|
|
|
|
|
|
// e. Return integer.
|
|
|
|
|
// NOTE: We return the clamped value here, which already has the right type.
|
|
|
|
|
return clamped;
|
2020-12-02 13:23:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<double> Value::to_integer_or_infinity(VM& vm) const
|
2020-12-02 13:23:51 +00:00
|
|
|
|
{
|
2022-12-10 00:03:24 +00:00
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto number = TRY(to_number(vm));
|
2022-12-10 00:03:24 +00:00
|
|
|
|
|
|
|
|
|
// 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
|
2020-12-02 13:23:51 +00:00
|
|
|
|
if (number.is_nan() || number.as_double() == 0)
|
|
|
|
|
return 0;
|
2022-12-10 00:03:24 +00:00
|
|
|
|
|
|
|
|
|
// 3. If number is +∞𝔽, return +∞.
|
|
|
|
|
// 4. If number is -∞𝔽, return -∞.
|
2020-12-02 13:23:51 +00:00
|
|
|
|
if (number.is_infinity())
|
|
|
|
|
return number.as_double();
|
2022-12-10 00:03:24 +00:00
|
|
|
|
|
|
|
|
|
// 5. Let integer be floor(abs(ℝ(number))).
|
2021-02-15 11:51:08 +01:00
|
|
|
|
auto integer = floor(fabs(number.as_double()));
|
2022-12-10 00:03:24 +00:00
|
|
|
|
|
|
|
|
|
// 6. If number < -0𝔽, set integer to -integer.
|
|
|
|
|
// NOTE: The zero check is required as 'integer' is a double here but an MV in the spec,
|
|
|
|
|
// which doesn't have negative zero.
|
2022-04-30 21:38:32 +03:00
|
|
|
|
if (number.as_double() < 0 && integer != 0)
|
2020-12-02 13:23:51 +00:00
|
|
|
|
integer = -integer;
|
2022-12-10 00:03:24 +00:00
|
|
|
|
|
|
|
|
|
// 7. Return integer.
|
2020-12-02 13:23:51 +00:00
|
|
|
|
return integer;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 20:17:16 +02:00
|
|
|
|
// Standalone variant using plain doubles for cases where we already got numbers and know the AO won't throw.
|
|
|
|
|
double to_integer_or_infinity(double number)
|
|
|
|
|
{
|
|
|
|
|
// 1. Let number be ? ToNumber(argument).
|
|
|
|
|
|
|
|
|
|
// 2. If number is NaN, +0𝔽, or -0𝔽, return 0.
|
|
|
|
|
if (isnan(number) || number == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// 3. If number is +∞𝔽, return +∞.
|
|
|
|
|
if (__builtin_isinf_sign(number) > 0)
|
|
|
|
|
return static_cast<double>(INFINITY);
|
|
|
|
|
|
|
|
|
|
// 4. If number is -∞𝔽, return -∞.
|
|
|
|
|
if (__builtin_isinf_sign(number) < 0)
|
|
|
|
|
return static_cast<double>(-INFINITY);
|
|
|
|
|
|
|
|
|
|
// 5. Let integer be floor(abs(ℝ(number))).
|
|
|
|
|
auto integer = floor(fabs(number));
|
|
|
|
|
|
|
|
|
|
// 6. If number < -0𝔽, set integer to -integer.
|
2022-12-10 00:03:24 +00:00
|
|
|
|
// NOTE: The zero check is required as 'integer' is a double here but an MV in the spec,
|
|
|
|
|
// which doesn't have negative zero.
|
2022-05-06 20:17:16 +02:00
|
|
|
|
if (number < 0 && integer != 0)
|
|
|
|
|
integer = -integer;
|
|
|
|
|
|
|
|
|
|
// 7. Return integer.
|
|
|
|
|
return integer;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 19:17:28 +01:00
|
|
|
|
// 7.3.3 GetV ( V, P ), https://tc39.es/ecma262/#sec-getv
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> Value::get(VM& vm, PropertyKey const& property_key) const
|
2021-06-26 19:17:28 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. Assert: IsPropertyKey(P) is true.
|
2022-02-06 15:59:04 +00:00
|
|
|
|
VERIFY(property_key.is_valid());
|
2021-06-26 19:17:28 +01:00
|
|
|
|
|
|
|
|
|
// 2. Let O be ? ToObject(V).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto* object = TRY(to_object(vm));
|
2021-06-26 19:17:28 +01:00
|
|
|
|
|
|
|
|
|
// 3. Return ? O.[[Get]](P, V).
|
2022-02-06 15:59:04 +00:00
|
|
|
|
return TRY(object->internal_get(property_key, *this));
|
2021-06-26 19:17:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 08:27:04 -05:00
|
|
|
|
// 7.3.11 GetMethod ( V, P ), https://tc39.es/ecma262/#sec-getmethod
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<FunctionObject*> Value::get_method(VM& vm, PropertyKey const& property_key) const
|
2021-06-26 19:24:35 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. Assert: IsPropertyKey(P) is true.
|
2022-02-06 15:59:04 +00:00
|
|
|
|
VERIFY(property_key.is_valid());
|
2021-06-26 19:24:35 +01:00
|
|
|
|
|
|
|
|
|
// 2. Let func be ? GetV(V, P).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto function = TRY(get(vm, property_key));
|
2021-06-26 19:24:35 +01:00
|
|
|
|
|
|
|
|
|
// 3. If func is either undefined or null, return undefined.
|
|
|
|
|
if (function.is_nullish())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
// 4. If IsCallable(func) is false, throw a TypeError exception.
|
2021-09-23 21:24:51 +03:00
|
|
|
|
if (!function.is_function())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, function.to_string_without_side_effects());
|
2021-06-26 19:24:35 +01:00
|
|
|
|
|
|
|
|
|
// 5. Return func.
|
|
|
|
|
return &function.as_function();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
|
2022-12-10 00:04:07 +00:00
|
|
|
|
// RelationalExpression : RelationalExpression > ShiftExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> greater_than(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:04:07 +00:00
|
|
|
|
// 1. Let lref be ? Evaluation of RelationalExpression.
|
|
|
|
|
// 2. Let lval be ? GetValue(lref).
|
|
|
|
|
// 3. Let rref be ? Evaluation of ShiftExpression.
|
|
|
|
|
// 4. Let rval be ? GetValue(rref).
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: If both values are i32, we can do a direct comparison without calling into IsLessThan.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (lhs.is_int32() && rhs.is_int32())
|
2021-10-25 14:34:22 +02:00
|
|
|
|
return lhs.as_i32() > rhs.as_i32();
|
|
|
|
|
|
2022-12-10 00:04:07 +00:00
|
|
|
|
// 5. Let r be ? IsLessThan(rval, lval, false).
|
|
|
|
|
auto relation = TRY(is_less_than(vm, lhs, rhs, false));
|
|
|
|
|
|
|
|
|
|
// 6. If r is undefined, return false. Otherwise, return r.
|
2020-05-28 17:19:59 +02:00
|
|
|
|
if (relation == TriState::Unknown)
|
|
|
|
|
return Value(false);
|
|
|
|
|
return Value(relation == TriState::True);
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
|
2022-12-10 00:04:18 +00:00
|
|
|
|
// RelationalExpression : RelationalExpression >= ShiftExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> greater_than_equals(VM& vm, Value lhs, Value rhs)
|
2020-03-12 23:07:08 +11:00
|
|
|
|
{
|
2022-12-10 00:04:18 +00:00
|
|
|
|
// 1. Let lref be ? Evaluation of RelationalExpression.
|
|
|
|
|
// 2. Let lval be ? GetValue(lref).
|
|
|
|
|
// 3. Let rref be ? Evaluation of ShiftExpression.
|
|
|
|
|
// 4. Let rval be ? GetValue(rref).
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: If both values are i32, we can do a direct comparison without calling into IsLessThan.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (lhs.is_int32() && rhs.is_int32())
|
2021-10-25 14:34:22 +02:00
|
|
|
|
return lhs.as_i32() >= rhs.as_i32();
|
|
|
|
|
|
2022-12-10 00:04:18 +00:00
|
|
|
|
// 5. Let r be ? IsLessThan(lval, rval, true).
|
|
|
|
|
auto relation = TRY(is_less_than(vm, lhs, rhs, true));
|
|
|
|
|
|
|
|
|
|
// 6. If r is true or undefined, return false. Otherwise, return true.
|
2020-05-28 17:19:59 +02:00
|
|
|
|
if (relation == TriState::Unknown || relation == TriState::True)
|
|
|
|
|
return Value(false);
|
|
|
|
|
return Value(true);
|
2020-03-12 23:07:08 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
|
2022-12-10 00:04:35 +00:00
|
|
|
|
// RelationalExpression : RelationalExpression < ShiftExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> less_than(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:04:35 +00:00
|
|
|
|
// 1. Let lref be ? Evaluation of RelationalExpression.
|
|
|
|
|
// 2. Let lval be ? GetValue(lref).
|
|
|
|
|
// 3. Let rref be ? Evaluation of ShiftExpression.
|
|
|
|
|
// 4. Let rval be ? GetValue(rref).
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: If both values are i32, we can do a direct comparison without calling into IsLessThan.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (lhs.is_int32() && rhs.is_int32())
|
2021-10-25 14:34:22 +02:00
|
|
|
|
return lhs.as_i32() < rhs.as_i32();
|
|
|
|
|
|
2022-12-10 00:04:35 +00:00
|
|
|
|
// 5. Let r be ? IsLessThan(lval, rval, true).
|
|
|
|
|
auto relation = TRY(is_less_than(vm, lhs, rhs, true));
|
|
|
|
|
|
|
|
|
|
// 6. If r is undefined, return false. Otherwise, return r.
|
2020-05-28 17:19:59 +02:00
|
|
|
|
if (relation == TriState::Unknown)
|
|
|
|
|
return Value(false);
|
|
|
|
|
return Value(relation == TriState::True);
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.10 Relational Operators, https://tc39.es/ecma262/#sec-relational-operators
|
2022-12-10 00:04:46 +00:00
|
|
|
|
// RelationalExpression : RelationalExpression <= ShiftExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> less_than_equals(VM& vm, Value lhs, Value rhs)
|
2020-03-12 23:07:08 +11:00
|
|
|
|
{
|
2022-12-10 00:04:46 +00:00
|
|
|
|
// 1. Let lref be ? Evaluation of RelationalExpression.
|
|
|
|
|
// 2. Let lval be ? GetValue(lref).
|
|
|
|
|
// 3. Let rref be ? Evaluation of ShiftExpression.
|
|
|
|
|
// 4. Let rval be ? GetValue(rref).
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: If both values are i32, we can do a direct comparison without calling into IsLessThan.
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (lhs.is_int32() && rhs.is_int32())
|
2021-10-25 14:34:22 +02:00
|
|
|
|
return lhs.as_i32() <= rhs.as_i32();
|
|
|
|
|
|
2022-12-10 00:04:46 +00:00
|
|
|
|
// 5. Let r be ? IsLessThan(rval, lval, false).
|
|
|
|
|
auto relation = TRY(is_less_than(vm, lhs, rhs, false));
|
|
|
|
|
|
|
|
|
|
// 6. If r is true or undefined, return false. Otherwise, return true.
|
|
|
|
|
if (relation == TriState::True || relation == TriState::Unknown)
|
2020-05-28 17:19:59 +02:00
|
|
|
|
return Value(false);
|
|
|
|
|
return Value(true);
|
2020-03-12 23:07:08 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
|
2022-12-10 00:05:02 +00:00
|
|
|
|
// BitwiseANDExpression : BitwiseANDExpression & EqualityExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> bitwise_and(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:05:02 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:05:02 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:05:02 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-07-22 20:27:32 -04:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:05:02 +00:00
|
|
|
|
// 6.1.6.1.17 Number::bitwiseAND ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-bitwiseAND
|
|
|
|
|
// 1. Return NumberBitwiseOp(&, x, y).
|
2020-07-22 20:27:32 -04:00
|
|
|
|
if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
|
|
|
|
|
return Value(0);
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return Value(TRY(lhs_numeric.to_i32(vm)) & TRY(rhs_numeric.to_i32(vm)));
|
2020-07-22 20:27:32 -04:00
|
|
|
|
}
|
2022-12-10 00:05:02 +00:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.2.18 BigInt::bitwiseAND ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-bitwiseAND
|
|
|
|
|
// 1. Return BigIntBitwiseOp(&, x, y).
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
|
2022-12-10 00:05:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
|
2022-12-10 00:05:22 +00:00
|
|
|
|
// BitwiseORExpression : BitwiseORExpression | BitwiseXORExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> bitwise_or(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:05:22 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:05:22 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:05:22 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:05:22 +00:00
|
|
|
|
// 6.1.6.1.19 Number::bitwiseOR ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-bitwiseOR
|
|
|
|
|
// 1. Return NumberBitwiseOp(|, x, y).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
|
|
|
|
|
return Value(0);
|
|
|
|
|
if (!lhs_numeric.is_finite_number())
|
|
|
|
|
return rhs_numeric;
|
|
|
|
|
if (!rhs_numeric.is_finite_number())
|
|
|
|
|
return lhs_numeric;
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return Value(TRY(lhs_numeric.to_i32(vm)) | TRY(rhs_numeric.to_i32(vm)));
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2022-12-10 00:05:22 +00:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.2.20 BigInt::bitwiseOR ( x, y )
|
|
|
|
|
// 1. Return BigIntBitwiseOp(|, x, y).
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
|
2022-12-10 00:05:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.12 Binary Bitwise Operators, https://tc39.es/ecma262/#sec-binary-bitwise-operators
|
2022-12-10 00:05:36 +00:00
|
|
|
|
// BitwiseXORExpression : BitwiseXORExpression ^ BitwiseANDExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> bitwise_xor(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:05:36 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:05:36 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:05:36 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-07-22 20:27:32 -04:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:05:36 +00:00
|
|
|
|
// 6.1.6.1.18 Number::bitwiseXOR ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-bitwiseXOR
|
|
|
|
|
// 1. Return NumberBitwiseOp(^, x, y).
|
2020-07-22 20:27:32 -04:00
|
|
|
|
if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
|
|
|
|
|
return Value(0);
|
|
|
|
|
if (!lhs_numeric.is_finite_number())
|
|
|
|
|
return rhs_numeric;
|
|
|
|
|
if (!rhs_numeric.is_finite_number())
|
|
|
|
|
return lhs_numeric;
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return Value(TRY(lhs_numeric.to_i32(vm)) ^ TRY(rhs_numeric.to_i32(vm)));
|
2020-07-22 20:27:32 -04:00
|
|
|
|
}
|
2022-12-10 00:05:36 +00:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.2.19 BigInt::bitwiseXOR ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-bitwiseXOR
|
|
|
|
|
// 1. Return BigIntBitwiseOp(^, x, y).
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
|
2022-12-10 00:05:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.5.6 Bitwise NOT Operator ( ~ ), https://tc39.es/ecma262/#sec-bitwise-not-operator
|
2022-12-10 00:05:46 +00:00
|
|
|
|
// UnaryExpression : ~ UnaryExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> bitwise_not(VM& vm, Value lhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:05:46 +00:00
|
|
|
|
// 1. Let expr be ? Evaluation of UnaryExpression.
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// 2. Let oldValue be ? ToNumeric(? GetValue(expr)).
|
|
|
|
|
|
|
|
|
|
auto old_value = TRY(lhs.to_numeric(vm));
|
|
|
|
|
|
|
|
|
|
// 3. If oldValue is a Number, then
|
|
|
|
|
if (old_value.is_number()) {
|
|
|
|
|
// a. Return Number::bitwiseNOT(oldValue).
|
|
|
|
|
|
|
|
|
|
// 6.1.6.1.2 Number::bitwiseNOT ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-bitwiseNOT
|
|
|
|
|
// 1. Let oldValue be ! ToInt32(x).
|
|
|
|
|
// 2. Return the result of applying bitwise complement to oldValue. The mathematical value of the result is
|
|
|
|
|
// exactly representable as a 32-bit two's complement bit string.
|
|
|
|
|
return Value(~TRY(old_value.to_i32(vm)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Else,
|
|
|
|
|
// a. Assert: oldValue is a BigInt.
|
|
|
|
|
VERIFY(old_value.is_bigint());
|
|
|
|
|
|
|
|
|
|
// b. Return BigInt::bitwiseNOT(oldValue).
|
|
|
|
|
|
|
|
|
|
// 6.1.6.2.2 BigInt::bitwiseNOT ( x ), https://tc39.es/ecma262/#sec-numeric-types-bigint-bitwiseNOT
|
|
|
|
|
// 1. Return -x - 1ℤ.
|
|
|
|
|
return BigInt::create(vm, old_value.as_bigint().big_integer().bitwise_not());
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
|
2022-12-10 00:05:57 +00:00
|
|
|
|
// UnaryExpression : + UnaryExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> unary_plus(VM& vm, Value lhs)
|
2020-04-02 17:58:39 +01:00
|
|
|
|
{
|
2022-12-10 00:05:57 +00:00
|
|
|
|
// 1. Let expr be ? Evaluation of UnaryExpression.
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// 2. Return ? ToNumber(? GetValue(expr)).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return TRY(lhs.to_number(vm));
|
2020-04-02 17:58:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
|
2022-12-10 00:06:16 +00:00
|
|
|
|
// UnaryExpression : - UnaryExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> unary_minus(VM& vm, Value lhs)
|
2020-04-02 17:58:39 +01:00
|
|
|
|
{
|
2022-12-10 00:06:16 +00:00
|
|
|
|
// 1. Let expr be ? Evaluation of UnaryExpression.
|
|
|
|
|
// NOTE: This is handled in the AST or Bytecode interpreter.
|
|
|
|
|
|
|
|
|
|
// 2. Let oldValue be ? ToNumeric(? GetValue(expr)).
|
|
|
|
|
auto old_value = TRY(lhs.to_numeric(vm));
|
|
|
|
|
|
|
|
|
|
// 3. If oldValue is a Number, then
|
|
|
|
|
if (old_value.is_number()) {
|
|
|
|
|
// a. Return Number::unaryMinus(oldValue).
|
|
|
|
|
|
|
|
|
|
// 6.1.6.1.1 Number::unaryMinus ( x ), https://tc39.es/ecma262/#sec-numeric-types-number-unaryMinus
|
|
|
|
|
// 1. If x is NaN, return NaN.
|
|
|
|
|
if (old_value.is_nan())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return js_nan();
|
2022-12-10 00:06:16 +00:00
|
|
|
|
|
|
|
|
|
// 2. Return the result of negating x; that is, compute a Number with the same magnitude but opposite sign.
|
|
|
|
|
return Value(-old_value.as_double());
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2022-12-10 00:06:16 +00:00
|
|
|
|
|
|
|
|
|
// 4. Else,
|
|
|
|
|
// a. Assert: oldValue is a BigInt.
|
|
|
|
|
VERIFY(old_value.is_bigint());
|
|
|
|
|
|
|
|
|
|
// b. Return BigInt::unaryMinus(oldValue).
|
|
|
|
|
|
|
|
|
|
// 6.1.6.2.1 BigInt::unaryMinus ( x ), https://tc39.es/ecma262/#sec-numeric-types-bigint-unaryMinus
|
|
|
|
|
// 1. If x is 0ℤ, return 0ℤ.
|
|
|
|
|
if (old_value.as_bigint().big_integer() == BIGINT_ZERO)
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, BIGINT_ZERO);
|
2022-12-10 00:06:16 +00:00
|
|
|
|
|
|
|
|
|
// 2. Return the BigInt value that represents the negation of ℝ(x).
|
|
|
|
|
auto big_integer_negated = old_value.as_bigint().big_integer();
|
2020-06-06 01:14:10 +01:00
|
|
|
|
big_integer_negated.negate();
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, big_integer_negated);
|
2020-04-02 17:58:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.9.1 The Left Shift Operator ( << ), https://tc39.es/ecma262/#sec-left-shift-operator
|
2022-12-10 00:06:37 +00:00
|
|
|
|
// ShiftExpression : ShiftExpression << AdditiveExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> left_shift(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:06:37 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:06:37 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:06:37 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:06:37 +00:00
|
|
|
|
// 6.1.6.1.9 Number::leftShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-leftShift
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: Handle infinite values according to the results returned by ToInt32/ToUint32.
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (!lhs_numeric.is_finite_number())
|
|
|
|
|
return Value(0);
|
|
|
|
|
if (!rhs_numeric.is_finite_number())
|
|
|
|
|
return lhs_numeric;
|
2022-12-10 00:06:37 +00:00
|
|
|
|
|
|
|
|
|
// 1. Let lnum be ! ToInt32(x).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_i32 = MUST(lhs_numeric.to_i32(vm));
|
2022-12-10 00:06:37 +00:00
|
|
|
|
|
|
|
|
|
// 2. Let rnum be ! ToUint32(y).
|
|
|
|
|
auto rhs_u32 = MUST(rhs_numeric.to_u32(vm));
|
|
|
|
|
|
|
|
|
|
// 3. Let shiftCount be ℝ(rnum) modulo 32.
|
|
|
|
|
auto shift_count = rhs_u32 % 32;
|
|
|
|
|
|
|
|
|
|
// 4. Return the result of left shifting lnum by shiftCount bits. The mathematical value of the result is
|
|
|
|
|
// exactly representable as a 32-bit two's complement bit string.
|
|
|
|
|
return Value(lhs_i32 << shift_count);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2021-05-31 20:27:44 +03:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
2022-08-20 00:23:11 +02:00
|
|
|
|
// 6.1.6.2.9 BigInt::leftShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-leftShift
|
2021-05-31 20:27:44 +03:00
|
|
|
|
auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
|
2022-08-20 00:23:11 +02:00
|
|
|
|
|
|
|
|
|
// 1. If y < 0ℤ, then
|
|
|
|
|
if (rhs_numeric.as_bigint().big_integer().is_negative()) {
|
|
|
|
|
// a. Return the BigInt value that represents ℝ(x) / 2^-y, rounding down to the nearest integer, including for negative numbers.
|
|
|
|
|
// NOTE: Since y is negative we can just do ℝ(x) / 2^|y|
|
|
|
|
|
auto const& big_integer = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto division_result = big_integer.divided_by(multiplier_divisor);
|
|
|
|
|
|
|
|
|
|
// For positive initial values and no remainder just return quotient
|
|
|
|
|
if (division_result.remainder.is_zero() || !big_integer.is_negative())
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, division_result.quotient);
|
2022-08-20 00:23:11 +02:00
|
|
|
|
// For negative round "down" to the next negative number
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return BigInt::create(vm, division_result.quotient.minus(Crypto::SignedBigInteger { 1 }));
|
2022-08-20 00:23:11 +02:00
|
|
|
|
}
|
|
|
|
|
// 2. Return the BigInt value that represents ℝ(x) × 2^y.
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return Value(BigInt::create(vm, lhs_numeric.as_bigint().big_integer().multiplied_by(multiplier_divisor)));
|
2021-05-31 20:27:44 +03:00
|
|
|
|
}
|
2022-12-10 00:06:37 +00:00
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "left-shift");
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.9.2 The Signed Right Shift Operator ( >> ), https://tc39.es/ecma262/#sec-signed-right-shift-operator
|
2022-12-10 00:07:00 +00:00
|
|
|
|
// ShiftExpression : ShiftExpression >> AdditiveExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> right_shift(VM& vm, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
|
{
|
2022-12-10 00:07:00 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:07:00 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:07:00 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:07:00 +00:00
|
|
|
|
// 6.1.6.1.10 Number::signedRightShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-signedRightShift
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: Handle infinite values according to the results returned by ToInt32/ToUint32.
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (!lhs_numeric.is_finite_number())
|
|
|
|
|
return Value(0);
|
|
|
|
|
if (!rhs_numeric.is_finite_number())
|
|
|
|
|
return lhs_numeric;
|
2022-12-10 00:07:00 +00:00
|
|
|
|
|
|
|
|
|
// 1. Let lnum be ! ToInt32(x).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_i32 = MUST(lhs_numeric.to_i32(vm));
|
2022-12-10 00:07:00 +00:00
|
|
|
|
|
|
|
|
|
// 2. Let rnum be ! ToUint32(y).
|
|
|
|
|
auto rhs_u32 = MUST(rhs_numeric.to_u32(vm));
|
|
|
|
|
|
|
|
|
|
// 3. Let shiftCount be ℝ(rnum) modulo 32.
|
|
|
|
|
auto shift_count = rhs_u32 % 32;
|
|
|
|
|
|
|
|
|
|
// 4. Return the result of performing a sign-extending right shift of lnum by shiftCount bits.
|
|
|
|
|
// The most significant bit is propagated. The mathematical value of the result is exactly representable
|
|
|
|
|
// as a 32-bit two's complement bit string.
|
|
|
|
|
return Value(lhs_i32 >> shift_count);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2021-05-31 20:27:44 +03:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:07:00 +00:00
|
|
|
|
// 6.1.6.2.10 BigInt::signedRightShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-signedRightShift
|
|
|
|
|
// 1. Return BigInt::leftShift(x, -y).
|
2021-05-31 20:27:44 +03:00
|
|
|
|
auto rhs_negated = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
rhs_negated.negate();
|
2022-12-06 22:03:52 +00:00
|
|
|
|
return left_shift(vm, lhs, BigInt::create(vm, rhs_negated));
|
2021-05-31 20:27:44 +03:00
|
|
|
|
}
|
2022-12-10 00:07:00 +00:00
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "right-shift");
|
2020-03-10 11:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.9.3 The Unsigned Right Shift Operator ( >>> ), https://tc39.es/ecma262/#sec-unsigned-right-shift-operator
|
2022-12-10 00:07:17 +00:00
|
|
|
|
// ShiftExpression : ShiftExpression >>> AdditiveExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> unsigned_right_shift(VM& vm, Value lhs, Value rhs)
|
2020-04-23 15:43:10 +01:00
|
|
|
|
{
|
2022-12-10 00:07:17 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 5-6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:07:17 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:07:17 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:07:17 +00:00
|
|
|
|
// 6.1.6.1.11 Number::unsignedRightShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-unsignedRightShift
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: Handle infinite values according to the results returned by ToUint32.
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (!lhs_numeric.is_finite_number())
|
|
|
|
|
return Value(0);
|
|
|
|
|
if (!rhs_numeric.is_finite_number())
|
|
|
|
|
return lhs_numeric;
|
2022-12-10 00:07:17 +00:00
|
|
|
|
|
|
|
|
|
// 1. Let lnum be ! ToUint32(x).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_u32 = MUST(lhs_numeric.to_u32(vm));
|
2022-12-10 00:07:17 +00:00
|
|
|
|
|
|
|
|
|
// 2. Let rnum be ! ToUint32(y).
|
|
|
|
|
auto rhs_u32 = MUST(rhs_numeric.to_u32(vm));
|
|
|
|
|
|
|
|
|
|
// 3. Let shiftCount be ℝ(rnum) modulo 32.
|
|
|
|
|
auto shift_count = rhs_u32 % 32;
|
|
|
|
|
|
|
|
|
|
// 4. Return the result of performing a zero-filling right shift of lnum by shiftCount bits.
|
|
|
|
|
// Vacated bits are filled with zero. The mathematical value of the result is exactly representable
|
|
|
|
|
// as a 32-bit unsigned bit string.
|
|
|
|
|
return Value(lhs_u32 >> shift_count);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2022-12-10 00:07:17 +00:00
|
|
|
|
|
|
|
|
|
// 6. If lnum is a BigInt, then
|
|
|
|
|
// d. If opText is >>>, return ? BigInt::unsignedRightShift(lnum, rnum).
|
|
|
|
|
|
|
|
|
|
// 6.1.6.2.11 BigInt::unsignedRightShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-unsignedRightShift
|
|
|
|
|
// 1. Throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperator, "unsigned right-shift");
|
2020-04-23 15:43:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.8.1 The Addition Operator ( + ), https://tc39.es/ecma262/#sec-addition-operator-plus
|
2022-12-10 00:07:37 +00:00
|
|
|
|
// AdditiveExpression : AdditiveExpression + MultiplicativeExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> add(VM& vm, Value lhs, Value rhs)
|
2020-03-11 12:16:26 +01:00
|
|
|
|
{
|
2022-12-10 00:07:37 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
|
|
|
|
|
// 1. If opText is +, then
|
|
|
|
|
|
|
|
|
|
// OPTIMIZATION: If both values are i32 or double, we can do a direct addition without the type conversions below.
|
2021-03-21 18:13:14 +01:00
|
|
|
|
if (both_number(lhs, rhs)) {
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (lhs.is_int32() && rhs.is_int32()) {
|
2021-03-21 18:13:14 +01:00
|
|
|
|
Checked<i32> result;
|
2022-08-21 14:00:56 +01:00
|
|
|
|
result = MUST(lhs.to_i32(vm));
|
|
|
|
|
result += MUST(rhs.to_i32(vm));
|
2021-03-21 18:13:14 +01:00
|
|
|
|
if (!result.has_overflow())
|
|
|
|
|
return Value(result.value());
|
|
|
|
|
}
|
|
|
|
|
return Value(lhs.as_double() + rhs.as_double());
|
|
|
|
|
}
|
2022-12-10 00:07:37 +00:00
|
|
|
|
|
|
|
|
|
// a. Let lprim be ? ToPrimitive(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_primitive = TRY(lhs.to_primitive(vm));
|
2022-12-10 00:07:37 +00:00
|
|
|
|
|
|
|
|
|
// b. Let rprim be ? ToPrimitive(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_primitive = TRY(rhs.to_primitive(vm));
|
2020-04-15 09:32:44 +02:00
|
|
|
|
|
2022-12-10 00:07:37 +00:00
|
|
|
|
// c. If lprim is a String or rprim is a String, then
|
2020-05-15 13:39:24 +02:00
|
|
|
|
if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
|
2022-12-10 00:07:37 +00:00
|
|
|
|
// i. Let lstr be ? ToString(lprim).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_string = TRY(lhs_primitive.to_primitive_string(vm));
|
2022-12-10 00:07:37 +00:00
|
|
|
|
|
|
|
|
|
// ii. Let rstr be ? ToString(rprim).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_string = TRY(rhs_primitive.to_primitive_string(vm));
|
2022-12-10 00:07:37 +00:00
|
|
|
|
|
|
|
|
|
// iii. Return the string-concatenation of lstr and rstr.
|
2022-12-06 22:17:27 +00:00
|
|
|
|
return PrimitiveString::create(vm, *lhs_string, *rhs_string);
|
2020-05-15 13:39:24 +02:00
|
|
|
|
}
|
2020-03-16 00:19:41 +02:00
|
|
|
|
|
2022-12-10 00:07:37 +00:00
|
|
|
|
// d. Set lval to lprim.
|
|
|
|
|
// e. Set rval to rprim.
|
|
|
|
|
|
|
|
|
|
// 2. NOTE: At this point, it must be a numeric operation.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs_primitive.to_numeric(vm));
|
2022-12-10 00:07:37 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs_primitive.to_numeric(vm));
|
2022-12-10 00:07:37 +00:00
|
|
|
|
|
|
|
|
|
// 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.1.7 Number::add ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-add
|
|
|
|
|
auto x = lhs_numeric.as_double();
|
|
|
|
|
auto y = rhs_numeric.as_double();
|
|
|
|
|
return Value(x + y);
|
|
|
|
|
}
|
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.2.7 BigInt::add ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-add
|
|
|
|
|
auto x = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto y = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
return BigInt::create(vm, x.plus(y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "addition");
|
2020-03-11 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.8.2 The Subtraction Operator ( - ), https://tc39.es/ecma262/#sec-subtraction-operator-minus
|
2022-12-10 00:07:53 +00:00
|
|
|
|
// AdditiveExpression : AdditiveExpression - MultiplicativeExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> sub(VM& vm, Value lhs, Value rhs)
|
2020-03-11 12:16:26 +01:00
|
|
|
|
{
|
2022-12-10 00:07:53 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:07:53 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:07:53 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:07:53 +00:00
|
|
|
|
// 6.1.6.1.8 Number::subtract ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-subtract
|
|
|
|
|
auto x = lhs_numeric.as_double();
|
|
|
|
|
auto y = rhs_numeric.as_double();
|
|
|
|
|
// 1. Return Number::add(x, Number::unaryMinus(y)).
|
|
|
|
|
return Value(x - y);
|
2022-02-25 01:26:52 +01:00
|
|
|
|
}
|
2022-12-10 00:07:53 +00:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.2.8 BigInt::subtract ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-subtract
|
|
|
|
|
auto x = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto y = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
// 1. Return the BigInt value that represents the difference x minus y.
|
|
|
|
|
return BigInt::create(vm, x.minus(y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "subtraction");
|
2020-03-11 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
|
2022-12-10 00:08:06 +00:00
|
|
|
|
// MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> mul(VM& vm, Value lhs, Value rhs)
|
2020-03-12 23:04:52 +11:00
|
|
|
|
{
|
2022-12-10 00:08:06 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:08:06 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:08:06 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.1.4 Number::multiply ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-multiply
|
|
|
|
|
auto x = lhs_numeric.as_double();
|
|
|
|
|
auto y = rhs_numeric.as_double();
|
|
|
|
|
return Value(x * y);
|
|
|
|
|
}
|
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.2.4 BigInt::multiply ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-multiply
|
|
|
|
|
auto x = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto y = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
// 1. Return the BigInt value that represents the product of x and y.
|
|
|
|
|
return BigInt::create(vm, x.multiplied_by(y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "multiplication");
|
2020-03-12 23:04:52 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
|
2022-12-10 00:08:19 +00:00
|
|
|
|
// MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> div(VM& vm, Value lhs, Value rhs)
|
2020-03-12 23:04:52 +11:00
|
|
|
|
{
|
2022-12-10 00:08:19 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:08:19 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:08:19 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
|
|
|
|
// 6.1.6.1.5 Number::divide ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-number-divide
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
|
2022-12-10 00:08:19 +00:00
|
|
|
|
}
|
2021-03-16 20:34:40 +01:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:08:19 +00:00
|
|
|
|
// 6.1.6.2.5 BigInt::divide ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-divide
|
|
|
|
|
auto x = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto y = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
// 1. If y is 0ℤ, throw a RangeError exception.
|
|
|
|
|
if (y == BIGINT_ZERO)
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::DivisionByZero);
|
2022-12-10 00:08:19 +00:00
|
|
|
|
// 2. Let quotient be ℝ(x) / ℝ(y).
|
|
|
|
|
// 3. Return the BigInt value that represents quotient rounded towards 0 to the next integer value.
|
|
|
|
|
return BigInt::create(vm, x.divided_by(y).quotient);
|
2021-03-16 20:34:40 +01:00
|
|
|
|
}
|
2022-12-10 00:08:19 +00:00
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "division");
|
2020-03-12 23:04:52 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.7 Multiplicative Operators, https://tc39.es/ecma262/#sec-multiplicative-operators
|
2022-12-10 00:08:36 +00:00
|
|
|
|
// MultiplicativeExpression : MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> mod(VM& vm, Value lhs, Value rhs)
|
2020-04-04 21:17:34 +02:00
|
|
|
|
{
|
2022-12-10 00:08:36 +00:00
|
|
|
|
// 13.15.3 ApplyStringOrNumericBinaryOperator ( lval, opText, rval ), https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator
|
|
|
|
|
// 1-2, 6. N/A.
|
|
|
|
|
|
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:08:36 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:08:36 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2021-09-01 16:50:56 +01:00
|
|
|
|
// 6.1.6.1.6 Number::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-number-remainder
|
2022-02-16 02:14:57 -08:00
|
|
|
|
// The ECMA specification is describing the mathematical definition of modulus
|
|
|
|
|
// implemented by fmod.
|
|
|
|
|
auto n = lhs_numeric.as_double();
|
|
|
|
|
auto d = rhs_numeric.as_double();
|
|
|
|
|
return Value(fmod(n, d));
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2021-03-16 20:34:40 +01:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:08:36 +00:00
|
|
|
|
// 6.1.6.2.6 BigInt::remainder ( n, d ), https://tc39.es/ecma262/#sec-numeric-types-bigint-remainder
|
|
|
|
|
auto n = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto d = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
// 1. If d is 0ℤ, throw a RangeError exception.
|
|
|
|
|
if (d == BIGINT_ZERO)
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::DivisionByZero);
|
2022-12-10 00:08:36 +00:00
|
|
|
|
// 2. If n is 0ℤ, return 0ℤ.
|
|
|
|
|
// 3. Let quotient be ℝ(n) / ℝ(d).
|
|
|
|
|
// 4. Let q be the BigInt whose sign is the sign of quotient and whose magnitude is floor(abs(quotient)).
|
|
|
|
|
// 5. Return n - (d × q).
|
|
|
|
|
return BigInt::create(vm, n.divided_by(d).remainder);
|
2021-03-16 20:34:40 +01:00
|
|
|
|
}
|
2022-12-10 00:08:36 +00:00
|
|
|
|
|
|
|
|
|
// 5. If Type(lnum) is different from Type(rnum), throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "modulo");
|
2020-04-04 21:17:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// 6.1.6.1.3 Number::exponentiate ( base, exponent ), https://tc39.es/ecma262/#sec-numeric-types-number-exponentiate
|
2022-02-17 11:52:45 -08:00
|
|
|
|
static Value exp_double(Value base, Value exponent)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(both_number(base, exponent));
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 1. If exponent is NaN, return NaN.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (exponent.is_nan())
|
|
|
|
|
return js_nan();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 2. If exponent is +0𝔽 or exponent is -0𝔽, return 1𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (exponent.is_positive_zero() || exponent.is_negative_zero())
|
|
|
|
|
return Value(1);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 3. If base is NaN, return NaN.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (base.is_nan())
|
|
|
|
|
return js_nan();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 4. If base is +∞𝔽, then
|
|
|
|
|
if (base.is_positive_infinity()) {
|
|
|
|
|
// a. If exponent > +0𝔽, return +∞𝔽. Otherwise, return +0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return exponent.as_double() > 0 ? js_infinity() : Value(0);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. If base is -∞𝔽, then
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (base.is_negative_infinity()) {
|
2022-02-25 01:26:52 +01:00
|
|
|
|
auto is_odd_integral_number = exponent.is_integral_number() && (static_cast<i32>(exponent.as_double()) % 2 != 0);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// a. If exponent > +0𝔽, then
|
|
|
|
|
if (exponent.as_double() > 0) {
|
|
|
|
|
// i. If exponent is an odd integral Number, return -∞𝔽. Otherwise, return +∞𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
|
|
|
|
// b. Else,
|
|
|
|
|
else {
|
|
|
|
|
// i. If exponent is an odd integral Number, return -0𝔽. Otherwise, return +0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return is_odd_integral_number ? Value(-0.0) : Value(0);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
2022-02-17 11:52:45 -08:00
|
|
|
|
}
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 6. If base is +0𝔽, then
|
|
|
|
|
if (base.is_positive_zero()) {
|
|
|
|
|
// a. If exponent > +0𝔽, return +0𝔽. Otherwise, return +∞𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return exponent.as_double() > 0 ? Value(0) : js_infinity();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7. If base is -0𝔽, then
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (base.is_negative_zero()) {
|
2022-02-25 01:26:52 +01:00
|
|
|
|
auto is_odd_integral_number = exponent.is_integral_number() && (static_cast<i32>(exponent.as_double()) % 2 != 0);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// a. If exponent > +0𝔽, then
|
|
|
|
|
if (exponent.as_double() > 0) {
|
|
|
|
|
// i. If exponent is an odd integral Number, return -0𝔽. Otherwise, return +0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return is_odd_integral_number ? Value(-0.0) : Value(0);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
|
|
|
|
// b. Else,
|
|
|
|
|
else {
|
|
|
|
|
// i. If exponent is an odd integral Number, return -∞𝔽. Otherwise, return +∞𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
2022-02-17 11:52:45 -08:00
|
|
|
|
}
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 8. Assert: base is finite and is neither +0𝔽 nor -0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 9. If exponent is +∞𝔽, then
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (exponent.is_positive_infinity()) {
|
|
|
|
|
auto absolute_base = fabs(base.as_double());
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// a. If abs(ℝ(base)) > 1, return +∞𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (absolute_base > 1)
|
|
|
|
|
return js_infinity();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// b. If abs(ℝ(base)) is 1, return NaN.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
else if (absolute_base == 1)
|
|
|
|
|
return js_nan();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// c. If abs(ℝ(base)) < 1, return +0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
else if (absolute_base < 1)
|
|
|
|
|
return Value(0);
|
|
|
|
|
}
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 10. If exponent is -∞𝔽, then
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (exponent.is_negative_infinity()) {
|
|
|
|
|
auto absolute_base = fabs(base.as_double());
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// a. If abs(ℝ(base)) > 1, return +0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (absolute_base > 1)
|
|
|
|
|
return Value(0);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// b. If abs(ℝ(base)) is 1, return NaN.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
else if (absolute_base == 1)
|
|
|
|
|
return js_nan();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// a. If abs(ℝ(base)) > 1, return +0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
else if (absolute_base < 1)
|
|
|
|
|
return js_infinity();
|
|
|
|
|
}
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 11. Assert: exponent is finite and is neither +0𝔽 nor -0𝔽.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 12. If base < -0𝔽 and exponent is not an integral Number, return NaN.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
if (base.as_double() < 0 && !exponent.is_integral_number())
|
|
|
|
|
return js_nan();
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 13. Return an implementation-approximated Number value representing the result of raising ℝ(base) to the ℝ(exponent) power.
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return Value(::pow(base.as_double(), exponent.as_double()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// ExponentiationExpression : UpdateExpression ** ExponentiationExpression
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> exp(VM& vm, Value lhs, Value rhs)
|
2020-04-05 13:40:00 +01:00
|
|
|
|
{
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// 3. Let lnum be ? ToNumeric(lval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_numeric = TRY(lhs.to_numeric(vm));
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 4. Let rnum be ? ToNumeric(rval).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_numeric = TRY(rhs.to_numeric(vm));
|
2022-12-10 00:09:31 +00:00
|
|
|
|
|
|
|
|
|
// 7. Let operation be the abstract operation associated with opText and Type(lnum) in the following table:
|
|
|
|
|
// [...]
|
|
|
|
|
// 8. Return operation(lnum, rnum).
|
|
|
|
|
if (both_number(lhs_numeric, rhs_numeric)) {
|
2022-02-17 11:52:45 -08:00
|
|
|
|
return exp_double(lhs_numeric, rhs_numeric);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
}
|
2021-03-16 20:35:55 +01:00
|
|
|
|
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// 6.1.6.2.3 BigInt::exponentiate ( base, exponent ), https://tc39.es/ecma262/#sec-numeric-types-bigint-exponentiate
|
|
|
|
|
auto base = lhs_numeric.as_bigint().big_integer();
|
|
|
|
|
auto exponent = rhs_numeric.as_bigint().big_integer();
|
|
|
|
|
// 1. If exponent < 0ℤ, throw a RangeError exception.
|
|
|
|
|
if (exponent.is_negative())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::NegativeExponent);
|
2022-12-10 00:09:31 +00:00
|
|
|
|
// 2. If base is 0ℤ and exponent is 0ℤ, return 1ℤ.
|
|
|
|
|
// 3. Return the BigInt value that represents ℝ(base) raised to the power ℝ(exponent).
|
|
|
|
|
return BigInt::create(vm, Crypto::NumberTheory::Power(base, exponent));
|
2021-03-16 20:35:55 +01:00
|
|
|
|
}
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::BigIntBadOperatorOtherType, "exponentiation");
|
2020-04-05 13:40:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> in(VM& vm, Value lhs, Value rhs)
|
2020-05-07 17:09:00 -07:00
|
|
|
|
{
|
2021-10-19 00:13:29 +03:00
|
|
|
|
if (!rhs.is_object())
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::InOperatorWithObject);
|
|
|
|
|
auto lhs_property_key = TRY(lhs.to_property_key(vm));
|
2021-10-19 00:13:29 +03:00
|
|
|
|
return Value(TRY(rhs.as_object().has_property(lhs_property_key)));
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 13.10.2 InstanceofOperator ( V, target ), https://tc39.es/ecma262/#sec-instanceofoperator
|
2022-12-10 00:09:44 +00:00
|
|
|
|
ThrowCompletionOr<Value> instance_of(VM& vm, Value value, Value target)
|
2020-03-11 12:16:26 +01:00
|
|
|
|
{
|
2022-12-10 00:09:44 +00:00
|
|
|
|
// 1. If target is not an Object, throw a TypeError exception.
|
|
|
|
|
if (!target.is_object())
|
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::NotAnObject, target.to_string_without_side_effects());
|
|
|
|
|
|
|
|
|
|
// 2. Let instOfHandler be ? GetMethod(target, @@hasInstance).
|
|
|
|
|
auto* instance_of_handler = TRY(target.get_method(vm, *vm.well_known_symbol_has_instance()));
|
|
|
|
|
|
|
|
|
|
// 3. If instOfHandler is not undefined, then
|
|
|
|
|
if (instance_of_handler) {
|
|
|
|
|
// a. Return ToBoolean(? Call(instOfHandler, target, « V »)).
|
|
|
|
|
return Value(TRY(call(vm, *instance_of_handler, target, value)).to_boolean());
|
2020-07-11 15:27:28 -07:00
|
|
|
|
}
|
2022-12-10 00:09:44 +00:00
|
|
|
|
|
|
|
|
|
// 4. If IsCallable(target) is false, throw a TypeError exception.
|
|
|
|
|
if (!target.is_function())
|
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, target.to_string_without_side_effects());
|
|
|
|
|
|
|
|
|
|
// 5. Return ? OrdinaryHasInstance(target, V).
|
|
|
|
|
return ordinary_has_instance(vm, target, value);
|
2020-07-11 15:27:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 08:27:04 -05:00
|
|
|
|
// 7.3.22 OrdinaryHasInstance ( C, O ), https://tc39.es/ecma262/#sec-ordinaryhasinstance
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> ordinary_has_instance(VM& vm, Value lhs, Value rhs)
|
2020-07-11 15:27:28 -07:00
|
|
|
|
{
|
2022-12-10 00:10:01 +00:00
|
|
|
|
// 1. If IsCallable(C) is false, return false.
|
2020-07-11 15:27:28 -07:00
|
|
|
|
if (!rhs.is_function())
|
|
|
|
|
return Value(false);
|
2022-12-10 00:10:01 +00:00
|
|
|
|
|
2020-07-11 15:27:28 -07:00
|
|
|
|
auto& rhs_function = rhs.as_function();
|
|
|
|
|
|
2022-12-10 00:10:01 +00:00
|
|
|
|
// 2. If C has a [[BoundTargetFunction]] internal slot, then
|
2021-01-01 17:46:39 +01:00
|
|
|
|
if (is<BoundFunction>(rhs_function)) {
|
2022-12-10 00:10:01 +00:00
|
|
|
|
auto const& bound_target = static_cast<BoundFunction const&>(rhs_function);
|
|
|
|
|
|
|
|
|
|
// a. Let BC be C.[[BoundTargetFunction]].
|
|
|
|
|
// b. Return ? InstanceofOperator(O, BC).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return instance_of(vm, lhs, Value(&bound_target.bound_target_function()));
|
2020-07-11 15:27:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 00:10:01 +00:00
|
|
|
|
// 3. If O is not an Object, return false.
|
2020-07-11 15:27:28 -07:00
|
|
|
|
if (!lhs.is_object())
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return Value(false);
|
2020-07-11 15:27:28 -07:00
|
|
|
|
|
2022-12-10 00:10:01 +00:00
|
|
|
|
auto* lhs_object = &lhs.as_object();
|
|
|
|
|
|
|
|
|
|
// 4. Let P be ? Get(C, "prototype").
|
2021-10-19 00:13:29 +03:00
|
|
|
|
auto rhs_prototype = TRY(rhs_function.get(vm.names.prototype));
|
2022-12-10 00:10:01 +00:00
|
|
|
|
|
|
|
|
|
// 5. If P is not an Object, throw a TypeError exception.
|
2021-10-19 00:13:29 +03:00
|
|
|
|
if (!rhs_prototype.is_object())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::InstanceOfOperatorBadPrototype, rhs.to_string_without_side_effects());
|
2022-12-10 00:10:01 +00:00
|
|
|
|
|
|
|
|
|
// 6. Repeat,
|
2020-07-11 15:27:28 -07:00
|
|
|
|
while (true) {
|
2022-12-10 00:10:01 +00:00
|
|
|
|
// a. Set O to ? O.[[GetPrototypeOf]]().
|
2021-10-19 00:13:29 +03:00
|
|
|
|
lhs_object = TRY(lhs_object->internal_get_prototype_of());
|
2022-12-10 00:10:01 +00:00
|
|
|
|
|
|
|
|
|
// b. If O is null, return false.
|
2020-07-11 15:27:28 -07:00
|
|
|
|
if (!lhs_object)
|
|
|
|
|
return Value(false);
|
2022-12-10 00:10:01 +00:00
|
|
|
|
|
|
|
|
|
// c. If SameValue(P, O) is true, return true.
|
2020-09-27 18:36:49 +02:00
|
|
|
|
if (same_value(rhs_prototype, lhs_object))
|
2020-07-11 15:27:28 -07:00
|
|
|
|
return Value(true);
|
|
|
|
|
}
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.10 SameValue ( x, y ), https://tc39.es/ecma262/#sec-samevalue
|
2020-09-27 18:36:49 +02:00
|
|
|
|
bool same_value(Value lhs, Value rhs)
|
2020-05-07 17:09:00 -07:00
|
|
|
|
{
|
2021-03-21 18:03:00 +01:00
|
|
|
|
if (!same_type_for_equality(lhs, rhs))
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs.is_number()) {
|
|
|
|
|
if (lhs.is_nan() && rhs.is_nan())
|
|
|
|
|
return true;
|
|
|
|
|
if (lhs.is_positive_zero() && rhs.is_negative_zero())
|
|
|
|
|
return false;
|
|
|
|
|
if (lhs.is_negative_zero() && rhs.is_positive_zero())
|
|
|
|
|
return false;
|
2020-05-18 00:28:00 +01:00
|
|
|
|
return lhs.as_double() == rhs.as_double();
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (lhs.is_bigint()) {
|
|
|
|
|
auto lhs_big_integer = lhs.as_bigint().big_integer();
|
|
|
|
|
auto rhs_big_integer = rhs.as_bigint().big_integer();
|
|
|
|
|
if (lhs_big_integer == BIGINT_ZERO && rhs_big_integer == BIGINT_ZERO && lhs_big_integer.is_negative() != rhs_big_integer.is_negative())
|
|
|
|
|
return false;
|
|
|
|
|
return lhs_big_integer == rhs_big_integer;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
|
return same_value_non_numeric(lhs, rhs);
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.11 SameValueZero ( x, y ), https://tc39.es/ecma262/#sec-samevaluezero
|
2020-09-27 18:36:49 +02:00
|
|
|
|
bool same_value_zero(Value lhs, Value rhs)
|
2020-05-07 17:09:00 -07:00
|
|
|
|
{
|
2021-03-21 18:03:00 +01:00
|
|
|
|
if (!same_type_for_equality(lhs, rhs))
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (lhs.is_number()) {
|
|
|
|
|
if (lhs.is_nan() && rhs.is_nan())
|
|
|
|
|
return true;
|
2020-05-18 00:28:00 +01:00
|
|
|
|
return lhs.as_double() == rhs.as_double();
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (lhs.is_bigint())
|
|
|
|
|
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
|
|
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
|
return same_value_non_numeric(lhs, rhs);
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.12 SameValueNonNumeric ( x, y ), https://tc39.es/ecma262/#sec-samevaluenonnumeric
|
2020-09-27 18:36:49 +02:00
|
|
|
|
bool same_value_non_numeric(Value lhs, Value rhs)
|
2020-05-07 17:09:00 -07:00
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY(!lhs.is_number() && !lhs.is_bigint());
|
2021-03-21 18:03:00 +01:00
|
|
|
|
VERIFY(same_type_for_equality(lhs, rhs));
|
2020-05-07 17:09:00 -07:00
|
|
|
|
|
2022-02-25 01:26:52 +01:00
|
|
|
|
if (lhs.is_string())
|
2022-12-06 01:12:49 +00:00
|
|
|
|
return lhs.as_string().deprecated_string() == rhs.as_string().deprecated_string();
|
2022-02-25 01:26:52 +01:00
|
|
|
|
|
|
|
|
|
return lhs.m_value.encoded == rhs.m_value.encoded;
|
2020-03-11 12:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.15 IsStrictlyEqual ( x, y ), https://tc39.es/ecma262/#sec-isstrictlyequal
|
2021-09-23 23:43:28 +02:00
|
|
|
|
bool is_strictly_equal(Value lhs, Value rhs)
|
2020-03-16 00:23:38 +02:00
|
|
|
|
{
|
2021-03-21 18:03:00 +01:00
|
|
|
|
if (!same_type_for_equality(lhs, rhs))
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return false;
|
2020-03-16 00:23:38 +02:00
|
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
|
if (lhs.is_number()) {
|
|
|
|
|
if (lhs.is_nan() || rhs.is_nan())
|
|
|
|
|
return false;
|
2020-05-18 00:28:00 +01:00
|
|
|
|
if (lhs.as_double() == rhs.as_double())
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-16 00:23:38 +02:00
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (lhs.is_bigint())
|
|
|
|
|
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
|
|
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
|
return same_value_non_numeric(lhs, rhs);
|
2020-05-07 17:09:00 -07:00
|
|
|
|
}
|
2020-03-16 00:23:38 +02:00
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.14 IsLooselyEqual ( x, y ), https://tc39.es/ecma262/#sec-islooselyequal
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<bool> is_loosely_equal(VM& vm, Value lhs, Value rhs)
|
2020-05-07 17:09:00 -07:00
|
|
|
|
{
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// 1. If Type(x) is the same as Type(y), then
|
|
|
|
|
if (same_type_for_equality(lhs, rhs)) {
|
|
|
|
|
// a. Return IsStrictlyEqual(x, y).
|
2021-09-23 23:43:28 +02:00
|
|
|
|
return is_strictly_equal(lhs, rhs);
|
2021-10-15 01:47:54 +01:00
|
|
|
|
}
|
2020-03-16 00:23:38 +02:00
|
|
|
|
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// 2. If x is null and y is undefined, return true.
|
|
|
|
|
// 3. If x is undefined and y is null, return true.
|
2020-10-02 16:00:15 +02:00
|
|
|
|
if (lhs.is_nullish() && rhs.is_nullish())
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return true;
|
2020-03-16 00:23:38 +02:00
|
|
|
|
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// 4. NOTE: This step is replaced in section B.3.6.2.
|
|
|
|
|
// B.3.6.2 Changes to IsLooselyEqual, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-aec
|
2022-05-03 21:25:02 +02:00
|
|
|
|
// 4. Perform the following steps:
|
|
|
|
|
// a. If Type(x) is Object and x has an [[IsHTMLDDA]] internal slot and y is either null or undefined, return true.
|
2021-06-20 17:08:29 +01:00
|
|
|
|
if (lhs.is_object() && lhs.as_object().is_htmldda() && rhs.is_nullish())
|
|
|
|
|
return true;
|
2021-10-15 01:47:54 +01:00
|
|
|
|
|
2022-05-03 21:25:02 +02:00
|
|
|
|
// b. If x is either null or undefined and Type(y) is Object and y has an [[IsHTMLDDA]] internal slot, return true.
|
2021-06-20 17:08:29 +01:00
|
|
|
|
if (lhs.is_nullish() && rhs.is_object() && rhs.as_object().is_htmldda())
|
|
|
|
|
return true;
|
|
|
|
|
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// == End of B.3.6.2 ==
|
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 5. If Type(x) is Number and Type(y) is String, return ! IsLooselyEqual(x, ! ToNumber(y)).
|
2020-05-07 17:09:00 -07:00
|
|
|
|
if (lhs.is_number() && rhs.is_string())
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return is_loosely_equal(vm, lhs, MUST(rhs.to_number(vm)));
|
2020-03-16 00:23:38 +02:00
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 6. If Type(x) is String and Type(y) is Number, return ! IsLooselyEqual(! ToNumber(x), y).
|
2020-05-07 17:09:00 -07:00
|
|
|
|
if (lhs.is_string() && rhs.is_number())
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return is_loosely_equal(vm, MUST(lhs.to_number(vm)), rhs);
|
2020-04-23 16:06:01 +01:00
|
|
|
|
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// 7. If Type(x) is BigInt and Type(y) is String, then
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (lhs.is_bigint() && rhs.is_string()) {
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// a. Let n be StringToBigInt(y).
|
2022-12-06 01:12:49 +00:00
|
|
|
|
auto bigint = string_to_bigint(vm, rhs.as_string().deprecated_string());
|
2022-01-31 10:24:12 -05:00
|
|
|
|
|
|
|
|
|
// b. If n is undefined, return false.
|
|
|
|
|
if (!bigint.has_value())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return false;
|
2022-01-31 10:24:12 -05:00
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// c. Return ! IsLooselyEqual(x, n).
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return is_loosely_equal(vm, lhs, *bigint);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 8. If Type(x) is String and Type(y) is BigInt, return ! IsLooselyEqual(y, x).
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (lhs.is_string() && rhs.is_bigint())
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return is_loosely_equal(vm, rhs, lhs);
|
2020-06-06 01:14:10 +01:00
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 9. If Type(x) is Boolean, return ! IsLooselyEqual(! ToNumber(x), y).
|
2020-05-07 17:09:00 -07:00
|
|
|
|
if (lhs.is_boolean())
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return is_loosely_equal(vm, MUST(lhs.to_number(vm)), rhs);
|
2020-04-23 16:06:01 +01:00
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 10. If Type(y) is Boolean, return ! IsLooselyEqual(x, ! ToNumber(y)).
|
2020-05-07 17:09:00 -07:00
|
|
|
|
if (rhs.is_boolean())
|
2022-08-21 14:00:56 +01:00
|
|
|
|
return is_loosely_equal(vm, lhs, MUST(rhs.to_number(vm)));
|
2020-03-28 16:56:54 +01:00
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return ! IsLooselyEqual(x, ? ToPrimitive(y)).
|
2021-03-02 19:22:36 +01:00
|
|
|
|
if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto rhs_primitive = TRY(rhs.to_primitive(vm));
|
|
|
|
|
return is_loosely_equal(vm, lhs, rhs_primitive);
|
2021-03-02 19:22:36 +01:00
|
|
|
|
}
|
2020-03-28 16:56:54 +01:00
|
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
|
// 12. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return ! IsLooselyEqual(? ToPrimitive(x), y).
|
2021-10-15 01:47:54 +01:00
|
|
|
|
if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_bigint() || rhs.is_symbol())) {
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto lhs_primitive = TRY(lhs.to_primitive(vm));
|
|
|
|
|
return is_loosely_equal(vm, lhs_primitive, rhs);
|
2021-03-02 19:22:36 +01:00
|
|
|
|
}
|
2020-03-28 16:56:54 +01:00
|
|
|
|
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// 13. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// a. If x or y are any of NaN, +∞𝔽, or -∞𝔽, return false.
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
|
|
|
|
|
return false;
|
2021-10-15 01:47:54 +01:00
|
|
|
|
|
|
|
|
|
// b. If ℝ(x) = ℝ(y), return true; otherwise return false.
|
2021-06-16 13:17:06 +03:00
|
|
|
|
if ((lhs.is_number() && !lhs.is_integral_number()) || (rhs.is_number() && !rhs.is_integral_number()))
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return false;
|
2022-08-19 23:51:32 +02:00
|
|
|
|
|
|
|
|
|
VERIFY(!lhs.is_nan() && !rhs.is_nan());
|
|
|
|
|
|
|
|
|
|
auto& number_side = lhs.is_number() ? lhs : rhs;
|
|
|
|
|
auto& bigint_side = lhs.is_number() ? rhs : lhs;
|
|
|
|
|
|
2022-10-23 16:46:35 +01:00
|
|
|
|
return bigint_side.as_bigint().big_integer().compare_to_double(number_side.as_double()) == Crypto::UnsignedBigInteger::CompareResult::DoubleEqualsBigInt;
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 01:47:54 +01:00
|
|
|
|
// 14. Return false.
|
2020-05-07 17:09:00 -07:00
|
|
|
|
return false;
|
2020-03-07 21:43:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
|
// 7.2.13 IsLessThan ( x, y, LeftFirst ), https://tc39.es/ecma262/#sec-islessthan
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<TriState> is_less_than(VM& vm, Value lhs, Value rhs, bool left_first)
|
2020-05-28 17:19:59 +02:00
|
|
|
|
{
|
2020-06-06 01:14:10 +01:00
|
|
|
|
Value x_primitive;
|
|
|
|
|
Value y_primitive;
|
2020-05-28 17:19:59 +02:00
|
|
|
|
|
|
|
|
|
if (left_first) {
|
2022-08-21 14:00:56 +01:00
|
|
|
|
x_primitive = TRY(lhs.to_primitive(vm, Value::PreferredType::Number));
|
|
|
|
|
y_primitive = TRY(rhs.to_primitive(vm, Value::PreferredType::Number));
|
2020-05-28 17:19:59 +02:00
|
|
|
|
} else {
|
2022-08-21 14:00:56 +01:00
|
|
|
|
y_primitive = TRY(lhs.to_primitive(vm, Value::PreferredType::Number));
|
|
|
|
|
x_primitive = TRY(rhs.to_primitive(vm, Value::PreferredType::Number));
|
2020-05-28 17:19:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x_primitive.is_string() && y_primitive.is_string()) {
|
2022-12-06 01:12:49 +00:00
|
|
|
|
auto x_string = x_primitive.as_string().deprecated_string();
|
|
|
|
|
auto y_string = y_primitive.as_string().deprecated_string();
|
2020-05-28 17:19:59 +02:00
|
|
|
|
|
2021-03-21 22:33:06 +02:00
|
|
|
|
Utf8View x_code_points { x_string };
|
|
|
|
|
Utf8View y_code_points { y_string };
|
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
|
for (auto k = x_code_points.begin(), l = y_code_points.begin();
|
|
|
|
|
k != x_code_points.end() && l != y_code_points.end();
|
2020-05-28 17:19:59 +02:00
|
|
|
|
++k, ++l) {
|
|
|
|
|
if (*k != *l) {
|
|
|
|
|
if (*k < *l) {
|
|
|
|
|
return TriState::True;
|
|
|
|
|
} else {
|
|
|
|
|
return TriState::False;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-09 23:41:29 +00:00
|
|
|
|
|
|
|
|
|
return x_code_points.length() < y_code_points.length()
|
|
|
|
|
? TriState::True
|
|
|
|
|
: TriState::False;
|
2020-05-28 17:19:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (x_primitive.is_bigint() && y_primitive.is_string()) {
|
2022-12-06 01:12:49 +00:00
|
|
|
|
auto y_bigint = string_to_bigint(vm, y_primitive.as_string().deprecated_string());
|
2022-01-31 10:59:53 -05:00
|
|
|
|
if (!y_bigint.has_value())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return TriState::Unknown;
|
2022-01-31 10:59:53 -05:00
|
|
|
|
|
|
|
|
|
if (x_primitive.as_bigint().big_integer() < (*y_bigint)->big_integer())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return TriState::True;
|
2022-01-31 10:59:53 -05:00
|
|
|
|
return TriState::False;
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
2020-05-28 17:19:59 +02:00
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (x_primitive.is_string() && y_primitive.is_bigint()) {
|
2022-12-06 01:12:49 +00:00
|
|
|
|
auto x_bigint = string_to_bigint(vm, x_primitive.as_string().deprecated_string());
|
2022-01-31 10:59:53 -05:00
|
|
|
|
if (!x_bigint.has_value())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return TriState::Unknown;
|
2022-01-31 10:59:53 -05:00
|
|
|
|
|
|
|
|
|
if ((*x_bigint)->big_integer() < y_primitive.as_bigint().big_integer())
|
2020-06-06 01:14:10 +01:00
|
|
|
|
return TriState::True;
|
2022-01-31 10:59:53 -05:00
|
|
|
|
return TriState::False;
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto x_numeric = TRY(x_primitive.to_numeric(vm));
|
|
|
|
|
auto y_numeric = TRY(y_primitive.to_numeric(vm));
|
2020-05-28 17:19:59 +02:00
|
|
|
|
|
|
|
|
|
if (x_numeric.is_nan() || y_numeric.is_nan())
|
|
|
|
|
return TriState::Unknown;
|
|
|
|
|
|
|
|
|
|
if (x_numeric.is_positive_infinity() || y_numeric.is_negative_infinity())
|
|
|
|
|
return TriState::False;
|
|
|
|
|
|
|
|
|
|
if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
|
|
|
|
|
return TriState::True;
|
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (x_numeric.is_number() && y_numeric.is_number()) {
|
|
|
|
|
if (x_numeric.as_double() < y_numeric.as_double())
|
|
|
|
|
return TriState::True;
|
|
|
|
|
else
|
|
|
|
|
return TriState::False;
|
|
|
|
|
}
|
2020-05-28 17:19:59 +02:00
|
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (x_numeric.is_bigint() && y_numeric.is_bigint()) {
|
|
|
|
|
if (x_numeric.as_bigint().big_integer() < y_numeric.as_bigint().big_integer())
|
|
|
|
|
return TriState::True;
|
|
|
|
|
else
|
|
|
|
|
return TriState::False;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
|
VERIFY((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
|
2020-06-06 01:14:10 +01:00
|
|
|
|
|
|
|
|
|
bool x_lower_than_y;
|
2022-08-19 23:51:32 +02:00
|
|
|
|
VERIFY(!x_numeric.is_nan() && !y_numeric.is_nan());
|
2020-06-06 01:14:10 +01:00
|
|
|
|
if (x_numeric.is_number()) {
|
2022-08-19 23:51:32 +02:00
|
|
|
|
x_lower_than_y = y_numeric.as_bigint().big_integer().compare_to_double(x_numeric.as_double())
|
2022-10-23 16:46:35 +01:00
|
|
|
|
== Crypto::UnsignedBigInteger::CompareResult::DoubleLessThanBigInt;
|
2020-06-06 01:14:10 +01:00
|
|
|
|
} else {
|
2022-08-19 23:51:32 +02:00
|
|
|
|
x_lower_than_y = x_numeric.as_bigint().big_integer().compare_to_double(y_numeric.as_double())
|
2022-10-23 16:46:35 +01:00
|
|
|
|
== Crypto::UnsignedBigInteger::CompareResult::DoubleGreaterThanBigInt;
|
2020-06-06 01:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (x_lower_than_y)
|
2020-05-28 17:19:59 +02:00
|
|
|
|
return TriState::True;
|
|
|
|
|
else
|
|
|
|
|
return TriState::False;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 08:27:04 -05:00
|
|
|
|
// 7.3.21 Invoke ( V, P [ , argumentsList ] ), https://tc39.es/ecma262/#sec-invoke
|
2022-08-21 14:00:56 +01:00
|
|
|
|
ThrowCompletionOr<Value> Value::invoke_internal(VM& vm, PropertyKey const& property_key, Optional<MarkedVector<Value>> arguments)
|
2021-08-09 16:45:43 +02:00
|
|
|
|
{
|
2022-08-21 14:00:56 +01:00
|
|
|
|
auto property = TRY(get(vm, property_key));
|
2021-09-23 20:56:28 +03:00
|
|
|
|
if (!property.is_function())
|
2022-08-16 20:33:17 +01:00
|
|
|
|
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, property.to_string_without_side_effects());
|
2021-08-09 16:45:43 +02:00
|
|
|
|
|
2022-08-21 19:24:32 +01:00
|
|
|
|
return call(vm, property.as_function(), *this, move(arguments));
|
2021-08-09 16:45:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
|
}
|