1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:17:35 +00:00

LibJS: Add BigInt

This commit is contained in:
Linus Groh 2020-06-06 01:14:10 +01:00 committed by Andreas Kling
parent 40829b849a
commit 0ff9d7e189
32 changed files with 1910 additions and 636 deletions

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/BigInt.h>
namespace JS {
BigInt::BigInt(Crypto::SignedBigInteger big_integer)
: m_big_integer(move(big_integer))
{
ASSERT(!m_big_integer.is_invalid());
}
BigInt::~BigInt()
{
}
BigInt* js_bigint(Heap& heap, Crypto::SignedBigInteger big_integer)
{
return heap.allocate<BigInt>(move(big_integer));
}
BigInt* js_bigint(Interpreter& interpreter, Crypto::SignedBigInteger big_integer)
{
return js_bigint(interpreter.heap(), move(big_integer));
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/Cell.h>
namespace JS {
class BigInt final : public Cell {
public:
BigInt(Crypto::SignedBigInteger);
virtual ~BigInt();
const Crypto::SignedBigInteger& big_integer() const { return m_big_integer; }
const String to_string() const { return String::format("%sn", m_big_integer.to_base10().characters()); }
private:
virtual const char* class_name() const override { return "BigInt"; }
Crypto::SignedBigInteger m_big_integer;
};
BigInt* js_bigint(Heap&, Crypto::SignedBigInteger);
BigInt* js_bigint(Interpreter&, Crypto::SignedBigInteger);
}

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/BigIntConstructor.h>
#include <LibJS/Runtime/BigIntObject.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS {
BigIntConstructor::BigIntConstructor()
: NativeFunction("BigInt", *interpreter().global_object().function_prototype())
{
define_property("prototype", interpreter().global_object().bigint_prototype(), 0);
define_property("length", Value(1), Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("asIntN", as_int_n, 2, attr);
define_native_function("asUintN", as_uint_n, 2, attr);
}
BigIntConstructor::~BigIntConstructor()
{
}
Value BigIntConstructor::call(Interpreter& interpreter)
{
auto primitive = interpreter.argument(0).to_primitive(interpreter, Value::PreferredType::Number);
if (interpreter.exception())
return {};
if (primitive.is_number()) {
if (!primitive.is_integer()) {
interpreter.throw_exception<RangeError>("BigInt argument must be an integer");
return {};
}
return js_bigint(interpreter, Crypto::SignedBigInteger { primitive.as_i32() });
}
auto* bigint = interpreter.argument(0).to_bigint(interpreter);
if (interpreter.exception())
return {};
return bigint;
}
Value BigIntConstructor::construct(Interpreter& interpreter)
{
interpreter.throw_exception<TypeError>("BigInt is not a constructor");
return {};
}
Value BigIntConstructor::as_int_n(Interpreter&)
{
TODO();
}
Value BigIntConstructor::as_uint_n(Interpreter&)
{
TODO();
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
namespace JS {
class BigIntConstructor final : public NativeFunction {
public:
BigIntConstructor();
virtual ~BigIntConstructor() override;
virtual Value call(Interpreter&) override;
virtual Value construct(Interpreter&) override;
private:
virtual bool has_constructor() const override { return true; }
virtual const char* class_name() const override { return "BigIntConstructor"; }
static Value as_int_n(Interpreter&);
static Value as_uint_n(Interpreter&);
};
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/BigIntObject.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS {
BigIntObject* BigIntObject::create(GlobalObject& global_object, BigInt& bigint)
{
return global_object.heap().allocate<BigIntObject>(bigint, *global_object.bigint_prototype());
}
BigIntObject::BigIntObject(BigInt& bigint, Object& prototype)
: Object(&prototype)
, m_bigint(bigint)
{
}
BigIntObject::~BigIntObject()
{
}
void BigIntObject::visit_children(Cell::Visitor& visitor)
{
Object::visit_children(visitor);
visitor.visit(&m_bigint);
}
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
class BigIntObject final : public Object {
public:
static BigIntObject* create(GlobalObject&, BigInt&);
BigIntObject(BigInt&, Object& prototype);
virtual ~BigIntObject();
const BigInt& bigint() const { return m_bigint; }
virtual Value value_of() const override
{
return Value(&m_bigint);
}
private:
virtual void visit_children(Visitor&) override;
virtual const char* class_name() const override { return "BigIntObject"; }
virtual bool is_bigint_object() const override { return true; }
BigInt& m_bigint;
};
}

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/BigIntObject.h>
#include <LibJS/Runtime/BigIntPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS {
BigIntPrototype::BigIntPrototype()
: Object(interpreter().global_object().object_prototype())
{
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("toString", to_string, 0, attr);
define_native_function("toLocaleString", to_locale_string, 0, attr);
define_native_function("valueOf", value_of, 0, attr);
}
BigIntPrototype::~BigIntPrototype()
{
}
static BigIntObject* bigint_object_from(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter);
if (!this_object)
return nullptr;
if (!this_object->is_bigint_object()) {
interpreter.throw_exception<TypeError>("Not a BigInt object");
return nullptr;
}
return static_cast<BigIntObject*>(this_object);
}
Value BigIntPrototype::to_string(Interpreter& interpreter)
{
auto* bigint_object = bigint_object_from(interpreter);
if (!bigint_object)
return {};
return js_string(interpreter, bigint_object->bigint().big_integer().to_base10());
}
Value BigIntPrototype::to_locale_string(Interpreter& interpreter)
{
return to_string(interpreter);
}
Value BigIntPrototype::value_of(Interpreter& interpreter)
{
auto* bigint_object = bigint_object_from(interpreter);
if (!bigint_object)
return {};
return bigint_object->value_of();
}
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibJS/Runtime/Object.h>
namespace JS {
class BigIntPrototype final : public Object {
public:
BigIntPrototype();
virtual ~BigIntPrototype() override;
private:
virtual const char* class_name() const override { return "BigIntPrototype"; }
static Value to_string(Interpreter&);
static Value to_locale_string(Interpreter&);
static Value value_of(Interpreter&);
};
}

View file

@ -29,6 +29,8 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/ArrayConstructor.h>
#include <LibJS/Runtime/ArrayPrototype.h>
#include <LibJS/Runtime/BigIntConstructor.h>
#include <LibJS/Runtime/BigIntPrototype.h>
#include <LibJS/Runtime/BooleanConstructor.h>
#include <LibJS/Runtime/BooleanPrototype.h>
#include <LibJS/Runtime/ConsoleObject.h>
@ -97,6 +99,7 @@ void GlobalObject::initialize()
define_property("Reflect", heap().allocate<ReflectObject>(), attr);
add_constructor("Array", m_array_constructor, *m_array_prototype);
add_constructor("BigInt", m_bigint_constructor, *m_bigint_prototype);
add_constructor("Boolean", m_boolean_constructor, *m_boolean_prototype);
add_constructor("Date", m_date_constructor, *m_date_prototype);
add_constructor("Error", m_error_constructor, *m_error_prototype);

View file

@ -105,6 +105,7 @@ public:
virtual bool is_regexp_object() const { return false; }
virtual bool is_string_object() const { return false; }
virtual bool is_symbol_object() const { return false; }
virtual bool is_bigint_object() const { return false; }
virtual const char* class_name() const override { return "Object"; }
virtual void visit_children(Cell::Visitor&) override;

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,10 +29,14 @@
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibCrypto/NumberTheory/ModularFunctions.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/BigIntObject.h>
#include <LibJS/Runtime/BooleanObject.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Function.h>
@ -42,10 +47,35 @@
#include <LibJS/Runtime/Symbol.h>
#include <LibJS/Runtime/SymbolObject.h>
#include <LibJS/Runtime/Value.h>
#include <ctype.h>
#include <math.h>
namespace JS {
static const Crypto::SignedBigInteger BIGINT_ZERO { 0 };
static bool is_valid_bigint_value(String string)
{
string = string.trim_whitespace();
if (string.length() > 1 && (string[0] == '-' || string[0] == '+'))
string = string.substring_view(1, string.length() - 1);
for (auto& ch : string) {
if (!isdigit(ch))
return false;
}
return true;
}
ALWAYS_INLINE bool both_number(const Value& lhs, const Value& rhs)
{
return lhs.is_number() && rhs.is_number();
}
ALWAYS_INLINE bool both_bigint(const Value& lhs, const Value& rhs)
{
return lhs.is_bigint() && rhs.is_bigint();
}
bool Value::is_array() const
{
return is_object() && as_object().is_array();
@ -83,6 +113,8 @@ String Value::to_string_without_side_effects() const
return m_value.as_string->string();
case Type::Symbol:
return m_value.as_symbol->to_string();
case Type::BigInt:
return m_value.as_bigint->to_string();
case Type::Object:
return String::format("[object %s]", as_object().class_name());
case Type::Accessor:
@ -124,6 +156,8 @@ String Value::to_string(Interpreter& interpreter) const
case Type::Symbol:
interpreter.throw_exception<TypeError>("Can't convert symbol to string");
return {};
case Type::BigInt:
return m_value.as_bigint->big_integer().to_base10();
case Type::Object: {
auto primitive_value = as_object().to_primitive(PreferredType::String);
if (interpreter.exception())
@ -150,6 +184,9 @@ bool Value::to_boolean() const
case Type::String:
return !m_value.as_string->string().is_empty();
case Type::Symbol:
return true;
case Type::BigInt:
return m_value.as_bigint->big_integer() != BIGINT_ZERO;
case Type::Object:
return true;
default:
@ -179,6 +216,8 @@ Object* Value::to_object(Interpreter& interpreter) const
return StringObject::create(interpreter.global_object(), *m_value.as_string);
case Type::Symbol:
return SymbolObject::create(interpreter.global_object(), *m_value.as_symbol);
case Type::BigInt:
return BigIntObject::create(interpreter.global_object(), *m_value.as_bigint);
case Type::Object:
return &const_cast<Object&>(as_object());
default:
@ -187,6 +226,16 @@ Object* Value::to_object(Interpreter& interpreter) const
}
}
Value Value::to_numeric(Interpreter& interpreter) const
{
auto primitive = to_primitive(interpreter, Value::PreferredType::Number);
if (interpreter.exception())
return {};
if (primitive.is_bigint())
return primitive;
return primitive.to_number(interpreter);
}
Value Value::to_number(Interpreter& interpreter) const
{
switch (m_type) {
@ -215,6 +264,9 @@ Value Value::to_number(Interpreter& interpreter) const
case Type::Symbol:
interpreter.throw_exception<TypeError>("Can't convert symbol to number");
return {};
case Type::BigInt:
interpreter.throw_exception<TypeError>("Can't convert BigInt to number");
return {};
case Type::Object: {
auto primitive = m_value.as_object->to_primitive(PreferredType::Number);
if (interpreter.exception())
@ -226,6 +278,43 @@ Value Value::to_number(Interpreter& interpreter) const
}
}
BigInt* Value::to_bigint(Interpreter& interpreter) const
{
auto primitive = to_primitive(interpreter, PreferredType::Number);
if (interpreter.exception())
return nullptr;
switch (primitive.type()) {
case Type::Undefined:
interpreter.throw_exception<TypeError>("Can't convert undefined to BigInt");
return nullptr;
case Type::Null:
interpreter.throw_exception<TypeError>("Can't convert null to BigInt");
return nullptr;
case Type::Boolean: {
auto value = primitive.as_bool() ? 1 : 0;
return js_bigint(interpreter, Crypto::SignedBigInteger { value });
}
case Type::BigInt:
return &primitive.as_bigint();
case Type::Number:
interpreter.throw_exception<TypeError>("Can't convert number to BigInt");
return {};
case Type::String: {
auto& string = primitive.as_string().string();
if (!is_valid_bigint_value(string)) {
interpreter.throw_exception<SyntaxError>(String::format("Invalid value for BigInt: %s", string.characters()));
return {};
}
return js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(string.trim_whitespace()));
}
case Type::Symbol:
interpreter.throw_exception<TypeError>("Can't convert symbol to BigInt");
return {};
default:
ASSERT_NOT_REACHED();
}
}
i32 Value::as_i32() const
{
return static_cast<i32>(as_double());
@ -305,55 +394,70 @@ Value less_than_equals(Interpreter& interpreter, Value lhs, Value rhs)
Value bitwise_and(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value((i32)lhs_number.as_double() & (i32)rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric))
return Value((i32)lhs_numeric.as_double() & (i32)rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use bitwise AND operator with BigInt and other type");
return {};
}
Value bitwise_or(Interpreter& interpreter, Value lhs, Value rhs)
{
bool lhs_invalid = lhs.is_undefined() || lhs.is_null() || lhs.is_nan() || lhs.is_infinity();
bool rhs_invalid = rhs.is_undefined() || rhs.is_null() || rhs.is_nan() || rhs.is_infinity();
if (lhs_invalid && rhs_invalid)
return Value(0);
if (lhs_invalid || rhs_invalid)
return lhs_invalid ? rhs.to_number(interpreter) : lhs.to_number(interpreter);
if (!rhs.is_number() && !lhs.is_number())
return Value(0);
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value((i32)lhs_number.as_double() | (i32)rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric)) {
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;
return Value((i32)lhs_numeric.as_double() | (i32)rhs_numeric.as_double());
}
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_or(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use bitwise OR operator with BigInt and other type");
return {};
}
Value bitwise_xor(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value((i32)lhs_number.as_double() ^ (i32)rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric))
return Value((i32)lhs_numeric.as_double() ^ (i32)rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use bitwise XOR operator with BigInt and other type");
return {};
}
Value bitwise_not(Interpreter& interpreter, Value lhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value(~(i32)lhs_number.as_double());
if (lhs_numeric.is_number())
return Value(~(i32)lhs_numeric.as_double());
auto big_integer_bitwise_not = lhs_numeric.as_bigint().big_integer();
big_integer_bitwise_not = big_integer_bitwise_not.plus(Crypto::SignedBigInteger { 1 });
big_integer_bitwise_not.negate();
return js_bigint(interpreter, big_integer_bitwise_not);
}
Value unary_plus(Interpreter& interpreter, Value lhs)
@ -363,57 +467,80 @@ Value unary_plus(Interpreter& interpreter, Value lhs)
Value unary_minus(Interpreter& interpreter, Value lhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (lhs_number.is_nan())
return js_nan();
return Value(-lhs_number.as_double());
if (lhs_numeric.is_number()) {
if (lhs_numeric.is_nan())
return js_nan();
return Value(-lhs_numeric.as_double());
}
if (lhs_numeric.as_bigint().big_integer() == BIGINT_ZERO)
return js_bigint(interpreter, BIGINT_ZERO);
auto big_integer_negated = lhs_numeric.as_bigint().big_integer();
big_integer_negated.negate();
return js_bigint(interpreter, big_integer_negated);
}
Value left_shift(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (!lhs_number.is_finite_number())
return Value(0);
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (!rhs_number.is_finite_number())
return lhs_number;
return Value((i32)lhs_number.as_double() << (i32)rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number())
return Value(0);
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
return Value((i32)lhs_numeric.as_double() << (i32)rhs_numeric.as_double());
}
if (both_bigint(lhs_numeric, rhs_numeric))
TODO();
interpreter.throw_exception<TypeError>("Can't use left-shift operator with BigInt and other type");
return {};
}
Value right_shift(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (!lhs_number.is_finite_number())
return Value(0);
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (!rhs_number.is_finite_number())
return lhs_number;
return Value((i32)lhs_number.as_double() >> (i32)rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number())
return Value(0);
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
return Value((i32)lhs_numeric.as_double() >> (i32)rhs_numeric.as_double());
}
if (both_bigint(lhs_numeric, rhs_numeric))
TODO();
interpreter.throw_exception<TypeError>("Can't use right-shift operator with BigInt and other type");
return {};
}
Value unsigned_right_shift(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (!lhs_number.is_finite_number())
return Value(0);
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (!rhs_number.is_finite_number())
return lhs_number;
return Value((unsigned)lhs_number.as_double() >> (i32)rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number())
return Value(0);
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
return Value((unsigned)lhs_numeric.as_double() >> (i32)rhs_numeric.as_double());
}
interpreter.throw_exception<TypeError>("Can't use unsigned right-shift operator with BigInt");
return {};
}
Value add(Interpreter& interpreter, Value lhs, Value rhs)
@ -438,73 +565,104 @@ Value add(Interpreter& interpreter, Value lhs, Value rhs)
return js_string(interpreter, builder.to_string());
}
auto lhs_number = lhs_primitive.to_number(interpreter);
auto lhs_numeric = lhs_primitive.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs_primitive.to_number(interpreter);
auto rhs_numeric = rhs_primitive.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() + rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() + rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().plus(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use addition operator with BigInt and other type");
return {};
}
Value sub(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() - rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() - rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().minus(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use subtraction operator with BigInt and other type");
return {};
}
Value mul(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() * rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() * rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().multiplied_by(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use multiplication operator with BigInt and other type");
return {};
}
Value div(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value(lhs_number.as_double() / rhs_number.as_double());
if (both_number(lhs_numeric, rhs_numeric))
return Value(lhs_numeric.as_double() / rhs_numeric.as_double());
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).quotient);
interpreter.throw_exception<TypeError>("Can't use division operator with BigInt and other type");
return {};
}
Value mod(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (lhs_number.is_nan() || rhs_number.is_nan())
return js_nan();
auto index = lhs_number.as_double();
auto period = rhs_number.as_double();
auto trunc = (double)(i32)(index / period);
return Value(index - trunc * period);
if (both_number(lhs_numeric, rhs_numeric)) {
if (lhs_numeric.is_nan() || rhs_numeric.is_nan())
return js_nan();
auto index = lhs_numeric.as_double();
auto period = rhs_numeric.as_double();
auto trunc = (double)(i32)(index / period);
return Value(index - trunc * period);
}
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().divided_by(rhs_numeric.as_bigint().big_integer()).remainder);
interpreter.throw_exception<TypeError>("Can't use modulo operator with BigInt and other type");
return {};
}
Value exp(Interpreter& interpreter, Value lhs, Value rhs)
{
auto lhs_number = lhs.to_number(interpreter);
auto lhs_numeric = lhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto rhs_number = rhs.to_number(interpreter);
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
return Value(pow(lhs_number.as_double(), rhs_number.as_double()));
if (both_number(lhs_numeric, rhs_numeric))
return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, Crypto::NumberTheory::Power(lhs_numeric.as_bigint().big_integer(), rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>("Can't use exponentiation operator with BigInt and other type");
return {};
}
Value in(Interpreter& interpreter, Value lhs, Value rhs)
@ -547,6 +705,14 @@ bool same_value(Interpreter& interpreter, Value lhs, Value rhs)
return lhs.as_double() == rhs.as_double();
}
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;
}
return same_value_non_numeric(interpreter, lhs, rhs);
}
@ -558,17 +724,18 @@ bool same_value_zero(Interpreter& interpreter, Value lhs, Value rhs)
if (lhs.is_number()) {
if (lhs.is_nan() && rhs.is_nan())
return true;
if ((lhs.is_positive_zero() || lhs.is_negative_zero()) && (rhs.is_positive_zero() || rhs.is_negative_zero()))
return true;
return lhs.as_double() == rhs.as_double();
}
if (lhs.is_bigint())
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
return same_value_non_numeric(interpreter, lhs, rhs);
}
bool same_value_non_numeric(Interpreter&, Value lhs, Value rhs)
{
ASSERT(!lhs.is_number());
ASSERT(!lhs.is_number() && !lhs.is_bigint());
ASSERT(lhs.type() == rhs.type());
switch (lhs.type()) {
@ -598,11 +765,12 @@ bool strict_eq(Interpreter& interpreter, Value lhs, Value rhs)
return false;
if (lhs.as_double() == rhs.as_double())
return true;
if ((lhs.is_positive_zero() || lhs.is_negative_zero()) && (rhs.is_positive_zero() || rhs.is_negative_zero()))
return true;
return false;
}
if (lhs.is_bigint())
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
return same_value_non_numeric(interpreter, lhs, rhs);
}
@ -620,25 +788,46 @@ bool abstract_eq(Interpreter& interpreter, Value lhs, Value rhs)
if (lhs.is_string() && rhs.is_number())
return abstract_eq(interpreter, lhs.to_number(interpreter), rhs);
if (lhs.is_bigint() && rhs.is_string()) {
auto& rhs_string = rhs.as_string().string();
if (!is_valid_bigint_value(rhs_string))
return false;
return abstract_eq(interpreter, lhs, js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(rhs_string)));
}
if (lhs.is_string() && rhs.is_bigint())
return abstract_eq(interpreter, rhs, lhs);
if (lhs.is_boolean())
return abstract_eq(interpreter, lhs.to_number(interpreter), rhs);
if (rhs.is_boolean())
return abstract_eq(interpreter, lhs, rhs.to_number(interpreter));
if ((lhs.is_string() || lhs.is_number() || lhs.is_symbol()) && rhs.is_object())
if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object())
return abstract_eq(interpreter, lhs, rhs.to_primitive(interpreter));
if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_symbol()))
if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol()))
return abstract_eq(interpreter, lhs.to_primitive(interpreter), rhs);
if ((lhs.is_bigint() && rhs.is_number()) || (lhs.is_number() && rhs.is_bigint())) {
if (lhs.is_nan() || lhs.is_infinity() || rhs.is_nan() || rhs.is_infinity())
return false;
if ((lhs.is_number() && !lhs.is_integer()) || (rhs.is_number() && !rhs.is_integer()))
return false;
if (lhs.is_number())
return Crypto::SignedBigInteger { lhs.as_i32() } == rhs.as_bigint().big_integer();
else
return Crypto::SignedBigInteger { rhs.as_i32() } == lhs.as_bigint().big_integer();
}
return false;
}
TriState abstract_relation(Interpreter& interpreter, bool left_first, Value lhs, Value rhs)
{
Value x_primitive = {};
Value y_primitive = {};
Value x_primitive;
Value y_primitive;
if (left_first) {
x_primitive = lhs.to_primitive(interpreter, Value::PreferredType::Number);
@ -681,12 +870,30 @@ TriState abstract_relation(Interpreter& interpreter, bool left_first, Value lhs,
ASSERT_NOT_REACHED();
}
// FIXME add BigInt cases here once we have BigInt
if (x_primitive.is_bigint() && y_primitive.is_string()) {
auto& y_string = y_primitive.as_string().string();
if (!is_valid_bigint_value(y_string))
return TriState::Unknown;
if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
return TriState::True;
else
return TriState::False;
}
auto x_numeric = x_primitive.to_number(interpreter);
if (x_primitive.is_string() && y_primitive.is_bigint()) {
auto& x_string = x_primitive.as_string().string();
if (!is_valid_bigint_value(x_string))
return TriState::Unknown;
if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
return TriState::True;
else
return TriState::False;
}
auto x_numeric = x_primitive.to_numeric(interpreter);
if (interpreter.exception())
return {};
auto y_numeric = y_primitive.to_number(interpreter);
auto y_numeric = y_primitive.to_numeric(interpreter);
if (interpreter.exception())
return {};
@ -699,10 +906,33 @@ TriState abstract_relation(Interpreter& interpreter, bool left_first, Value lhs,
if (x_numeric.is_negative_infinity() || y_numeric.is_positive_infinity())
return TriState::True;
auto x_value = x_numeric.as_double();
auto y_value = y_numeric.as_double();
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;
}
if (x_value < y_value)
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;
}
ASSERT((x_numeric.is_number() && y_numeric.is_bigint()) || (x_numeric.is_bigint() && y_numeric.is_number()));
bool x_lower_than_y;
if (x_numeric.is_number()) {
x_lower_than_y = x_numeric.is_integer()
? Crypto::SignedBigInteger { x_numeric.as_i32() } < y_numeric.as_bigint().big_integer()
: (Crypto::SignedBigInteger { x_numeric.as_i32() } < y_numeric.as_bigint().big_integer() || Crypto::SignedBigInteger { x_numeric.as_i32() + 1 } < y_numeric.as_bigint().big_integer());
} else {
x_lower_than_y = y_numeric.is_integer()
? x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() }
: (x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() } || x_numeric.as_bigint().big_integer() < Crypto::SignedBigInteger { y_numeric.as_i32() + 1 });
}
if (x_lower_than_y)
return TriState::True;
else
return TriState::False;

