diff options
author | Linus Groh <mail@linusgroh.de> | 2021-10-03 20:20:19 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-10-04 09:52:15 +0100 |
commit | 3655aee54301c7854f296f4f468353cb315e3943 (patch) | |
tree | 77094ecc3586b086ed9db934c84abfe272d28cd9 | |
parent | d7d73f910061db9cf78d7ddcf12c35beff7c01f9 (diff) | |
download | serenity-3655aee54301c7854f296f4f468353cb315e3943.zip |
LibJS: Convert validate_typed_array() to ThrowCompletionOr
Also add spec step comments to it while we're here.
4 files changed, 20 insertions, 12 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index b593509511..d5feac0a6d 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 91905de326..f0ffee1326 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -8,6 +8,7 @@ #include <AK/Checked.h> #include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/ArrayBuffer.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/IteratorOperations.h> #include <LibJS/Runtime/TypedArray.h> @@ -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<void> 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<TypeError>(global_object, ErrorType::NotAnObjectOfType, "TypedArray"); - else if (typed_array.viewed_array_buffer()->is_detached()) - vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer); + return vm.throw_completion<TypeError>(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<TypeError>(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 10c4bc1d47..d8b4e88805 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -9,6 +9,7 @@ #include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/ArrayBuffer.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/PropertyDescriptor.h> #include <LibJS/Runtime/PropertyName.h> @@ -20,7 +21,7 @@ namespace JS { class TypedArrayBase; TypedArrayBase* typed_array_from(GlobalObject&, Value); -void validate_typed_array(GlobalObject&, TypedArrayBase&); +ThrowCompletionOr<void> 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 d6607bbfd6..db35a32ca4 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; } |