summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Value.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-06-06 16:10:23 +0100
committerAndreas Kling <kling@serenityos.org>2021-06-06 19:34:43 +0200
commitcd12b2aa577bd977ec19214e7099c02dadd5d765 (patch)
tree20ca44197d159e29f6e8cc32234e18aacddd9981 /Userland/Libraries/LibJS/Runtime/Value.cpp
parent4ca4c43cbd2b31c79b505066b7f6d28dfe0a7eaa (diff)
downloadserenity-cd12b2aa577bd977ec19214e7099c02dadd5d765.zip
LibJS: Implement the RequireObjectCoercible abstract operation
Throws an exception if the given value is nullish, returns it otherwise. We can now gradually replace such manual checks with this function where applicable. This also has the advantage that the somewhat useless "ToObject on null or undefined" will be replaced with "null cannot be converted to an object" or "undefined cannot be converted to an object". :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Value.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 1801f75245..69e8c3272c 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -1390,4 +1390,16 @@ Object* species_constructor(GlobalObject& global_object, const Object& object, O
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, species.to_string_without_side_effects());
return nullptr;
}
+
+// 7.2.1 RequireObjectCoercible, https://tc39.es/ecma262/#sec-requireobjectcoercible
+Value require_object_coercible(GlobalObject& global_object, Value value)
+{
+ auto& vm = global_object.vm();
+ if (value.is_nullish()) {
+ vm.throw_exception<TypeError>(global_object, ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
+ return {};
+ }
+ return value;
+}
+
}