LibJS: Add Value::is_nullish()

This commit is contained in:
Andreas Kling 2020-10-02 16:00:15 +02:00
parent ef1b21004f
commit fa18baf3e8
Notes: sideshowbarker 2024-07-19 02:05:54 +09:00
9 changed files with 22 additions and 21 deletions

View file

@ -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 {};

View file

@ -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())

View file

@ -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())

View file

@ -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);

View file

@ -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");

View file

@ -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 {};
}

View file

@ -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 {};
}

View file

@ -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())

View file

@ -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;