View file

@ -31,7 +31,6 @@
#include <AK/LogStream.h>
#include <AK/Types.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Symbol.h>
#include <math.h>
// 2 ** 53 - 1
@ -51,6 +50,7 @@ public:
Boolean,
Symbol,
Accessor,
BigInt,
};
enum class PreferredType {
@ -68,6 +68,7 @@ public:
bool is_boolean() const { return m_type == Type::Boolean; }
bool is_symbol() const { return m_type == Type::Symbol; }
bool is_accessor() const { return m_type == Type::Accessor; };
bool is_bigint() const { return m_type == Type::BigInt; };
bool is_cell() const { return is_string() || is_accessor() || is_object(); }
bool is_array() const;
bool is_function() const;
@ -140,6 +141,12 @@ public:
m_value.as_accessor = accessor;
}
Value(BigInt* bigint)
: m_type(Type::BigInt)
{
m_value.as_bigint = bigint;
}
explicit Value(Type type)
: m_type(type)
{
@ -207,6 +214,12 @@ public:
return *m_value.as_accessor;
}
BigInt& as_bigint()
{
ASSERT(is_bigint());
return *m_value.as_bigint;
}
Function& as_function();
i32 as_i32() const;
@ -216,7 +229,9 @@ public:
PrimitiveString* to_primitive_string(Interpreter&);
Value to_primitive(Interpreter&, PreferredType preferred_type = PreferredType::Default) const;
Object* to_object(Interpreter&) const;
Value to_numeric(Interpreter&) const;
Value to_number(Interpreter&) const;
BigInt* to_bigint(Interpreter&) const;
double to_double(Interpreter&) const;
i32 to_i32(Interpreter&) const;
size_t to_size_t(Interpreter&) const;
@ -242,6 +257,7 @@ private:
Object* as_object;
Cell* as_cell;
Accessor* as_accessor;
BigInt* as_bigint;
} m_value;
};