From 3edf86462bc81ea6e0efce0ddf51d45318508cb5 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 23 Oct 2021 13:57:04 -0400 Subject: [PATCH] LibJS: Convert typed_array_from to ThrowCompletionOr --- .../Libraries/LibJS/Runtime/AtomicsObject.cpp | 36 +++++-------------- .../Libraries/LibJS/Runtime/TypedArray.cpp | 10 +++--- Userland/Libraries/LibJS/Runtime/TypedArray.h | 2 +- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index b1e9451d995..f3bc20f3de7 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -164,9 +164,7 @@ void AtomicsObject::initialize(GlobalObject& global_object) // 25.4.3 Atomics.add ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.add JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::add) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); auto atomic_add = [](auto* storage, auto value) { return AK::atomic_fetch_add(storage, value); }; @@ -182,9 +180,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::add) // 25.4.4 Atomics.and ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.and JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::and_) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); auto atomic_and = [](auto* storage, auto value) { return AK::atomic_fetch_and(storage, value); }; @@ -280,9 +276,7 @@ static ThrowCompletionOr atomic_compare_exchange_impl(GlobalObject& globa // 25.4.5 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue ), https://tc39.es/ecma262/#sec-atomics.compareexchange JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::compare_exchange) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \ if (is(typed_array)) \ @@ -296,9 +290,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::compare_exchange) // 25.4.6 Atomics.exchange ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.exchange JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::exchange) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); auto atomic_exchange = [](auto* storage, auto value) { return AK::atomic_exchange(storage, value); }; @@ -329,9 +321,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::is_lock_free) // 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::load) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); TRY_OR_DISCARD(validate_integer_typed_array(global_object, *typed_array)); @@ -348,9 +338,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::load) // 25.4.9 Atomics.or ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.or JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::or_) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); auto atomic_or = [](auto* storage, auto value) { return AK::atomic_fetch_or(storage, value); }; @@ -366,9 +354,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::or_) // 25.4.10 Atomics.store ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.store JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::store) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); TRY_OR_DISCARD(validate_integer_typed_array(global_object, *typed_array)); @@ -393,9 +379,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::store) // 25.4.11 Atomics.sub ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.sub JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::sub) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); auto atomic_sub = [](auto* storage, auto value) { return AK::atomic_fetch_sub(storage, value); }; @@ -411,9 +395,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::sub) // 25.4.14 Atomics.xor ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.xor JS_DEFINE_OLD_NATIVE_FUNCTION(AtomicsObject::xor_) { - auto* typed_array = typed_array_from(global_object, vm.argument(0)); - if (!typed_array) - return {}; + auto* typed_array = TRY_OR_DISCARD(typed_array_from(global_object, vm.argument(0))); auto atomic_xor = [](auto* storage, auto value) { return AK::atomic_fetch_xor(storage, value); }; diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index 3f0de1a103d..d9a8e9788de 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -17,15 +17,13 @@ namespace JS { -TypedArrayBase* typed_array_from(GlobalObject& global_object, Value typed_array_value) +ThrowCompletionOr typed_array_from(GlobalObject& global_object, Value typed_array_value) { auto& vm = global_object.vm(); - auto* this_object = TRY_OR_DISCARD(typed_array_value.to_object(global_object)); - if (!this_object->is_typed_array()) { - vm.throw_exception(global_object, ErrorType::NotAnObjectOfType, "TypedArray"); - return nullptr; - } + auto* this_object = TRY(typed_array_value.to_object(global_object)); + if (!this_object->is_typed_array()) + return vm.throw_completion(global_object, ErrorType::NotAnObjectOfType, "TypedArray"); return static_cast(this_object); } diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index c758ea6208a..a19c9e2b29d 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -20,7 +20,7 @@ namespace JS { class TypedArrayBase; -TypedArrayBase* typed_array_from(GlobalObject&, Value); +ThrowCompletionOr typed_array_from(GlobalObject&, Value); ThrowCompletionOr validate_typed_array(GlobalObject&, TypedArrayBase&); class TypedArrayBase : public Object {