2020-03-07 19:42:11 +01:00
|
|
|
/*
|
2021-03-21 18:03:00 +01:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2021-06-06 16:10:23 +01:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@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
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2021-03-22 21:26:16 +03:00
|
|
|
#include <AK/BitCast.h>
|
2022-01-27 07:45:00 -05:00
|
|
|
#include <AK/Concepts.h>
|
2020-12-06 16:55:19 +00:00
|
|
|
#include <AK/Format.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <AK/Forward.h>
|
2021-06-09 23:27:01 +01:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/Result.h>
|
2020-12-06 16:55:19 +00:00
|
|
|
#include <AK/String.h>
|
2020-05-28 17:19:59 +02:00
|
|
|
#include <AK/Types.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <LibJS/Forward.h>
|
2021-06-12 23:15:19 +03:00
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2021-08-09 09:06:45 -04:00
|
|
|
#include <LibJS/Runtime/Utf16String.h>
|
2020-06-04 13:03:15 +01:00
|
|
|
#include <math.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-05-22 13:20:53 +01:00
|
|
|
// 2 ** 53 - 1
|
|
|
|
static constexpr double MAX_ARRAY_LIKE_INDEX = 9007199254740991.0;
|
2021-03-22 21:26:16 +03:00
|
|
|
// Unique bit representation of negative zero (only sign bit set)
|
|
|
|
static constexpr u64 NEGATIVE_ZERO_BITS = ((u64)1 << 63);
|
2020-05-22 13:20:53 +01:00
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class Value {
|
|
|
|
public:
|
|
|
|
enum class Type {
|
2020-04-06 20:24:45 +02:00
|
|
|
Empty,
|
2020-03-07 19:42:11 +01:00
|
|
|
Undefined,
|
|
|
|
Null,
|
2021-03-21 18:03:00 +01:00
|
|
|
Int32,
|
|
|
|
Double,
|
2020-03-07 19:42:11 +01:00
|
|
|
String,
|
|
|
|
Object,
|
|
|
|
Boolean,
|
2020-04-29 23:25:21 -07:00
|
|
|
Symbol,
|
2020-05-21 11:14:23 -07:00
|
|
|
Accessor,
|
2020-06-06 01:14:10 +01:00
|
|
|
BigInt,
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
2020-05-28 17:19:59 +02:00
|
|
|
enum class PreferredType {
|
|
|
|
Default,
|
|
|
|
String,
|
|
|
|
Number,
|
|
|
|
};
|
|
|
|
|
2020-04-06 20:24:45 +02:00
|
|
|
bool is_empty() const { return m_type == Type::Empty; }
|
2020-03-07 19:42:11 +01:00
|
|
|
bool is_undefined() const { return m_type == Type::Undefined; }
|
|
|
|
bool is_null() const { return m_type == Type::Null; }
|
2021-03-21 18:03:00 +01:00
|
|
|
bool is_number() const { return m_type == Type::Int32 || m_type == Type::Double; }
|
2020-03-07 19:42:11 +01:00
|
|
|
bool is_string() const { return m_type == Type::String; }
|
|
|
|
bool is_object() const { return m_type == Type::Object; }
|
|
|
|
bool is_boolean() const { return m_type == Type::Boolean; }
|
2020-04-29 23:25:21 -07:00
|
|
|
bool is_symbol() const { return m_type == Type::Symbol; }
|
2020-05-21 11:14:23 -07:00
|
|
|
bool is_accessor() const { return m_type == Type::Accessor; };
|
2020-06-06 01:14:10 +01:00
|
|
|
bool is_bigint() const { return m_type == Type::BigInt; };
|
2020-10-02 16:00:15 +02:00
|
|
|
bool is_nullish() const { return is_null() || is_undefined(); }
|
2021-07-07 19:34:26 +03:00
|
|
|
bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol(); }
|
2021-09-23 20:05:34 +03:00
|
|
|
ThrowCompletionOr<bool> is_array(GlobalObject&) const;
|
2020-05-06 11:52:53 +01:00
|
|
|
bool is_function() 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
|
|
|
bool is_constructor() const;
|
2021-09-23 20:10:20 +03:00
|
|
|
ThrowCompletionOr<bool> is_regexp(GlobalObject&) const;
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2021-10-07 19:23:56 +02:00
|
|
|
bool is_nan() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return false;
|
|
|
|
return is_number() && __builtin_isnan(as_double());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_infinity() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return false;
|
|
|
|
return is_number() && __builtin_isinf(as_double());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_positive_infinity() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return false;
|
|
|
|
return is_number() && __builtin_isinf_sign(as_double()) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_negative_infinity() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return false;
|
|
|
|
return is_number() && __builtin_isinf_sign(as_double()) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_positive_zero() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return as_i32() == 0;
|
|
|
|
return is_number() && bit_cast<u64>(as_double()) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_negative_zero() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return false;
|
|
|
|
return is_number() && bit_cast<u64>(as_double()) == NEGATIVE_ZERO_BITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_integral_number() const
|
|
|
|
{
|
|
|
|
if (type() == Type::Int32)
|
|
|
|
return true;
|
|
|
|
return is_finite_number() && trunc(as_double()) == as_double();
|
|
|
|
}
|
|
|
|
|
2020-04-23 13:36:14 +01:00
|
|
|
bool is_finite_number() const
|
|
|
|
{
|
2021-10-07 19:23:56 +02:00
|
|
|
if (type() == Type::Int32)
|
|
|
|
return true;
|
2020-04-23 13:36:14 +01:00
|
|
|
if (!is_number())
|
|
|
|
return false;
|
|
|
|
auto number = as_double();
|
|
|
|
return !__builtin_isnan(number) && !__builtin_isinf(number);
|
|
|
|
}
|
2020-03-27 12:26:37 +01:00
|
|
|
|
2020-04-06 20:30:36 +02:00
|
|
|
Value()
|
|
|
|
: m_type(Type::Empty)
|
|
|
|
{
|
|
|
|
}
|
2020-03-20 20:49:48 +01:00
|
|
|
|
2022-01-27 07:45:00 -05:00
|
|
|
template<typename T>
|
|
|
|
requires(SameAs<RemoveCVReference<T>, bool>) explicit Value(T value)
|
2020-03-07 23:16:14 +01:00
|
|
|
: m_type(Type::Boolean)
|
|
|
|
{
|
|
|
|
m_value.as_bool = value;
|
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
explicit Value(double value)
|
|
|
|
{
|
2021-03-22 21:26:16 +03:00
|
|
|
bool is_negative_zero = bit_cast<u64>(value) == NEGATIVE_ZERO_BITS;
|
2021-03-22 07:49:08 +01:00
|
|
|
if (value >= NumericLimits<i32>::min() && value <= NumericLimits<i32>::max() && trunc(value) == value && !is_negative_zero) {
|
2021-03-21 18:03:00 +01:00
|
|
|
m_type = Type::Int32;
|
|
|
|
m_value.as_i32 = static_cast<i32>(value);
|
|
|
|
} else {
|
|
|
|
m_type = Type::Double;
|
|
|
|
m_value.as_double = value;
|
|
|
|
}
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 13:32:19 +02:00
|
|
|
explicit Value(unsigned long value)
|
|
|
|
{
|
|
|
|
if (value > NumericLimits<i32>::max()) {
|
|
|
|
m_value.as_double = static_cast<double>(value);
|
|
|
|
m_type = Type::Double;
|
|
|
|
} else {
|
|
|
|
m_value.as_i32 = static_cast<i32>(value);
|
|
|
|
m_type = Type::Int32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:43:10 +01:00
|
|
|
explicit Value(unsigned value)
|
|
|
|
{
|
2021-03-21 18:03:00 +01:00
|
|
|
if (value > NumericLimits<i32>::max()) {
|
|
|
|
m_value.as_double = static_cast<double>(value);
|
|
|
|
m_type = Type::Double;
|
|
|
|
} else {
|
|
|
|
m_value.as_i32 = static_cast<i32>(value);
|
|
|
|
m_type = Type::Int32;
|
|
|
|
}
|
2020-04-23 15:43:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
explicit Value(i32 value)
|
2021-03-21 18:03:00 +01:00
|
|
|
: m_type(Type::Int32)
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2021-03-21 18:03:00 +01:00
|
|
|
m_value.as_i32 = value;
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Value(Object const* object)
|
2020-03-28 17:23:54 +01:00
|
|
|
: m_type(object ? Type::Object : Type::Null)
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2020-11-28 15:57:25 +01:00
|
|
|
m_value.as_object = const_cast<Object*>(object);
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Value(PrimitiveString const* string)
|
2020-03-11 18:58:19 +01:00
|
|
|
: m_type(Type::String)
|
|
|
|
{
|
2020-11-28 15:57:25 +01:00
|
|
|
m_value.as_string = const_cast<PrimitiveString*>(string);
|
2020-03-11 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Value(Symbol const* symbol)
|
2020-04-29 23:25:21 -07:00
|
|
|
: m_type(Type::Symbol)
|
|
|
|
{
|
2020-11-28 15:57:25 +01:00
|
|
|
m_value.as_symbol = const_cast<Symbol*>(symbol);
|
2020-04-29 23:25:21 -07:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Value(Accessor const* accessor)
|
2020-05-21 11:14:23 -07:00
|
|
|
: m_type(Type::Accessor)
|
|
|
|
{
|
2020-11-28 15:57:25 +01:00
|
|
|
m_value.as_accessor = const_cast<Accessor*>(accessor);
|
2020-05-21 11:14:23 -07:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Value(BigInt const* bigint)
|
2020-06-06 01:14:10 +01:00
|
|
|
: m_type(Type::BigInt)
|
|
|
|
{
|
2020-11-28 15:57:25 +01:00
|
|
|
m_value.as_bigint = const_cast<BigInt*>(bigint);
|
2020-06-06 01:14:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
explicit Value(Type type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
|
|
double as_double() const
|
|
|
|
{
|
2021-03-21 18:03:00 +01:00
|
|
|
VERIFY(is_number());
|
|
|
|
if (m_type == Type::Int32)
|
|
|
|
return m_value.as_i32;
|
2020-03-07 19:42:11 +01:00
|
|
|
return m_value.as_double;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool as_bool() const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(type() == Type::Boolean);
|
2020-03-07 19:42:11 +01:00
|
|
|
return m_value.as_bool;
|
|
|
|
}
|
|
|
|
|
2020-04-01 22:18:47 +02:00
|
|
|
Object& as_object()
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(type() == Type::Object);
|
2020-04-01 22:18:47 +02:00
|
|
|
return *m_value.as_object;
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Object const& as_object() const
|
2020-03-07 19:42:11 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(type() == Type::Object);
|
2020-04-01 22:18:47 +02:00
|
|
|
return *m_value.as_object;
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 12:35:39 +02:00
|
|
|
PrimitiveString& as_string()
|
2020-03-11 18:58:19 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_string());
|
2020-04-29 12:35:39 +02:00
|
|
|
return *m_value.as_string;
|
2020-03-11 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
PrimitiveString const& as_string() const
|
2020-03-07 23:16:14 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_string());
|
2020-04-29 12:35:39 +02:00
|
|
|
return *m_value.as_string;
|
2020-03-07 23:16:14 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 23:25:21 -07:00
|
|
|
Symbol& as_symbol()
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_symbol());
|
2020-04-29 23:25:21 -07:00
|
|
|
return *m_value.as_symbol;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Symbol const& as_symbol() const
|
2020-04-29 23:25:21 -07:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_symbol());
|
2020-04-29 23:25:21 -07:00
|
|
|
return *m_value.as_symbol;
|
|
|
|
}
|
|
|
|
|
2021-05-25 18:48:11 +02:00
|
|
|
Cell& as_cell()
|
2020-03-11 18:58:19 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_cell());
|
2021-05-25 18:48:11 +02:00
|
|
|
return *m_value.as_cell;
|
2020-03-11 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:00:17 +01:00
|
|
|
Accessor& as_accessor()
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_accessor());
|
2020-06-04 22:00:17 +01:00
|
|
|
return *m_value.as_accessor;
|
|
|
|
}
|
2020-05-15 13:39:24 +02:00
|
|
|
|
2020-06-06 01:14:10 +01:00
|
|
|
BigInt& as_bigint()
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_bigint());
|
2020-06-06 01:14:10 +01:00
|
|
|
return *m_value.as_bigint;
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:01:00 -07:00
|
|
|
Array& as_array();
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject& as_function();
|
2021-09-25 09:43:34 +02:00
|
|
|
FunctionObject const& as_function() const;
|
2020-05-06 11:52:53 +01:00
|
|
|
|
2021-10-07 19:23:56 +02:00
|
|
|
// FIXME: These two conversions are wrong for JS, and seem likely to be footguns
|
|
|
|
i32 as_i32() const
|
|
|
|
{
|
|
|
|
if (m_type == Type::Int32)
|
|
|
|
return m_value.as_i32;
|
|
|
|
return static_cast<i32>(as_double());
|
|
|
|
}
|
|
|
|
u32 as_u32() const
|
|
|
|
{
|
|
|
|
if (m_type == Type::Int32 && m_value.as_i32 >= 0)
|
|
|
|
return m_value.as_i32;
|
|
|
|
VERIFY(as_double() >= 0);
|
|
|
|
return (u32)min(as_double(), (double)NumericLimits<u32>::max());
|
|
|
|
}
|
2020-05-18 08:59:35 +01:00
|
|
|
|
2021-06-09 00:08:47 +03:00
|
|
|
u64 encoded() const { return m_value.encoded; }
|
|
|
|
|
2021-10-12 17:49:01 +01:00
|
|
|
ThrowCompletionOr<String> to_string(GlobalObject&) const;
|
2021-10-12 18:01:17 +01:00
|
|
|
ThrowCompletionOr<Utf16String> to_utf16_string(GlobalObject&) const;
|
2021-10-12 18:05:46 +01:00
|
|
|
ThrowCompletionOr<PrimitiveString*> to_primitive_string(GlobalObject&);
|
2021-10-12 18:09:14 +01:00
|
|
|
ThrowCompletionOr<Value> to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
|
2021-10-12 19:24:57 +01:00
|
|
|
ThrowCompletionOr<Object*> to_object(GlobalObject&) const;
|
2021-10-16 21:42:32 +03:00
|
|
|
ThrowCompletionOr<Value> to_numeric(GlobalObject&) const;
|
2021-10-17 23:20:05 +03:00
|
|
|
ThrowCompletionOr<Value> to_number(GlobalObject&) const;
|
2021-10-16 21:52:16 +03:00
|
|
|
ThrowCompletionOr<BigInt*> to_bigint(GlobalObject&) const;
|
2021-10-16 21:56:14 +03:00
|
|
|
ThrowCompletionOr<i64> to_bigint_int64(GlobalObject&) const;
|
2021-10-16 22:01:10 +03:00
|
|
|
ThrowCompletionOr<u64> to_bigint_uint64(GlobalObject&) const;
|
2021-10-16 22:11:08 +03:00
|
|
|
ThrowCompletionOr<double> to_double(GlobalObject&) const;
|
2021-10-24 16:25:39 +02:00
|
|
|
ThrowCompletionOr<PropertyKey> to_property_key(GlobalObject&) const;
|
2021-10-17 23:33:35 +03:00
|
|
|
ThrowCompletionOr<i32> to_i32(GlobalObject& global_object) const;
|
2021-10-17 23:43:29 +03:00
|
|
|
ThrowCompletionOr<u32> to_u32(GlobalObject&) const;
|
2021-10-17 23:44:25 +03:00
|
|
|
ThrowCompletionOr<i16> to_i16(GlobalObject&) const;
|
2021-10-17 23:48:24 +03:00
|
|
|
ThrowCompletionOr<u16> to_u16(GlobalObject&) const;
|
2021-10-17 23:49:09 +03:00
|
|
|
ThrowCompletionOr<i8> to_i8(GlobalObject&) const;
|
2021-10-17 23:49:41 +03:00
|
|
|
ThrowCompletionOr<u8> to_u8(GlobalObject&) const;
|
2021-10-17 23:50:06 +03:00
|
|
|
ThrowCompletionOr<u8> to_u8_clamp(GlobalObject&) const;
|
2021-10-17 23:57:37 +03:00
|
|
|
ThrowCompletionOr<size_t> to_length(GlobalObject&) const;
|
2021-10-18 00:05:01 +03:00
|
|
|
ThrowCompletionOr<size_t> to_index(GlobalObject&) const;
|
2021-10-18 19:19:57 +01:00
|
|
|
ThrowCompletionOr<double> to_integer_or_infinity(GlobalObject&) const;
|
2020-05-18 00:28:00 +01:00
|
|
|
bool to_boolean() const;
|
2020-03-11 18:58:19 +01:00
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
ThrowCompletionOr<Value> get(GlobalObject&, PropertyKey const&) const;
|
|
|
|
ThrowCompletionOr<FunctionObject*> get_method(GlobalObject&, PropertyKey const&) const;
|
2021-06-26 19:17:28 +01:00
|
|
|
|
2020-06-04 22:00:17 +01:00
|
|
|
String to_string_without_side_effects() const;
|
2022-01-31 10:23:51 -05:00
|
|
|
Optional<BigInt*> string_to_bigint(GlobalObject& global_object) const;
|
2020-06-04 22:00:17 +01:00
|
|
|
|
2020-04-25 18:43:34 +02:00
|
|
|
Value value_or(Value fallback) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return fallback;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 20:33:03 +02:00
|
|
|
String typeof() const;
|
|
|
|
|
2021-07-04 17:49:54 +01:00
|
|
|
bool operator==(Value const&) const;
|
|
|
|
|
2021-08-09 16:45:43 +02:00
|
|
|
template<typename... Args>
|
2022-02-06 15:59:04 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE ThrowCompletionOr<Value> invoke(GlobalObject& global_object, PropertyKey const& property_key, Args... args);
|
2021-08-09 16:45:43 +02:00
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
private:
|
2020-04-16 16:11:11 +02:00
|
|
|
Type m_type { Type::Empty };
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2022-02-09 10:06:40 +00:00
|
|
|
[[nodiscard]] ThrowCompletionOr<Value> invoke_internal(GlobalObject& global_object, PropertyKey const&, Optional<MarkedVector<Value>> arguments);
|
2021-08-09 16:45:43 +02:00
|
|
|
|
2021-10-17 23:33:35 +03:00
|
|
|
ThrowCompletionOr<i32> to_i32_slow_case(GlobalObject&) const;
|
2021-03-21 18:03:00 +01:00
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
union {
|
|
|
|
bool as_bool;
|
2021-03-21 18:03:00 +01:00
|
|
|
i32 as_i32;
|
2020-03-07 19:42:11 +01:00
|
|
|
double as_double;
|
2020-03-11 18:58:19 +01:00
|
|
|
PrimitiveString* as_string;
|
2020-04-29 23:25:21 -07:00
|
|
|
Symbol* as_symbol;
|
2020-03-07 19:42:11 +01:00
|
|
|
Object* as_object;
|
2020-03-11 18:58:19 +01:00
|
|
|
Cell* as_cell;
|
2020-05-21 11:14:23 -07:00
|
|
|
Accessor* as_accessor;
|
2020-06-06 01:14:10 +01:00
|
|
|
BigInt* as_bigint;
|
2021-06-09 00:08:47 +03:00
|
|
|
|
|
|
|
u64 encoded;
|
|
|
|
} m_value { .encoded = 0 };
|
2020-03-07 19:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline Value js_undefined()
|
|
|
|
{
|
|
|
|
return Value(Value::Type::Undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value js_null()
|
|
|
|
{
|
|
|
|
return Value(Value::Type::Null);
|
|
|
|
}
|
|
|
|
|
2020-03-27 12:26:37 +01:00
|
|
|
inline Value js_nan()
|
|
|
|
{
|
2020-06-04 13:03:15 +01:00
|
|
|
return Value(NAN);
|
2020-03-27 12:26:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 16:59:01 +01:00
|
|
|
inline Value js_infinity()
|
|
|
|
{
|
2020-06-04 13:05:27 +01:00
|
|
|
return Value(INFINITY);
|
2020-04-02 16:59:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:25:42 +01:00
|
|
|
inline Value js_negative_infinity()
|
|
|
|
{
|
2020-06-04 13:05:27 +01:00
|
|
|
return Value(-INFINITY);
|
2020-04-12 13:25:42 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:17:41 +02:00
|
|
|
inline void Cell::Visitor::visit(Value value)
|
|
|
|
{
|
|
|
|
if (value.is_cell())
|
2021-05-25 18:48:11 +02:00
|
|
|
visit_impl(value.as_cell());
|
2021-05-25 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 00:13:29 +03:00
|
|
|
ThrowCompletionOr<Value> greater_than(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> greater_than_equals(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> less_than(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> less_than_equals(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> bitwise_and(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> bitwise_or(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> bitwise_xor(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> bitwise_not(GlobalObject&, Value);
|
|
|
|
ThrowCompletionOr<Value> unary_plus(GlobalObject&, Value);
|
|
|
|
ThrowCompletionOr<Value> unary_minus(GlobalObject&, Value);
|
|
|
|
ThrowCompletionOr<Value> left_shift(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> right_shift(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> unsigned_right_shift(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> add(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> sub(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> mul(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> div(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> mod(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> exp(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> in(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> instance_of(GlobalObject&, Value lhs, Value rhs);
|
|
|
|
ThrowCompletionOr<Value> ordinary_has_instance(GlobalObject&, Value lhs, Value rhs);
|
2020-03-10 11:35:05 +01:00
|
|
|
|
2021-10-18 23:46:42 +03:00
|
|
|
ThrowCompletionOr<bool> is_loosely_equal(GlobalObject&, Value lhs, Value rhs);
|
2021-09-23 23:43:28 +02:00
|
|
|
bool is_strictly_equal(Value lhs, Value rhs);
|
2020-09-27 18:36:49 +02:00
|
|
|
bool same_value(Value lhs, Value rhs);
|
|
|
|
bool same_value_zero(Value lhs, Value rhs);
|
|
|
|
bool same_value_non_numeric(Value lhs, Value rhs);
|
2022-06-13 23:10:12 +01:00
|
|
|
ThrowCompletionOr<TriState> is_less_than(GlobalObject&, Value lhs, Value rhs, bool left_first);
|
2020-05-07 17:09:00 -07:00
|
|
|
|
2022-05-06 20:17:16 +02:00
|
|
|
double to_integer_or_infinity(double);
|
|
|
|
|
2021-07-04 17:49:54 +01:00
|
|
|
inline bool Value::operator==(Value const& value) const { return same_value(*this, value); }
|
|
|
|
|
2020-12-06 16:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Formatter<JS::Value> : Formatter<StringView> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, JS::Value value)
|
2020-12-06 16:55:19 +00:00
|
|
|
{
|
2021-11-16 01:15:21 +01:00
|
|
|
return Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
|
2020-12-06 16:55:19 +00:00
|
|
|
}
|
|
|
|
};
|
2020-03-07 19:42:11 +01:00
|
|
|
|
|
|
|
}
|