diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index b5935095116..d5feac0a6df 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -19,8 +19,8 @@ static void validate_integer_typed_array(GlobalObject& global_object, TypedArray { auto& vm = global_object.vm(); - validate_typed_array(global_object, typed_array); - if (vm.exception()) + auto maybe_error = validate_typed_array(global_object, typed_array); + if (maybe_error.is_error()) return; auto type_name = typed_array.element_name(); diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index 91905de3267..f0ffee1326a 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -31,14 +32,24 @@ TypedArrayBase* typed_array_from(GlobalObject& global_object, Value typed_array_ } // 23.2.4.3 ValidateTypedArray ( O ), https://tc39.es/ecma262/#sec-validatetypedarray -void validate_typed_array(GlobalObject& global_object, TypedArrayBase& typed_array) +ThrowCompletionOr validate_typed_array(GlobalObject& global_object, TypedArrayBase& typed_array) { auto& vm = global_object.vm(); + // 1. Perform ? RequireInternalSlot(O, [[TypedArrayName]]). if (!typed_array.is_typed_array()) - vm.throw_exception(global_object, ErrorType::NotAnObjectOfType, "TypedArray"); - else if (typed_array.viewed_array_buffer()->is_detached()) - vm.throw_exception(global_object, ErrorType::DetachedArrayBuffer); + return vm.throw_completion(global_object, ErrorType::NotAnObjectOfType, "TypedArray"); + + // 2. Assert: O has a [[ViewedArrayBuffer]] internal slot. + + // 3. Let buffer be O.[[ViewedArrayBuffer]]. + auto* buffer = typed_array.viewed_array_buffer(); + + // 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + if (buffer->is_detached()) + return vm.throw_completion(global_object, ErrorType::DetachedArrayBuffer); + + return {}; } // 22.2.5.1.3 InitializeTypedArrayFromArrayBuffer, https://tc39.es/ecma262/#sec-initializetypedarrayfromarraybuffer diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 10c4bc1d471..d8b4e88805b 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -20,7 +21,7 @@ namespace JS { class TypedArrayBase; TypedArrayBase* typed_array_from(GlobalObject&, Value); -void validate_typed_array(GlobalObject&, TypedArrayBase&); +ThrowCompletionOr validate_typed_array(GlobalObject&, TypedArrayBase&); class TypedArrayBase : public Object { JS_OBJECT(TypedArrayBase, Object); diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index d6607bbfd6e..db35a32ca46 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -75,15 +75,11 @@ static TypedArrayBase* typed_array_from_this(GlobalObject& global_object) static TypedArrayBase* validate_typed_array_from_this(GlobalObject& global_object) { - auto& vm = global_object.vm(); - auto* typed_array = typed_array_from_this(global_object); if (!typed_array) return nullptr; - validate_typed_array(global_object, *typed_array); - if (vm.exception()) - return nullptr; + TRY_OR_DISCARD(validate_typed_array(global_object, *typed_array)); return typed_array; }