summaryrefslogtreecommitdiff
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
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". :^)
-rw-r--r--Userland/Libraries/LibJS/Runtime/ErrorTypes.h3
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp12
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.h2
3 files changed, 16 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
index c23e43a612..db65922626 100644
--- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
+++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h
@@ -44,6 +44,7 @@
M(JsonCircular, "Cannot stringify circular object") \
M(JsonMalformed, "Malformed JSON string") \
M(NegativeExponent, "Exponent must be positive") \
+ M(NonExtensibleDefine, "Cannot define property {} on non-extensible object") \
M(NotA, "Not a {} object") \
M(NotAConstructor, "{} is not a constructor") \
M(NotAFunction, "{} is not a function") \
@@ -52,7 +53,7 @@
M(NotAnObject, "{} is not an object") \
M(NotASymbol, "{} is not a symbol") \
M(NotIterable, "{} is not iterable") \
- M(NonExtensibleDefine, "Cannot define property {} on non-extensible object") \
+ M(NotObjectCoercible, "{} cannot be converted to an object") \
M(NumberIncompatibleThis, "Number.prototype.{}() called with incompatible this target") \
M(ObjectDefinePropertyReturnedFalse, "Object's [[DefineProperty]] method returned false") \
M(ObjectFreezeFailed, "Could not freeze object") \
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;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h
index 37cbf2cfcc..40122ee9a6 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.h
+++ b/Userland/Libraries/LibJS/Runtime/Value.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -366,6 +367,7 @@ TriState abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs)
Function* get_method(GlobalObject& global_object, Value, const PropertyName&);
size_t length_of_array_like(GlobalObject&, const Object&);
Object* species_constructor(GlobalObject&, const Object&, Object& default_constructor);
+Value require_object_coercible(GlobalObject&, Value);
}