diff options
author | Andreas Kling <kling@serenityos.org> | 2020-10-02 16:00:15 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-02 18:01:27 +0200 |
commit | fa18baf3e84a8688b4ade4d841c909d5940778db (patch) | |
tree | 5d43f3a769e2b059d879b0fea65172678b08e925 /Libraries | |
parent | ef1b21004fc04efdd1baf01d375d4bac614abc61 (diff) | |
download | serenity-fa18baf3e84a8688b4ade4d841c909d5940778db.zip |
LibJS: Add Value::is_nullish()
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/AST.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/MarkupGenerator.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ArrayPrototype.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/FunctionPrototype.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ProxyObject.cpp | 24 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/StringConstructor.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/StringPrototype.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.h | 1 |
9 files changed, 22 insertions, 21 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 00687215eb..a8a1b44cdc 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -126,7 +126,7 @@ CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interprete auto lookup_target = is_super_property_lookup ? interpreter.current_environment()->get_super_base() : member_expression.object().execute(interpreter, global_object); if (interpreter.exception()) return {}; - if (is_super_property_lookup && (lookup_target.is_null() || lookup_target.is_undefined())) { + if (is_super_property_lookup && lookup_target.is_nullish()) { interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, lookup_target.to_string_without_side_effects().characters()); return {}; } @@ -543,7 +543,7 @@ Value LogicalExpression::execute(Interpreter& interpreter, GlobalObject& global_ return rhs_result; } case LogicalOp::NullishCoalescing: - if (lhs_result.is_null() || lhs_result.is_undefined()) { + if (lhs_result.is_nullish()) { auto rhs_result = m_rhs->execute(interpreter, global_object); if (interpreter.exception()) return {}; diff --git a/Libraries/LibJS/MarkupGenerator.cpp b/Libraries/LibJS/MarkupGenerator.cpp index dfaa5cffcc..c0855e53f4 100644 --- a/Libraries/LibJS/MarkupGenerator.cpp +++ b/Libraries/LibJS/MarkupGenerator.cpp @@ -101,7 +101,7 @@ void MarkupGenerator::value_to_html(Value value, StringBuilder& output_html, Has output_html.append(open_style_type(StyleType::String)); else if (value.is_number()) output_html.append(open_style_type(StyleType::Number)); - else if (value.is_boolean() || value.is_null() || value.is_undefined()) + else if (value.is_boolean() || value.is_nullish()) output_html.append(open_style_type(StyleType::KeywordBold)); if (value.is_string()) diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 5f47eef197..bef371a353 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -295,7 +295,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::to_locale_string) auto value = this_object->get(i).value_or(js_undefined()); if (vm.exception()) return {}; - if (value.is_undefined() || value.is_null()) + if (value.is_nullish()) continue; auto* value_object = value.to_object(global_object); ASSERT(value_object); @@ -331,7 +331,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::join) auto value = this_object->get(i).value_or(js_undefined()); if (vm.exception()) return {}; - if (value.is_undefined() || value.is_null()) + if (value.is_nullish()) continue; auto string = value.to_string(global_object); if (vm.exception()) diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index 625acd06ec..28011570a4 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -72,7 +72,7 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::apply) auto& function = static_cast<Function&>(*this_object); auto this_arg = vm.argument(0); auto arg_array = vm.argument(1); - if (arg_array.is_null() || arg_array.is_undefined()) + if (arg_array.is_nullish()) return vm.call(function, this_arg); if (!arg_array.is_object()) { vm.throw_exception<TypeError>(global_object, ErrorType::FunctionArgsNotObject); diff --git a/Libraries/LibJS/Runtime/ProxyObject.cpp b/Libraries/LibJS/Runtime/ProxyObject.cpp index 4d01e47496..48413be261 100644 --- a/Libraries/LibJS/Runtime/ProxyObject.cpp +++ b/Libraries/LibJS/Runtime/ProxyObject.cpp @@ -82,7 +82,7 @@ Object* ProxyObject::prototype() auto trap = m_handler.get("getPrototypeOf"); if (vm().exception()) return nullptr; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.prototype(); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "getPrototypeOf"); @@ -131,7 +131,7 @@ bool ProxyObject::set_prototype(Object* object) auto trap = m_handler.get("setPrototypeOf"); if (vm().exception()) return false; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.set_prototype(object); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "setPrototypeOf"); @@ -162,7 +162,7 @@ bool ProxyObject::is_extensible() const auto trap = m_handler.get("isExtensible"); if (vm().exception()) return false; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.is_extensible(); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "isExtensible"); @@ -189,7 +189,7 @@ bool ProxyObject::prevent_extensions() auto trap = m_handler.get("preventExtensions"); if (vm().exception()) return false; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.prevent_extensions(); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "preventExtensions"); @@ -216,7 +216,7 @@ Optional<PropertyDescriptor> ProxyObject::get_own_property_descriptor(const Prop auto trap = m_handler.get("getOwnPropertyDescriptor"); if (vm().exception()) return {}; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.get_own_property_descriptor(name); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "getOwnPropertyDescriptor"); @@ -271,7 +271,7 @@ bool ProxyObject::define_property(const StringOrSymbol& property_name, const Obj auto trap = m_handler.get("defineProperty"); if (vm().exception()) return false; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.define_property(property_name, descriptor, throw_exceptions); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "defineProperty"); @@ -322,7 +322,7 @@ bool ProxyObject::has_property(const PropertyName& name) const auto trap = m_handler.get("has"); if (vm().exception()) return false; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.has_property(name); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "has"); @@ -360,7 +360,7 @@ Value ProxyObject::get(const PropertyName& name, Value) const auto trap = m_handler.get("get"); if (vm().exception()) return {}; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.get(name); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "get"); @@ -395,7 +395,7 @@ bool ProxyObject::put(const PropertyName& name, Value value, Value) auto trap = m_handler.get("set"); if (vm().exception()) return false; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.put(name, value); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "set"); @@ -428,7 +428,7 @@ Value ProxyObject::delete_property(const PropertyName& name) auto trap = m_handler.get("deleteProperty"); if (vm().exception()) return {}; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return m_target.delete_property(name); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "deleteProperty"); @@ -472,7 +472,7 @@ Value ProxyObject::call() auto trap = m_handler.get("apply"); if (vm().exception()) return {}; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return static_cast<Function&>(m_target).call(); if (!trap.is_function()) { vm().throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "apply"); @@ -505,7 +505,7 @@ Value ProxyObject::construct(Function& new_target) auto trap = m_handler.get("construct"); if (vm.exception()) return {}; - if (trap.is_empty() || trap.is_undefined() || trap.is_null()) + if (trap.is_empty() || trap.is_nullish()) return static_cast<Function&>(m_target).construct(new_target); if (!trap.is_function()) { vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyInvalidTrap, "construct"); diff --git a/Libraries/LibJS/Runtime/StringConstructor.cpp b/Libraries/LibJS/Runtime/StringConstructor.cpp index 258b144465..b8c4e3592f 100644 --- a/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -87,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw) auto raw = template_object->get("raw"); if (vm.exception()) return {}; - if (raw.is_empty() || raw.is_undefined() || raw.is_null()) { + if (raw.is_empty() || raw.is_nullish()) { vm.throw_exception<TypeError>(global_object, ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined"); return {}; } diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index 77c633688b..17eea30465 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -459,7 +459,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::last_index_of) JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator) { auto this_object = vm.this_value(global_object); - if (this_object.is_undefined() || this_object.is_null()) { + if (this_object.is_nullish()) { vm.throw_exception<TypeError>(global_object, ErrorType::ToObjectNullOrUndef); return {}; } diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index dd002516ba..f31147e9f9 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -851,7 +851,7 @@ bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs) if (lhs.type() == rhs.type()) return strict_eq(lhs, rhs); - if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null())) + if (lhs.is_nullish() && rhs.is_nullish()) return true; if (lhs.is_number() && rhs.is_string()) diff --git a/Libraries/LibJS/Runtime/Value.h b/Libraries/LibJS/Runtime/Value.h index ff4ac91575..09bfafd13d 100644 --- a/Libraries/LibJS/Runtime/Value.h +++ b/Libraries/LibJS/Runtime/Value.h @@ -71,6 +71,7 @@ public: bool is_accessor() const { return m_type == Type::Accessor; }; bool is_bigint() const { return m_type == Type::BigInt; }; bool is_native_property() const { return m_type == Type::NativeProperty; } + bool is_nullish() const { return is_null() || is_undefined(); } bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol() || is_native_property(); } bool is_array() const; bool is_function() const; |