/* * Copyright (c) 2020, Andreas Kling * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // 2 ** 53 - 1 #define MAX_ARRAY_LIKE_INDEX 9007199254740991.0 namespace JS { bool Value::is_array() const { return is_object() && as_object().is_array(); } bool Value::is_function() const { return is_object() && as_object().is_function(); } Function& Value::as_function() { ASSERT(is_function()); return static_cast(as_object()); } String Value::to_string_without_side_effects() const { if (is_boolean()) return as_bool() ? "true" : "false"; if (is_null()) return "null"; if (is_undefined()) return "undefined"; if (is_number()) { if (is_nan()) return "NaN"; if (is_infinity()) return as_double() < 0 ? "-Infinity" : "Infinity"; if (is_integer()) return String::number(to_i32()); return String::format("%.4f", as_double()); } if (is_string()) return m_value.as_string->string(); if (is_symbol()) return as_symbol().to_string(); ASSERT(is_object()); return String::format("[object %s]", as_object().class_name()); } PrimitiveString* Value::to_primitive_string(Interpreter& interpreter) { 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 { if (is_boolean()) return as_bool() ? "true" : "false"; if (is_null()) return "null"; if (is_undefined()) return "undefined"; if (is_number()) { if (is_nan()) return "NaN"; if (is_infinity()) return as_double() < 0 ? "-Infinity" : "Infinity"; if (is_integer()) return String::number(to_i32()); return String::format("%.4f", as_double()); } if (is_symbol()) { interpreter.throw_exception("Can't convert symbol to string"); return {}; } 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(interpreter); } ASSERT(is_string()); return m_value.as_string->string(); } bool Value::to_boolean() const { switch (m_type) { case Type::Boolean: return m_value.as_bool; case Type::Number: if (is_nan()) { return false; } return !(m_value.as_double == 0 || m_value.as_double == -0); case Type::Null: case Type::Undefined: return false; case Type::String: return !as_string().string().is_empty(); case Type::Object: case Type::Symbol: return true; default: ASSERT_NOT_REACHED(); } } Value Value::to_primitive(Interpreter&) const { if (is_object()) return as_object().to_primitive(); return *this; } Object* Value::to_object(Interpreter& interpreter) const { if (is_object()) return &const_cast(as_object()); if (is_string()) return StringObject::create(interpreter.global_object(), *m_value.as_string); if (is_symbol()) return SymbolObject::create(interpreter.global_object(), *m_value.as_symbol); if (is_number()) return NumberObject::create(interpreter.global_object(), m_value.as_double); if (is_boolean()) return BooleanObject::create(interpreter.global_object(), m_value.as_bool); if (is_null() || is_undefined()) { interpreter.throw_exception("ToObject on null or undefined."); return nullptr; } dbg() << "Dying because I can't to_object() on " << *this; ASSERT_NOT_REACHED(); } Value Value::to_number(Interpreter& interpreter) const { switch (m_type) { case Type::Empty: ASSERT_NOT_REACHED(); return {}; case Type::Undefined: return js_nan(); case Type::Null: return Value(0); case Type::Boolean: return Value(m_value.as_bool ? 1 : 0); case Type::Number: return Value(m_value.as_double); case Type::String: { auto string = as_string().string().trim_whitespace(); if (string.is_empty()) return Value(0); if (string == "Infinity" || string == "+Infinity") return js_infinity(); if (string == "-Infinity") return js_negative_infinity(); char* endptr; auto parsed_double = strtod(string.characters(), &endptr); if (*endptr) return js_nan(); return Value(parsed_double); } case Type::Symbol: // FIXME: Get access to the interpreter and throw a TypeError ASSERT_NOT_REACHED(); case Type::Object: auto primitive = m_value.as_object->to_primitive(Object::PreferredType::Number); if (interpreter.exception()) return {}; return primitive.to_number(interpreter); } ASSERT_NOT_REACHED(); } double Value::to_double(Interpreter& interpreter) const { auto number = to_number(interpreter); if (interpreter.exception()) return 0; return number.as_double(); } i32 Value::to_i32() const { return static_cast(as_double()); } i32 Value::to_i32(Interpreter& interpreter) const { auto number = to_number(interpreter); if (interpreter.exception()) return 0; return number.to_i32(); } size_t Value::to_size_t() const { ASSERT(type() == Type::Number); if (is_nan() || as_double() <= 0) return 0; return min((double)(i32)as_double(), MAX_ARRAY_LIKE_INDEX); } size_t Value::to_size_t(Interpreter& interpreter) const { if (is_empty()) return 0; auto number = to_number(interpreter); if (interpreter.exception()) return 0; return number.to_size_t(); } Value greater_than(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() > rhs_number.as_double()); } Value greater_than_equals(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() >= rhs_number.as_double()); } Value less_than(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() < rhs_number.as_double()); } Value less_than_equals(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() <= rhs_number.as_double()); } Value bitwise_and(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value((i32)lhs_number.as_double() & (i32)rhs_number.as_double()); } 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); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value((i32)lhs_number.as_double() | (i32)rhs_number.as_double()); } Value bitwise_xor(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value((i32)lhs_number.as_double() ^ (i32)rhs_number.as_double()); } Value bitwise_not(Interpreter& interpreter, Value lhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(~(i32)lhs_number.as_double()); } Value unary_plus(Interpreter& interpreter, Value lhs) { return lhs.to_number(interpreter); } Value unary_minus(Interpreter& interpreter, Value lhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; if (lhs_number.is_nan()) return js_nan(); return Value(-lhs_number.as_double()); } Value left_shift(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; if (!lhs_number.is_finite_number()) return Value(0); auto rhs_number = rhs.to_number(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()); } Value right_shift(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; if (!lhs_number.is_finite_number()) return Value(0); auto rhs_number = rhs.to_number(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()); } Value unsigned_right_shift(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; if (!lhs_number.is_finite_number()) return Value(0); auto rhs_number = rhs.to_number(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()); } Value add(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_primitive = lhs.to_primitive(interpreter); if (interpreter.exception()) return {}; auto rhs_primitive = rhs.to_primitive(interpreter); if (interpreter.exception()) return {}; 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()); } auto lhs_number = lhs_primitive.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs_primitive.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() + rhs_number.as_double()); } Value sub(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() - rhs_number.as_double()); } Value mul(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() * rhs_number.as_double()); } Value div(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(lhs_number.as_double() / rhs_number.as_double()); } Value mod(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(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); } Value exp(Interpreter& interpreter, Value lhs, Value rhs) { auto lhs_number = lhs.to_number(interpreter); if (interpreter.exception()) return {}; auto rhs_number = rhs.to_number(interpreter); if (interpreter.exception()) return {}; return Value(pow(lhs_number.as_double(), rhs_number.as_double())); } Value in(Interpreter& interpreter, Value lhs, Value rhs) { if (!rhs.is_object()) return interpreter.throw_exception("'in' operator must be used on object"); auto lhs_string = lhs.to_string(interpreter); if (interpreter.exception()) return {}; return Value(!rhs.as_object().get(lhs_string).is_empty()); } Value instance_of(Interpreter&, Value lhs, Value rhs) { 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()) return Value(false); return Value(lhs.as_object().has_prototype(&constructor_prototype_property.as_object())); } const LogStream& operator<<(const LogStream& stream, const Value& value) { return stream << (value.is_empty() ? "" : value.to_string_without_side_effects()); } 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.as_double() == rhs.as_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.as_double() == rhs.as_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()); switch (lhs.type()) { case Value::Type::Empty: ASSERT_NOT_REACHED(); case Value::Type::Undefined: case Value::Type::Null: return true; case Value::Type::String: return lhs.as_string().string() == rhs.as_string().string(); case Value::Type::Symbol: return &lhs.as_symbol() == &rhs.as_symbol(); case Value::Type::Boolean: return lhs.as_bool() == rhs.as_bool(); case Value::Type::Object: return &lhs.as_object() == &rhs.as_object(); default: ASSERT_NOT_REACHED(); } } bool strict_eq(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 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; } return same_value_non_numeric(interpreter, lhs, rhs); } bool abstract_eq(Interpreter& interpreter, Value lhs, Value rhs) { if (lhs.type() == rhs.type()) return strict_eq(interpreter, lhs, rhs); if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null())) return true; if (lhs.is_number() && rhs.is_string()) return abstract_eq(interpreter, lhs, rhs.to_number(interpreter)); if (lhs.is_string() && rhs.is_number()) return abstract_eq(interpreter, lhs.to_number(interpreter), rhs); 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()) return abstract_eq(interpreter, lhs, rhs.to_primitive(interpreter)); if (lhs.is_object() && (rhs.is_string() || rhs.is_number() || rhs.is_symbol())) return abstract_eq(interpreter, lhs.to_primitive(interpreter), rhs); return false; } }