summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-04-14 22:17:42 +0200
committerAndreas Kling <kling@serenityos.org>2021-04-14 22:37:12 +0200
commit51a5427419e99cf2760bd15a9ef985cf5bcb7ead (patch)
treeb7f70d686567330bfc2e8984a1ec44303d6bc8fc /Userland
parent73a92c79b8d465d65a3cf7815cd007219cb185bc (diff)
downloadserenity-51a5427419e99cf2760bd15a9ef985cf5bcb7ead.zip
LibJS: Improve Reference::get() TypeError message for nullish base
"ToObject on null or undefined" is useless. "Cannot get property 'foo' of undefined" isn't.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ErrorTypes.h5
-rw-r--r--Userland/Libraries/LibJS/Runtime/Reference.cpp16
2 files changed, 15 insertions, 6 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
index b9043ec771..04d64d35c8 100644
--- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
+++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
@@ -148,8 +148,9 @@
"target is non-extensible") \
M(ProxyTwoArguments, "Proxy constructor requires at least two arguments") \
M(ReduceNoInitial, "Reduce of empty array with no initial value") \
- M(ReferenceNullishAssignment, "Cannot set property '{}' of {}") \
- M(ReferencePrimitiveAssignment, "Cannot set property '{}' of {} '{}'") \
+ M(ReferenceNullishGetProperty, "Cannot get property '{}' of {}") \
+ M(ReferenceNullishSetProperty, "Cannot set property '{}' of {}") \
+ M(ReferencePrimitiveSetProperty, "Cannot set property '{}' of {} '{}'") \
M(ReferenceUnresolvable, "Unresolvable reference") \
M(ReflectArgumentMustBeAFunction, "First argument of Reflect.{}() must be a function") \
M(ReflectArgumentMustBeAnObject, "First argument of Reflect.{}() must be an object") \
diff --git a/Userland/Libraries/LibJS/Runtime/Reference.cpp b/Userland/Libraries/LibJS/Runtime/Reference.cpp
index b567e38d23..01b18843fa 100644
--- a/Userland/Libraries/LibJS/Runtime/Reference.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Reference.cpp
@@ -51,15 +51,15 @@ void Reference::put(GlobalObject& global_object, Value value)
if (!base.is_object() && vm.in_strict_mode()) {
if (base.is_nullish())
- vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishAssignment, m_name.to_value(global_object.vm()).to_string_without_side_effects(), base.to_string_without_side_effects());
+ vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name.to_value(vm).to_string_without_side_effects(), base.to_string_without_side_effects());
else
- vm.throw_exception<TypeError>(global_object, ErrorType::ReferencePrimitiveAssignment, m_name.to_value(global_object.vm()).to_string_without_side_effects(), base.typeof(), base.to_string_without_side_effects());
+ vm.throw_exception<TypeError>(global_object, ErrorType::ReferencePrimitiveSetProperty, m_name.to_value(vm).to_string_without_side_effects(), base.typeof(), base.to_string_without_side_effects());
return;
}
if (base.is_nullish()) {
// This will always fail the to_object() call below, let's throw the TypeError ourselves with a nice message instead.
- vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishAssignment, m_name.to_value(global_object.vm()).to_string_without_side_effects(), base.to_string_without_side_effects());
+ vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishSetProperty, m_name.to_value(vm).to_string_without_side_effects(), base.to_string_without_side_effects());
return;
}
@@ -103,7 +103,15 @@ Value Reference::get(GlobalObject& global_object)
return value;
}
- auto* object = base().to_object(global_object);
+ auto base = this->base();
+
+ if (base.is_nullish()) {
+ // This will always fail the to_object() call below, let's throw the TypeError ourselves with a nice message instead.
+ vm.throw_exception<TypeError>(global_object, ErrorType::ReferenceNullishGetProperty, m_name.to_value(vm).to_string_without_side_effects(), base.to_string_without_side_effects());
+ return {};
+ }
+
+ auto* object = base.to_object(global_object);
if (!object)
return {};