2020-03-07 18:42:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-06-06 00:14:10 +00:00
|
|
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
2020-03-07 18:42:11 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-28 15:56:54 +00:00
|
|
|
#include <AK/FlyString.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
#include <AK/String.h>
|
2020-05-15 11:39:24 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-05-28 15:19:59 +00:00
|
|
|
#include <AK/Utf8View.h>
|
2020-06-06 00:14:10 +00:00
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
|
|
|
#include <LibCrypto/NumberTheory/ModularFunctions.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-03-28 21:48:35 +00:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-05-21 18:14:23 +00:00
|
|
|
#include <LibJS/Runtime/Accessor.h>
|
2020-03-29 14:20:09 +00:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-06-06 00:14:10 +00:00
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
|
|
|
#include <LibJS/Runtime/BigIntObject.h>
|
2020-04-07 03:51:16 +00:00
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
2020-03-28 21:48:35 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-05-06 10:52:53 +00:00
|
|
|
#include <LibJS/Runtime/Function.h>
|
2020-04-04 21:13:13 +00:00
|
|
|
#include <LibJS/Runtime/NumberObject.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2020-05-17 23:28:00 +00:00
|
|
|
#include <LibJS/Runtime/Symbol.h>
|
2020-04-30 06:25:21 +00:00
|
|
|
#include <LibJS/Runtime/SymbolObject.h>
|
2020-03-16 13:20:30 +00:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-06-06 00:14:10 +00:00
|
|
|
#include <ctype.h>
|
2020-04-05 12:40:00 +00:00
|
|
|
#include <math.h>
|
2020-03-07 18:42:11 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-03-26 11:02:18 +00:00
|
|
|
bool Value::is_array() const
|
|
|
|
{
|
2020-04-01 20:18:47 +00:00
|
|
|
return is_object() && as_object().is_array();
|
2020-03-26 11:02:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:52:53 +00:00
|
|
|
bool Value::is_function() const
|
|
|
|
{
|
|
|
|
return is_object() && as_object().is_function();
|
|
|
|
}
|
|
|
|
|
|
|
|
Function& Value::as_function()
|
|
|
|
{
|
|
|
|
ASSERT(is_function());
|
|
|
|
return static_cast<Function&>(as_object());
|
|
|
|
}
|
|
|
|
|
2020-05-15 11:39:24 +00:00
|
|
|
String Value::to_string_without_side_effects() const
|
2020-03-07 18:42:11 +00:00
|
|
|
{
|
2020-06-04 20:36:52 +00:00
|
|
|
switch (m_type) {
|
|
|
|
case Type::Undefined:
|
2020-03-07 22:11:17 +00:00
|
|
|
return "undefined";
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Null:
|
|
|
|
return "null";
|
|
|
|
case Type::Boolean:
|
|
|
|
return m_value.as_bool ? "true" : "false";
|
|
|
|
case Type::Number:
|
2020-03-27 11:26:37 +00:00
|
|
|
if (is_nan())
|
|
|
|
return "NaN";
|
2020-04-02 15:59:01 +00:00
|
|
|
if (is_infinity())
|
2020-06-04 20:36:52 +00:00
|
|
|
return is_negative_infinity() ? "-Infinity" : "Infinity";
|
2020-05-15 11:39:24 +00:00
|
|
|
if (is_integer())
|
2020-05-18 07:59:35 +00:00
|
|
|
return String::number(as_i32());
|
2020-06-04 20:36:52 +00:00
|
|
|
return String::format("%.4f", m_value.as_double);
|
|
|
|
case Type::String:
|
2020-05-15 11:39:24 +00:00
|
|
|
return m_value.as_string->string();
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Symbol:
|
|
|
|
return m_value.as_symbol->to_string();
|
2020-06-06 00:14:10 +00:00
|
|
|
case Type::BigInt:
|
|
|
|
return m_value.as_bigint->to_string();
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Object:
|
|
|
|
return String::format("[object %s]", as_object().class_name());
|
|
|
|
case Type::Accessor:
|
2020-05-21 18:14:23 +00:00
|
|
|
return "<accessor>";
|
2020-06-04 20:36:52 +00:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2020-05-15 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
PrimitiveString* Value::to_primitive_string(Interpreter& interpreter)
|
2020-05-15 11:39:24 +00:00
|
|
|
{
|
|
|
|
if (is_string())
|
|
|
|
return &as_string();
|
|
|
|
auto string = to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return nullptr;
|
|
|
|
return js_string(interpreter, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
String Value::to_string(Interpreter& interpreter) const
|
|
|
|
{
|
2020-06-04 20:36:52 +00:00
|
|
|
switch (m_type) {
|
|
|
|
case Type::Undefined:
|
2020-05-15 11:39:24 +00:00
|
|
|
return "undefined";
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Null:
|
|
|
|
return "null";
|
|
|
|
case Type::Boolean:
|
|
|
|
return m_value.as_bool ? "true" : "false";
|
|
|
|
case Type::Number:
|
2020-05-15 11:39:24 +00:00
|
|
|
if (is_nan())
|
|
|
|
return "NaN";
|
|
|
|
if (is_infinity())
|
2020-06-04 20:36:52 +00:00
|
|
|
return is_negative_infinity() ? "-Infinity" : "Infinity";
|
2020-05-15 11:39:24 +00:00
|
|
|
if (is_integer())
|
2020-05-18 07:59:35 +00:00
|
|
|
return String::number(as_i32());
|
2020-06-04 20:36:52 +00:00
|
|
|
return String::format("%.4f", m_value.as_double);
|
|
|
|
case Type::String:
|
|
|
|
return m_value.as_string->string();
|
|
|
|
case Type::Symbol:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "symbol", "string");
|
2020-04-30 06:25:21 +00:00
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
case Type::BigInt:
|
|
|
|
return m_value.as_bigint->big_integer().to_base10();
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Object: {
|
2020-05-28 15:19:59 +00:00
|
|
|
auto primitive_value = as_object().to_primitive(PreferredType::String);
|
2020-05-17 23:56:44 +00:00
|
|
|
if (interpreter.exception())
|
2020-04-29 15:29:26 +00:00
|
|
|
return {};
|
2020-05-15 11:39:24 +00:00
|
|
|
return primitive_value.to_string(interpreter);
|
2020-04-29 15:29:26 +00:00
|
|
|
}
|
2020-06-04 20:36:52 +00:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 07:46:20 +00:00
|
|
|
bool Value::to_boolean() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Undefined:
|
|
|
|
case Type::Null:
|
|
|
|
return false;
|
2020-03-10 07:46:20 +00:00
|
|
|
case Type::Boolean:
|
|
|
|
return m_value.as_bool;
|
|
|
|
case Type::Number:
|
2020-06-04 20:36:52 +00:00
|
|
|
if (is_nan())
|
2020-04-07 03:48:47 +00:00
|
|
|
return false;
|
2020-06-04 20:36:52 +00:00
|
|
|
return m_value.as_double != 0;
|
2020-03-10 07:46:20 +00:00
|
|
|
case Type::String:
|
2020-06-04 20:36:52 +00:00
|
|
|
return !m_value.as_string->string().is_empty();
|
2020-04-30 06:25:21 +00:00
|
|
|
case Type::Symbol:
|
2020-06-06 00:14:10 +00:00
|
|
|
return true;
|
|
|
|
case Type::BigInt:
|
|
|
|
return m_value.as_bigint->big_integer() != BIGINT_ZERO;
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Object:
|
2020-03-10 07:46:20 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:19:59 +00:00
|
|
|
Value Value::to_primitive(Interpreter&, PreferredType preferred_type) const
|
2020-04-15 07:32:44 +00:00
|
|
|
{
|
|
|
|
if (is_object())
|
2020-05-28 15:19:59 +00:00
|
|
|
return as_object().to_primitive(preferred_type);
|
2020-04-15 07:32:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-17 20:38:47 +00:00
|
|
|
Object* Value::to_object(Interpreter& interpreter) const
|
2020-03-11 17:58:19 +00:00
|
|
|
{
|
2020-06-04 20:36:52 +00:00
|
|
|
switch (m_type) {
|
|
|
|
case Type::Undefined:
|
|
|
|
case Type::Null:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::ToObjectNullOrUndef);
|
2020-03-28 21:48:35 +00:00
|
|
|
return nullptr;
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Boolean:
|
|
|
|
return BooleanObject::create(interpreter.global_object(), m_value.as_bool);
|
|
|
|
case Type::Number:
|
|
|
|
return NumberObject::create(interpreter.global_object(), m_value.as_double);
|
|
|
|
case Type::String:
|
|
|
|
return StringObject::create(interpreter.global_object(), *m_value.as_string);
|
|
|
|
case Type::Symbol:
|
|
|
|
return SymbolObject::create(interpreter.global_object(), *m_value.as_symbol);
|
2020-06-06 00:14:10 +00:00
|
|
|
case Type::BigInt:
|
|
|
|
return BigIntObject::create(interpreter.global_object(), *m_value.as_bigint);
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Object:
|
|
|
|
return &const_cast<Object&>(as_object());
|
|
|
|
default:
|
|
|
|
dbg() << "Dying because I can't to_object() on " << *this;
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-28 21:48:35 +00:00
|
|
|
}
|
2020-03-11 17:58:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value Value::to_number(Interpreter& interpreter) const
|
2020-03-15 14:00:18 +00:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
2020-05-08 00:09:00 +00:00
|
|
|
case Type::Undefined:
|
|
|
|
return js_nan();
|
|
|
|
case Type::Null:
|
|
|
|
return Value(0);
|
2020-03-15 14:00:18 +00:00
|
|
|
case Type::Boolean:
|
2020-03-15 22:19:41 +00:00
|
|
|
return Value(m_value.as_bool ? 1 : 0);
|
2020-03-15 14:00:18 +00:00
|
|
|
case Type::Number:
|
2020-03-15 22:19:41 +00:00
|
|
|
return Value(m_value.as_double);
|
|
|
|
case Type::String: {
|
2020-05-12 23:28:42 +00:00
|
|
|
auto string = as_string().string().trim_whitespace();
|
2020-03-29 12:56:28 +00:00
|
|
|
if (string.is_empty())
|
|
|
|
return Value(0);
|
2020-04-12 12:06:34 +00:00
|
|
|
if (string == "Infinity" || string == "+Infinity")
|
|
|
|
return js_infinity();
|
|
|
|
if (string == "-Infinity")
|
2020-04-12 12:25:42 +00:00
|
|
|
return js_negative_infinity();
|
2020-05-12 23:04:52 +00:00
|
|
|
char* endptr;
|
|
|
|
auto parsed_double = strtod(string.characters(), &endptr);
|
|
|
|
if (*endptr)
|
|
|
|
return js_nan();
|
|
|
|
return Value(parsed_double);
|
2020-03-15 14:00:18 +00:00
|
|
|
}
|
2020-04-30 06:25:21 +00:00
|
|
|
case Type::Symbol:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "symbol", "number");
|
2020-05-17 23:54:10 +00:00
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
case Type::BigInt:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "BigInt", "number");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-06-04 20:36:52 +00:00
|
|
|
case Type::Object: {
|
2020-05-28 15:19:59 +00:00
|
|
|
auto primitive = m_value.as_object->to_primitive(PreferredType::Number);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
return primitive.to_number(interpreter);
|
2020-03-15 22:19:41 +00:00
|
|
|
}
|
2020-06-04 20:36:52 +00:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2020-03-15 22:19:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "undefined", "BigInt");
|
2020-06-06 00:14:10 +00:00
|
|
|
return nullptr;
|
|
|
|
case Type::Null:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "null", "BigInt");
|
2020-06-06 00:14:10 +00:00
|
|
|
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:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "number", "BigInt");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
|
|
|
case Type::String: {
|
|
|
|
auto& string = primitive.as_string().string();
|
|
|
|
if (!is_valid_bigint_value(string)) {
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<SyntaxError>(ErrorType::BigIntInvalidValue, string.characters());
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return js_bigint(interpreter, Crypto::SignedBigInteger::from_base10(string.trim_whitespace()));
|
|
|
|
}
|
|
|
|
case Type::Symbol:
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::Convert, "symbol", "BigInt");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 07:59:35 +00:00
|
|
|
i32 Value::as_i32() const
|
2020-05-17 23:28:00 +00:00
|
|
|
{
|
2020-05-18 07:59:35 +00:00
|
|
|
return static_cast<i32>(as_double());
|
2020-05-17 23:28:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:59:35 +00:00
|
|
|
size_t Value::as_size_t() const
|
2020-03-15 22:19:41 +00:00
|
|
|
{
|
2020-05-18 07:59:35 +00:00
|
|
|
ASSERT(as_double() >= 0);
|
2020-05-22 12:20:53 +00:00
|
|
|
return min((double)as_i32(), MAX_ARRAY_LIKE_INDEX);
|
2020-03-15 14:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:59:35 +00:00
|
|
|
double Value::to_double(Interpreter& interpreter) const
|
2020-04-08 09:22:20 +00:00
|
|
|
{
|
2020-05-17 23:28:00 +00:00
|
|
|
auto number = to_number(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return 0;
|
2020-05-18 07:59:35 +00:00
|
|
|
return number.as_double();
|
2020-04-08 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:59:35 +00:00
|
|
|
i32 Value::to_i32(Interpreter& interpreter) const
|
2020-05-17 23:28:00 +00:00
|
|
|
{
|
2020-05-18 07:59:35 +00:00
|
|
|
auto number = to_number(interpreter);
|
|
|
|
if (interpreter.exception())
|
2020-05-17 23:28:00 +00:00
|
|
|
return 0;
|
2020-05-23 14:02:14 +00:00
|
|
|
if (number.is_nan())
|
|
|
|
return 0;
|
|
|
|
// FIXME: What about infinity though - that's UB...
|
|
|
|
// Maybe NumericLimits<i32>::max() for +Infinity and NumericLimits<i32>::min() for -Infinity?
|
2020-05-18 07:59:35 +00:00
|
|
|
return number.as_i32();
|
2020-05-17 23:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Value::to_size_t(Interpreter& interpreter) const
|
2020-04-30 21:41:30 +00:00
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return 0;
|
2020-05-17 23:28:00 +00:00
|
|
|
auto number = to_number(interpreter);
|
|
|
|
if (interpreter.exception())
|
2020-04-30 21:41:30 +00:00
|
|
|
return 0;
|
2020-05-18 09:03:08 +00:00
|
|
|
if (number.is_nan())
|
|
|
|
return 0;
|
|
|
|
if (number.as_double() <= 0)
|
2020-05-18 07:59:35 +00:00
|
|
|
return 0;
|
|
|
|
return number.as_size_t();
|
2020-04-30 21:41:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value greater_than(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-05-28 15:19:59 +00:00
|
|
|
TriState relation = abstract_relation(interpreter, false, lhs, rhs);
|
|
|
|
if (relation == TriState::Unknown)
|
|
|
|
return Value(false);
|
|
|
|
return Value(relation == TriState::True);
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value greater_than_equals(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-12 12:07:08 +00:00
|
|
|
{
|
2020-05-28 15:19:59 +00:00
|
|
|
TriState relation = abstract_relation(interpreter, true, lhs, rhs);
|
|
|
|
if (relation == TriState::Unknown || relation == TriState::True)
|
|
|
|
return Value(false);
|
|
|
|
return Value(true);
|
2020-03-12 12:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value less_than(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-05-28 15:19:59 +00:00
|
|
|
TriState relation = abstract_relation(interpreter, true, lhs, rhs);
|
|
|
|
if (relation == TriState::Unknown)
|
|
|
|
return Value(false);
|
|
|
|
return Value(relation == TriState::True);
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value less_than_equals(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-12 12:07:08 +00:00
|
|
|
{
|
2020-05-28 15:19:59 +00:00
|
|
|
TriState relation = abstract_relation(interpreter, false, lhs, rhs);
|
|
|
|
if (relation == TriState::Unknown || relation == TriState::True)
|
|
|
|
return Value(false);
|
|
|
|
return Value(true);
|
2020-03-12 12:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value bitwise_and(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value bitwise_or(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise OR");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value bitwise_xor(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value bitwise_not(Interpreter& interpreter, Value lhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value unary_plus(Interpreter& interpreter, Value lhs)
|
2020-04-02 16:58:39 +00:00
|
|
|
{
|
2020-05-17 23:28:00 +00:00
|
|
|
return lhs.to_number(interpreter);
|
2020-04-02 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value unary_minus(Interpreter& interpreter, Value lhs)
|
2020-04-02 16:58:39 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
2020-04-02 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value left_shift(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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();
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "left-shift");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value right_shift(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-10 10:35:05 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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();
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "right-shift");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-10 10:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value unsigned_right_shift(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-04-23 14:43:10 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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());
|
|
|
|
}
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperator, "unsigned right-shift");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-04-23 14:43:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 07:28:41 +00:00
|
|
|
Value add(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-11 11:16:26 +00:00
|
|
|
{
|
2020-04-15 07:32:44 +00:00
|
|
|
auto lhs_primitive = lhs.to_primitive(interpreter);
|
2020-05-15 11:39:24 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-15 07:32:44 +00:00
|
|
|
auto rhs_primitive = rhs.to_primitive(interpreter);
|
2020-05-15 11:39:24 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-04-15 07:32:44 +00:00
|
|
|
|
2020-05-15 11:39:24 +00:00
|
|
|
if (lhs_primitive.is_string() || rhs_primitive.is_string()) {
|
|
|
|
auto lhs_string = lhs_primitive.to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
auto rhs_string = rhs_primitive.to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
StringBuilder builder(lhs_string.length() + rhs_string.length());
|
|
|
|
builder.append(lhs_string);
|
|
|
|
builder.append(rhs_string);
|
|
|
|
return js_string(interpreter, builder.to_string());
|
|
|
|
}
|
2020-03-15 22:19:41 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs_primitive.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs_primitive.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "addition");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-11 11:16:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value sub(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-11 11:16:26 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "subtraction");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-11 11:16:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value mul(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-12 12:04:52 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "multiplication");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-12 12:04:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value div(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-12 12:04:52 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "division");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-03-12 12:04:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value mod(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-04-04 19:17:34 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "modulo");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-04-04 19:17:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:28:00 +00:00
|
|
|
Value exp(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-04-05 12:40:00 +00:00
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
auto lhs_numeric = lhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto rhs_numeric = rhs.to_numeric(interpreter);
|
2020-05-17 23:28:00 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
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()));
|
2020-06-10 05:48:01 +00:00
|
|
|
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "exponentiation");
|
2020-06-06 00:14:10 +00:00
|
|
|
return {};
|
2020-04-05 12:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
Value in(Interpreter& interpreter, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
if (!rhs.is_object())
|
2020-06-10 05:48:01 +00:00
|
|
|
return interpreter.throw_exception<TypeError>(ErrorType::InOperatorWithObject);
|
2020-05-15 11:39:24 +00:00
|
|
|
auto lhs_string = lhs.to_string(interpreter);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-03 21:27:01 +00:00
|
|
|
return Value(rhs.as_object().has_property(lhs_string));
|
2020-05-08 00:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:53:14 +00:00
|
|
|
Value instance_of(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-11 11:16:26 +00:00
|
|
|
{
|
2020-05-08 00:09:00 +00:00
|
|
|
if (!lhs.is_object() || !rhs.is_object())
|
|
|
|
return Value(false);
|
|
|
|
auto constructor_prototype_property = rhs.as_object().get("prototype");
|
2020-06-07 17:53:14 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-05-08 00:09:00 +00:00
|
|
|
if (!constructor_prototype_property.is_object())
|
2020-03-11 11:16:26 +00:00
|
|
|
return Value(false);
|
2020-05-08 00:09:00 +00:00
|
|
|
return Value(lhs.as_object().has_prototype(&constructor_prototype_property.as_object()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const LogStream& operator<<(const LogStream& stream, const Value& value)
|
|
|
|
{
|
2020-05-15 11:39:24 +00:00
|
|
|
return stream << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
|
2020-05-08 00:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool same_value(Interpreter& interpreter, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
if (lhs.type() != rhs.type())
|
|
|
|
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-17 23:28:00 +00:00
|
|
|
return lhs.as_double() == rhs.as_double();
|
2020-05-08 00:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 00:14:10 +00: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-05-08 00:09:00 +00:00
|
|
|
return same_value_non_numeric(interpreter, lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool same_value_zero(Interpreter& interpreter, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
if (lhs.type() != rhs.type())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lhs.is_number()) {
|
|
|
|
if (lhs.is_nan() && rhs.is_nan())
|
|
|
|
return true;
|
2020-05-17 23:28:00 +00:00
|
|
|
return lhs.as_double() == rhs.as_double();
|
2020-05-08 00:09:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
if (lhs.is_bigint())
|
|
|
|
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
|
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
return same_value_non_numeric(interpreter, lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool same_value_non_numeric(Interpreter&, Value lhs, Value rhs)
|
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
ASSERT(!lhs.is_number() && !lhs.is_bigint());
|
2020-05-08 00:09:00 +00:00
|
|
|
ASSERT(lhs.type() == rhs.type());
|
|
|
|
|
2020-03-11 11:16:26 +00:00
|
|
|
switch (lhs.type()) {
|
|
|
|
case Value::Type::Undefined:
|
|
|
|
case Value::Type::Null:
|
2020-05-08 00:09:00 +00:00
|
|
|
return true;
|
2020-03-11 11:16:26 +00:00
|
|
|
case Value::Type::String:
|
2020-05-08 00:09:00 +00:00
|
|
|
return lhs.as_string().string() == rhs.as_string().string();
|
2020-04-30 06:25:21 +00:00
|
|
|
case Value::Type::Symbol:
|
|
|
|
return &lhs.as_symbol() == &rhs.as_symbol();
|
2020-03-11 11:16:26 +00:00
|
|
|
case Value::Type::Boolean:
|
2020-05-08 00:09:00 +00:00
|
|
|
return lhs.as_bool() == rhs.as_bool();
|
2020-03-11 11:16:26 +00:00
|
|
|
case Value::Type::Object:
|
2020-05-08 00:09:00 +00:00
|
|
|
return &lhs.as_object() == &rhs.as_object();
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-11 11:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
bool strict_eq(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-15 22:23:38 +00:00
|
|
|
{
|
2020-05-08 00:09:00 +00:00
|
|
|
if (lhs.type() != rhs.type())
|
|
|
|
return false;
|
2020-03-15 22:23:38 +00:00
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
if (lhs.is_number()) {
|
|
|
|
if (lhs.is_nan() || rhs.is_nan())
|
|
|
|
return false;
|
2020-05-17 23:28:00 +00:00
|
|
|
if (lhs.as_double() == rhs.as_double())
|
2020-05-08 00:09:00 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-15 22:23:38 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
if (lhs.is_bigint())
|
|
|
|
return lhs.as_bigint().big_integer() == rhs.as_bigint().big_integer();
|
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
return same_value_non_numeric(interpreter, lhs, rhs);
|
|
|
|
}
|
2020-03-15 22:23:38 +00:00
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
bool abstract_eq(Interpreter& interpreter, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
if (lhs.type() == rhs.type())
|
|
|
|
return strict_eq(interpreter, lhs, rhs);
|
2020-03-15 22:23:38 +00:00
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null()))
|
|
|
|
return true;
|
2020-03-15 22:23:38 +00:00
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
if (lhs.is_number() && rhs.is_string())
|
2020-05-17 23:28:00 +00:00
|
|
|
return abstract_eq(interpreter, lhs, rhs.to_number(interpreter));
|
2020-03-15 22:23:38 +00:00
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
if (lhs.is_string() && rhs.is_number())
|
2020-05-17 23:28:00 +00:00
|
|
|
return abstract_eq(interpreter, lhs.to_number(interpreter), rhs);
|
2020-04-23 15:06:01 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
if (lhs.is_boolean())
|
2020-05-17 23:28:00 +00:00
|
|
|
return abstract_eq(interpreter, lhs.to_number(interpreter), rhs);
|
2020-04-23 15:06:01 +00:00
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
if (rhs.is_boolean())
|
2020-05-17 23:28:00 +00:00
|
|
|
return abstract_eq(interpreter, lhs, rhs.to_number(interpreter));
|
2020-03-28 15:56:54 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object())
|
2020-05-08 00:09:00 +00:00
|
|
|
return abstract_eq(interpreter, lhs, rhs.to_primitive(interpreter));
|
2020-03-28 15:56:54 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || lhs.is_bigint() || rhs.is_symbol()))
|
2020-05-08 00:09:00 +00:00
|
|
|
return abstract_eq(interpreter, lhs.to_primitive(interpreter), rhs);
|
2020-03-28 15:56:54 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:09:00 +00:00
|
|
|
return false;
|
2020-03-07 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:19:59 +00:00
|
|
|
TriState abstract_relation(Interpreter& interpreter, bool left_first, Value lhs, Value rhs)
|
|
|
|
{
|
2020-06-06 00:14:10 +00:00
|
|
|
Value x_primitive;
|
|
|
|
Value y_primitive;
|
2020-05-28 15:19:59 +00:00
|
|
|
|
|
|
|
if (left_first) {
|
|
|
|
x_primitive = lhs.to_primitive(interpreter, Value::PreferredType::Number);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
y_primitive = rhs.to_primitive(interpreter, Value::PreferredType::Number);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
y_primitive = lhs.to_primitive(interpreter, Value::PreferredType::Number);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
x_primitive = rhs.to_primitive(interpreter, Value::PreferredType::Number);
|
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x_primitive.is_string() && y_primitive.is_string()) {
|
|
|
|
auto x_string = x_primitive.as_string().string();
|
|
|
|
auto y_string = y_primitive.as_string().string();
|
|
|
|
|
|
|
|
if (x_string.starts_with(y_string))
|
|
|
|
return TriState::False;
|
|
|
|
if (y_string.starts_with(x_string))
|
|
|
|
return TriState::True;
|
|
|
|
|
|
|
|
Utf8View x_codepoints { x_string };
|
|
|
|
Utf8View y_codepoints { y_string };
|
|
|
|
for (auto k = x_codepoints.begin(), l = y_codepoints.begin();
|
|
|
|
k != x_codepoints.end() && l != y_codepoints.end();
|
|
|
|
++k, ++l) {
|
|
|
|
if (*k != *l) {
|
|
|
|
if (*k < *l) {
|
|
|
|
return TriState::True;
|
|
|
|
} else {
|
|
|
|
return TriState::False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-05-28 15:19:59 +00:00
|
|
|
|
2020-06-06 00:14:10 +00:00
|
|
|
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);
|
2020-05-28 15:19:59 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
2020-06-06 00:14:10 +00:00
|
|
|
auto y_numeric = y_primitive.to_numeric(interpreter);
|
2020-05-28 15:19:59 +00:00
|
|
|
if (interpreter.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
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 00:14:10 +00: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 15:19:59 +00:00
|
|
|
|
2020-06-06 00:14:10 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-05-28 15:19:59 +00:00
|
|
|
return TriState::True;
|
|
|
|
else
|
|
|
|
return TriState::False;
|
|
|
|
}
|
|
|
|
|
2020-03-07 18:42:11 +00:00
|
|
|
}
|