2020-03-07 19:42:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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 16:56:54 +01:00
|
|
|
#include <AK/FlyString.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
#include <AK/String.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Heap/Heap.h>
|
2020-03-28 22:48:35 +01:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-29 15:20:09 +01:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-04-06 22:51:16 -05:00
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
2020-03-28 22:48:35 +01:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-05-06 11:52:53 +01:00
|
|
|
#include <LibJS/Runtime/Function.h>
|
2020-04-04 23:13:13 +02:00
|
|
|
#include <LibJS/Runtime/NumberObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-04-05 13:40:00 +01:00
|
|
|
#include <math.h>
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-05-01 11:06:27 +01:00
|
|
|
// 2 ** 53 - 1
|
|
|
|
#define MAX_ARRAY_LIKE_INDEX 9007199254740991.0
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
namespace JS {
|
|
|
|
|
2020-03-26 12:02:18 +01:00
|
|
|
bool Value::is_array() const
|
|
|
|
{
|
2020-04-01 22:18:47 +02:00
|
|
|
return is_object() && as_object().is_array();
|
2020-03-26 12:02:18 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 11:52:53 +01: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-03-07 19:42:11 +01:00
|
|
|
String Value::to_string() const
|
|
|
|
{
|
|
|
|
if (is_boolean())
|
|
|
|
return as_bool() ? "true" : "false";
|
|
|
|
|
|
|
|
if (is_null())
|
|
|
|
return "null";
|
|
|
|
|
|
|
|
if (is_undefined())
|
2020-03-07 23:11:17 +01:00
|
|
|
return "undefined";
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-27 12:26:37 +01:00
|
|
|
if (is_number()) {
|
|
|
|
if (is_nan())
|
|
|
|
return "NaN";
|
|
|
|
|
2020-04-02 16:59:01 +01:00
|
|
|
if (is_infinity())
|
|
|
|
return as_double() < 0 ? "-Infinity" : "Infinity";
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
// FIXME: This needs improvement.
|
2020-03-31 19:06:10 +02:00
|
|
|
if ((double)to_i32() == as_double())
|
|
|
|
return String::number(to_i32());
|
2020-04-12 10:59:29 +02:00
|
|
|
return String::format("%.4f", as_double());
|
2020-03-27 12:26:37 +01:00
|
|
|
}
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-04-29 16:29:26 +01:00
|
|
|
if (is_object()) {
|
|
|
|
auto primitive_value = as_object().to_primitive(Object::PreferredType::String);
|
|
|
|
// FIXME: Maybe we should pass in the Interpreter& and call interpreter.exception() instead?
|
|
|
|
if (primitive_value.is_empty())
|
|
|
|
return {};
|
|
|
|
return primitive_value.to_string();
|
|
|
|
}
|
2020-03-07 19:42:11 +01:00
|
|
|
|
2020-03-11 18:58:19 +01:00
|
|
|
if (is_string())
|
|
|
|
return m_value.as_string->string();
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-03-10 08:46:20 +01:00
|
|
|
bool Value::to_boolean() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Boolean:
|
|
|
|
return m_value.as_bool;
|
|
|
|
case Type::Number:
|
2020-04-06 22:48:47 -05:00
|
|
|
if (is_nan()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-10 10:03:50 +01:00
|
|
|
return !(m_value.as_double == 0 || m_value.as_double == -0);
|
2020-03-10 08:46:20 +01:00
|
|
|
case Type::Null:
|
|
|
|
case Type::Undefined:
|
|
|
|
return false;
|
|
|
|
case Type::String:
|
2020-04-29 12:35:39 +02:00
|
|
|
return !as_string().string().is_empty();
|
2020-03-10 08:46:20 +01:00
|
|
|
case Type::Object:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:32:44 +02:00
|
|
|
Value Value::to_primitive(Interpreter&) const
|
|
|
|
{
|
|
|
|
if (is_object())
|
|
|
|
return as_object().to_primitive();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-03-28 22:48:35 +01:00
|
|
|
Object* Value::to_object(Heap& heap) const
|
2020-03-11 18:58:19 +01:00
|
|
|
{
|
|
|
|
if (is_object())
|
2020-04-01 22:18:47 +02:00
|
|
|
return &const_cast<Object&>(as_object());
|
2020-03-11 18:58:19 +01:00
|
|
|
|
|
|
|
if (is_string())
|
2020-04-17 19:01:31 +02:00
|
|
|
return StringObject::create(heap.interpreter().global_object(), *m_value.as_string);
|
2020-03-11 18:58:19 +01:00
|
|
|
|
2020-04-04 23:13:13 +02:00
|
|
|
if (is_number())
|
2020-04-17 18:55:53 +02:00
|
|
|
return NumberObject::create(heap.interpreter().global_object(), m_value.as_double);
|
2020-04-04 23:13:13 +02:00
|
|
|
|
2020-04-06 22:51:16 -05:00
|
|
|
if (is_boolean())
|
2020-04-17 19:03:52 +02:00
|
|
|
return BooleanObject::create(heap.interpreter().global_object(), m_value.as_bool);
|
2020-04-06 22:51:16 -05:00
|
|
|
|
2020-03-28 22:48:35 +01:00
|
|
|
if (is_null() || is_undefined()) {
|
2020-04-10 12:48:31 +02:00
|
|
|
heap.interpreter().throw_exception<TypeError>("ToObject on null or undefined.");
|
2020-03-28 22:48:35 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-04 23:13:13 +02:00
|
|
|
dbg() << "Dying because I can't to_object() on " << *this;
|
2020-03-11 18:58:19 +01:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-03-16 00:19:41 +02:00
|
|
|
Value Value::to_number() const
|
2020-03-15 15:00:18 +01:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
2020-04-06 20:24:45 +02:00
|
|
|
case Type::Empty:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return {};
|
2020-05-07 17:09:00 -07:00
|
|
|
case Type::Undefined:
|
|
|
|
return js_nan();
|
|
|
|
case Type::Null:
|
|
|
|
return Value(0);
|
2020-03-15 15:00:18 +01:00
|
|
|
case Type::Boolean:
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(m_value.as_bool ? 1 : 0);
|
2020-03-15 15:00:18 +01:00
|
|
|
case Type::Number:
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(m_value.as_double);
|
|
|
|
case Type::String: {
|
2020-04-12 13:06:34 +01:00
|
|
|
// FIXME: Trim whitespace beforehand
|
2020-04-29 12:35:39 +02:00
|
|
|
auto& string = as_string().string();
|
2020-03-29 14:56:28 +02:00
|
|
|
if (string.is_empty())
|
|
|
|
return Value(0);
|
2020-04-12 13:06:34 +01:00
|
|
|
if (string == "Infinity" || string == "+Infinity")
|
|
|
|
return js_infinity();
|
|
|
|
if (string == "-Infinity")
|
2020-04-12 13:25:42 +01:00
|
|
|
return js_negative_infinity();
|
2020-03-16 00:19:41 +02:00
|
|
|
bool ok;
|
|
|
|
//FIXME: Parse in a better way
|
2020-03-29 14:56:28 +02:00
|
|
|
auto parsed_int = string.to_int(ok);
|
2020-03-16 00:19:41 +02:00
|
|
|
if (ok)
|
|
|
|
return Value(parsed_int);
|
|
|
|
|
2020-03-27 12:26:37 +01:00
|
|
|
return js_nan();
|
2020-03-15 15:00:18 +01:00
|
|
|
}
|
2020-03-16 00:19:41 +02:00
|
|
|
case Type::Object:
|
2020-04-28 23:58:23 +01:00
|
|
|
return m_value.as_object->to_primitive(Object::PreferredType::Number).to_number();
|
2020-03-16 00:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
i32 Value::to_i32() const
|
|
|
|
{
|
|
|
|
return static_cast<i32>(to_number().as_double());
|
2020-03-15 15:00:18 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:22:20 +02:00
|
|
|
double Value::to_double() const
|
|
|
|
{
|
|
|
|
return to_number().as_double();
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:41:30 +01:00
|
|
|
size_t Value::to_size_t() const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return 0;
|
|
|
|
auto number = to_number();
|
|
|
|
if (number.is_nan() || number.as_double() <= 0)
|
|
|
|
return 0;
|
2020-05-01 11:06:27 +01:00
|
|
|
return min((double)number.to_i32(), MAX_ARRAY_LIKE_INDEX);
|
2020-04-30 22:41:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value greater_than(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() > rhs.to_number().as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value greater_than_equals(Interpreter&, Value lhs, Value rhs)
|
2020-03-12 23:07:08 +11:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() >= rhs.to_number().as_double());
|
2020-03-12 23:07:08 +11:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value less_than(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() < rhs.to_number().as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value less_than_equals(Interpreter&, Value lhs, Value rhs)
|
2020-03-12 23:07:08 +11:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() <= rhs.to_number().as_double());
|
2020-03-12 23:07:08 +11:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value bitwise_and(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value((i32)lhs.to_number().as_double() & (i32)rhs.to_number().as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value bitwise_or(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-04-05 00:11:47 -07:00
|
|
|
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() : lhs.to_number();
|
|
|
|
|
|
|
|
if (!rhs.is_number() && !lhs.is_number())
|
|
|
|
return Value(0);
|
|
|
|
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value((i32)lhs.to_number().as_double() | (i32)rhs.to_number().as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value bitwise_xor(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value((i32)lhs.to_number().as_double() ^ (i32)rhs.to_number().as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value bitwise_not(Interpreter&, Value lhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(~(i32)lhs.to_number().as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value unary_plus(Interpreter&, Value lhs)
|
2020-04-02 17:58:39 +01:00
|
|
|
{
|
|
|
|
return lhs.to_number();
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value unary_minus(Interpreter&, Value lhs)
|
2020-04-02 17:58:39 +01:00
|
|
|
{
|
|
|
|
if (lhs.to_number().is_nan())
|
|
|
|
return js_nan();
|
|
|
|
return Value(-lhs.to_number().as_double());
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value left_shift(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-04-23 13:36:14 +01:00
|
|
|
auto lhs_number = lhs.to_number();
|
|
|
|
if (!lhs_number.is_finite_number())
|
|
|
|
return Value(0);
|
|
|
|
auto rhs_number = rhs.to_number();
|
|
|
|
if (!rhs_number.is_finite_number())
|
|
|
|
return lhs_number;
|
2020-04-23 20:00:23 +01:00
|
|
|
return Value((i32)lhs_number.as_double() << (i32)rhs_number.as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value right_shift(Interpreter&, Value lhs, Value rhs)
|
2020-03-10 11:35:05 +01:00
|
|
|
{
|
2020-04-23 13:45:19 +01:00
|
|
|
auto lhs_number = lhs.to_number();
|
|
|
|
if (!lhs_number.is_finite_number())
|
|
|
|
return Value(0);
|
|
|
|
auto rhs_number = rhs.to_number();
|
|
|
|
if (!rhs_number.is_finite_number())
|
|
|
|
return lhs_number;
|
|
|
|
return Value((i32)lhs_number.as_double() >> (i32)rhs_number.as_double());
|
2020-03-10 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 15:43:10 +01:00
|
|
|
Value unsigned_right_shift(Interpreter&, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
auto lhs_number = lhs.to_number();
|
|
|
|
if (!lhs_number.is_finite_number())
|
|
|
|
return Value(0);
|
|
|
|
auto rhs_number = rhs.to_number();
|
|
|
|
if (!rhs_number.is_finite_number())
|
|
|
|
return lhs_number;
|
|
|
|
return Value((unsigned)lhs_number.as_double() >> (i32)rhs_number.as_double());
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value add(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-11 12:16:26 +01:00
|
|
|
{
|
2020-04-15 09:32:44 +02:00
|
|
|
auto lhs_primitive = lhs.to_primitive(interpreter);
|
|
|
|
auto rhs_primitive = rhs.to_primitive(interpreter);
|
|
|
|
|
|
|
|
if (lhs_primitive.is_string() || rhs_primitive.is_string())
|
|
|
|
return js_string(interpreter.heap(), String::format("%s%s", lhs_primitive.to_string().characters(), rhs_primitive.to_string().characters()));
|
2020-03-16 00:19:41 +02:00
|
|
|
|
2020-04-15 09:32:44 +02:00
|
|
|
return Value(lhs_primitive.to_number().as_double() + rhs_primitive.to_number().as_double());
|
2020-03-11 12:16:26 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value sub(Interpreter&, Value lhs, Value rhs)
|
2020-03-11 12:16:26 +01:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() - rhs.to_number().as_double());
|
2020-03-11 12:16:26 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value mul(Interpreter&, Value lhs, Value rhs)
|
2020-03-12 23:04:52 +11:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() * rhs.to_number().as_double());
|
2020-03-12 23:04:52 +11:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value div(Interpreter&, Value lhs, Value rhs)
|
2020-03-12 23:04:52 +11:00
|
|
|
{
|
2020-03-16 00:19:41 +02:00
|
|
|
return Value(lhs.to_number().as_double() / rhs.to_number().as_double());
|
2020-03-12 23:04:52 +11:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value mod(Interpreter&, Value lhs, Value rhs)
|
2020-04-04 21:17:34 +02:00
|
|
|
{
|
2020-04-05 01:32:04 -07:00
|
|
|
if (lhs.to_number().is_nan() || rhs.to_number().is_nan())
|
|
|
|
return js_nan();
|
|
|
|
|
|
|
|
double index = lhs.to_number().as_double();
|
|
|
|
double period = rhs.to_number().as_double();
|
2020-04-30 22:41:30 +01:00
|
|
|
double trunc = (double)(i32)(index / period);
|
2020-04-05 01:32:04 -07:00
|
|
|
|
|
|
|
return Value(index - trunc * period);
|
2020-04-04 21:17:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:28:41 +02:00
|
|
|
Value exp(Interpreter&, Value lhs, Value rhs)
|
2020-04-05 13:40:00 +01:00
|
|
|
{
|
|
|
|
return Value(pow(lhs.to_number().as_double(), rhs.to_number().as_double()));
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
Value in(Interpreter& interpreter, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
if (!rhs.is_object())
|
|
|
|
return interpreter.throw_exception<TypeError>("'in' operator must be used on object");
|
|
|
|
|
|
|
|
return Value(!rhs.as_object().get(lhs.to_string()).is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
Value instance_of(Interpreter&, Value lhs, Value rhs)
|
2020-03-11 12:16:26 +01:00
|
|
|
{
|
2020-05-07 17:09:00 -07:00
|
|
|
if (!lhs.is_object() || !rhs.is_object())
|
|
|
|
return Value(false);
|
|
|
|
|
|
|
|
auto constructor_prototype_property = rhs.as_object().get("prototype");
|
|
|
|
if (!constructor_prototype_property.is_object())
|
2020-03-11 12:16:26 +01:00
|
|
|
return Value(false);
|
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
return Value(lhs.as_object().has_prototype(&constructor_prototype_property.as_object()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const LogStream& operator<<(const LogStream& stream, const Value& value)
|
|
|
|
{
|
|
|
|
return stream << value.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
return lhs.to_double() == rhs.to_double();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if ((lhs.is_positive_zero() || lhs.is_negative_zero()) && (rhs.is_positive_zero() || rhs.is_negative_zero()))
|
|
|
|
return true;
|
|
|
|
return lhs.to_double() == rhs.to_double();
|
|
|
|
}
|
|
|
|
|
|
|
|
return same_value_non_numeric(interpreter, lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool same_value_non_numeric(Interpreter&, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
ASSERT(!lhs.is_number());
|
|
|
|
ASSERT(lhs.type() == rhs.type());
|
|
|
|
|
2020-03-11 12:16:26 +01:00
|
|
|
switch (lhs.type()) {
|
2020-04-06 20:24:45 +02:00
|
|
|
case Value::Type::Empty:
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-11 12:16:26 +01:00
|
|
|
case Value::Type::Undefined:
|
|
|
|
case Value::Type::Null:
|
2020-05-07 17:09:00 -07:00
|
|
|
return true;
|
2020-03-11 12:16:26 +01:00
|
|
|
case Value::Type::String:
|
2020-05-07 17:09:00 -07:00
|
|
|
return lhs.as_string().string() == rhs.as_string().string();
|
2020-03-11 12:16:26 +01:00
|
|
|
case Value::Type::Boolean:
|
2020-05-07 17:09:00 -07:00
|
|
|
return lhs.as_bool() == rhs.as_bool();
|
2020-03-11 12:16:26 +01:00
|
|
|
case Value::Type::Object:
|
2020-05-07 17:09:00 -07:00
|
|
|
return &lhs.as_object() == &rhs.as_object();
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-03-11 12:16:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
bool strict_eq(Interpreter& interpreter, Value lhs, Value rhs)
|
2020-03-16 00:23:38 +02:00
|
|
|
{
|
2020-05-07 17:09:00 -07:00
|
|
|
if (lhs.type() != rhs.type())
|
|
|
|
return false;
|
2020-03-16 00:23:38 +02:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if (lhs.is_number()) {
|
|
|
|
if (lhs.is_nan() || rhs.is_nan())
|
|
|
|
return false;
|
|
|
|
if (lhs.to_double() == rhs.to_double())
|
|
|
|
return true;
|
|
|
|
if ((lhs.is_positive_zero() || lhs.is_negative_zero()) && (rhs.is_positive_zero() || rhs.is_negative_zero()))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-16 00:23:38 +02:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
return same_value_non_numeric(interpreter, lhs, rhs);
|
|
|
|
}
|
2020-03-16 00:23:38 +02:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
bool abstract_eq(Interpreter& interpreter, Value lhs, Value rhs)
|
|
|
|
{
|
|
|
|
if (lhs.type() == rhs.type())
|
|
|
|
return strict_eq(interpreter, lhs, rhs);
|
2020-03-16 00:23:38 +02:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null()))
|
|
|
|
return true;
|
2020-03-16 00:23:38 +02:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if (lhs.is_number() && rhs.is_string())
|
|
|
|
return abstract_eq(interpreter, lhs, rhs.to_number());
|
2020-03-16 00:23:38 +02:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if (lhs.is_string() && rhs.is_number())
|
|
|
|
return abstract_eq(interpreter, lhs.to_number(), rhs);
|
2020-04-23 16:06:01 +01:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if (lhs.is_boolean())
|
|
|
|
return abstract_eq(interpreter, lhs.to_number(), rhs);
|
2020-04-23 16:06:01 +01:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if (rhs.is_boolean())
|
|
|
|
return abstract_eq(interpreter, lhs, rhs.to_number());
|
2020-03-28 16:56:54 +01:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if ((lhs.is_string() || lhs.is_number()) && rhs.is_object())
|
|
|
|
return abstract_eq(interpreter, lhs, rhs.to_primitive(interpreter));
|
2020-03-28 16:56:54 +01:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
if (lhs.is_object() && (rhs.is_string() || rhs.is_number()))
|
|
|
|
return abstract_eq(interpreter, lhs.to_primitive(interpreter), rhs);
|
2020-03-28 16:56:54 +01:00
|
|
|
|
2020-05-07 17:09:00 -07:00
|
|
|
return false;
|
2020-03-07 21:43:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 19:42:11 +01:00
|
|
|
}
|