summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-09-15 20:52:21 +0100
committerLinus Groh <mail@linusgroh.de>2021-09-15 23:46:53 +0100
commit568296d0cc9016c8eefa7dd83ace616514ce261a (patch)
tree7f40a79056b3a6cf48960397bd652766b33d455a /Userland/Libraries/LibJS
parent33679a8445bbfbb0525ff45741e0f379ddca6c29 (diff)
downloadserenity-568296d0cc9016c8eefa7dd83ace616514ce261a.zip
LibJS: Use ThrowCompletionOr in require_object_coercible()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/AST.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp13
-rw-r--r--Userland/Libraries/LibJS/Runtime/AbstractOperations.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp8
-rw-r--r--Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/StringPrototype.cpp48
6 files changed, 22 insertions, 57 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp
index aad0fe71c4..a866f104a1 100644
--- a/Userland/Libraries/LibJS/AST.cpp
+++ b/Userland/Libraries/LibJS/AST.cpp
@@ -786,9 +786,7 @@ Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject&
// From here on equivalent to
// 13.3.4 EvaluatePropertyAccessWithIdentifierKey ( baseValue, identifierName, strict ), https://tc39.es/ecma262/#sec-evaluate-property-access-with-identifier-key
- object_value = require_object_coercible(global_object, object_value);
- if (interpreter.exception())
- return {};
+ object_value = TRY_OR_DISCARD(require_object_coercible(global_object, object_value));
auto property_name = computed_property_name(interpreter, global_object);
if (!property_name.is_valid())
diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
index bb3afd0430..5fa9080b0b 100644
--- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
+++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
@@ -17,6 +17,7 @@
#include <LibJS/Runtime/ArgumentsObject.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BoundFunction.h>
+#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
@@ -32,13 +33,11 @@
namespace JS {
// 7.2.1 RequireObjectCoercible ( argument ), https://tc39.es/ecma262/#sec-requireobjectcoercible
-Value require_object_coercible(GlobalObject& global_object, Value value)
+ThrowCompletionOr<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 {};
- }
+ if (value.is_nullish())
+ return vm.throw_completion<TypeError>(global_object, ErrorType::NotObjectCoercible, value.to_string_without_side_effects());
return value;
}
@@ -364,9 +363,7 @@ Reference make_super_property_reference(GlobalObject& global_object, Value actua
// 3. Let baseValue be ? env.GetSuperBase().
auto base_value = env.get_super_base();
// 4. Let bv be ? RequireObjectCoercible(baseValue).
- auto bv = require_object_coercible(global_object, base_value);
- if (vm.exception())
- return {};
+ auto bv = TRY_OR_DISCARD(require_object_coercible(global_object, base_value));
// 5. Return the Reference Record { [[Base]]: bv, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
// 6. NOTE: This returns a Super Reference Record.
return Reference { bv, property_key, actual_this, strict };
diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h
index fb9d24bdd3..6fb61bad9f 100644
--- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h
+++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h
@@ -19,7 +19,7 @@ ObjectEnvironment* new_object_environment(Object&, bool is_with_environment, Env
Environment& get_this_environment(VM&);
Object* get_super_constructor(VM&);
Reference make_super_property_reference(GlobalObject&, Value actual_this, StringOrSymbol const& property_key, bool strict);
-Value require_object_coercible(GlobalObject&, Value);
+ThrowCompletionOr<Value> require_object_coercible(GlobalObject&, Value);
size_t length_of_array_like(GlobalObject&, Object const&);
MarkedValueList create_list_from_array_like(GlobalObject&, Value, Function<void(Value)> = {});
FunctionObject* species_constructor(GlobalObject&, Object const&, FunctionObject& default_constructor);
diff --git a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
index c7dbdcab68..cc389b5834 100644
--- a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
@@ -149,9 +149,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
auto proto = vm.argument(1);
// 1. Set O to ? RequireObjectCoercible(O).
- auto object = require_object_coercible(global_object, vm.argument(0));
- if (vm.exception())
- return {};
+ auto object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.argument(0)));
// 2. If Type(proto) is neither Object nor Null, throw a TypeError exception.
if (!proto.is_object() && !proto.is_null()) {
@@ -242,9 +240,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
// 20.1.2.7 Object.fromEntries ( iterable ), https://tc39.es/ecma262/#sec-object.fromentries
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::from_entries)
{
- auto iterable = require_object_coercible(global_object, vm.argument(0));
- if (vm.exception())
- return {};
+ auto iterable = TRY_OR_DISCARD(require_object_coercible(global_object, vm.argument(0)));
auto* object = Object::create(global_object, global_object.object_prototype());
diff --git a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp
index ee54bd1722..f70dd8cd53 100644
--- a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp
@@ -320,9 +320,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::proto_getter)
// B.2.2.1.2 set Object.prototype.__proto__, https://tc39.es/ecma262/#sec-set-object.prototype.__proto__
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::proto_setter)
{
- auto object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto proto = vm.argument(0);
if (!proto.is_object() && !proto.is_null())
diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
index 963a3d3379..a6da87d6e2 100644
--- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
@@ -30,17 +30,13 @@ namespace JS {
static Optional<String> ak_string_from(VM& vm, GlobalObject& global_object)
{
- auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_value = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
return this_value.to_string(global_object);
}
static Utf16String utf16_string_from(VM& vm, GlobalObject& global_object)
{
- auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_value = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
return this_value.to_utf16_string(global_object);
}
@@ -717,9 +713,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::slice)
// 22.1.3.21 String.prototype.split ( separator, limit ), https://tc39.es/ecma262/#sec-string.prototype.split
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
{
- auto object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto separator_argument = vm.argument(0);
auto limit_argument = vm.argument(1);
@@ -862,9 +856,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::at)
// 22.1.3.33 String.prototype [ @@iterator ] ( ), https://tc39.es/ecma262/#sec-string.prototype-@@iterator
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
{
- auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto string = this_object.to_string(global_object);
if (vm.exception())
return {};
@@ -874,9 +866,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::symbol_iterator)
// 22.1.3.11 String.prototype.match ( regexp ), https://tc39.es/ecma262/#sec-string.prototype.match
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
{
- auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto regexp = vm.argument(0);
if (!regexp.is_nullish()) {
if (auto* matcher = regexp.get_method(global_object, *vm.well_known_symbol_match()))
@@ -898,9 +888,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
// 22.1.3.12 String.prototype.matchAll ( regexp ), https://tc39.es/ecma262/#sec-string.prototype.matchall
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match_all)
{
- auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto regexp = vm.argument(0);
if (!regexp.is_nullish()) {
auto is_regexp = regexp.is_regexp(global_object);
@@ -910,9 +898,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match_all)
auto flags = regexp.as_object().get("flags");
if (vm.exception())
return {};
- auto flags_object = require_object_coercible(global_object, flags);
- if (vm.exception())
- return {};
+ auto flags_object = TRY_OR_DISCARD(require_object_coercible(global_object, flags));
auto flags_string = flags_object.to_string(global_object);
if (vm.exception())
return {};
@@ -940,9 +926,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match_all)
// 22.1.3.17 String.prototype.replace ( searchValue, replaceValue ), https://tc39.es/ecma262/#sec-string.prototype.replace
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
{
- auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto search_value = vm.argument(0);
auto replace_value = vm.argument(1);
@@ -1001,9 +985,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace)
// 22.1.3.18 String.prototype.replaceAll ( searchValue, replaceValue ), https://tc39.es/ecma262/#sec-string.prototype.replaceall
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace_all)
{
- auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto search_value = vm.argument(0);
auto replace_value = vm.argument(1);
@@ -1016,9 +998,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace_all)
auto flags = search_value.as_object().get(vm.names.flags);
if (vm.exception())
return {};
- auto flags_object = require_object_coercible(global_object, flags);
- if (vm.exception())
- return {};
+ auto flags_object = TRY_OR_DISCARD(require_object_coercible(global_object, flags));
auto flags_string = flags_object.to_string(global_object);
if (vm.exception())
return {};
@@ -1103,9 +1083,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::replace_all)
// 22.1.3.19 String.prototype.search ( regexp ), https://tc39.es/ecma262/#sec-string.prototype.search
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search)
{
- auto this_object = require_object_coercible(global_object, vm.this_value(global_object));
- if (vm.exception())
- return {};
+ auto this_object = TRY_OR_DISCARD(require_object_coercible(global_object, vm.this_value(global_object)));
auto regexp = vm.argument(0);
if (!regexp.is_nullish()) {
if (auto* searcher = regexp.get_method(global_object, *vm.well_known_symbol_search()))
@@ -1128,9 +1106,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search)
static Value create_html(GlobalObject& global_object, Value string, const String& tag, const String& attribute, Value value)
{
auto& vm = global_object.vm();
- require_object_coercible(global_object, string);
- if (vm.exception())
- return {};
+ TRY_OR_DISCARD(require_object_coercible(global_object, string));
auto str = string.to_string(global_object);
if (vm.exception())
return {};