LibJS: Convert validate_typed_array() to ThrowCompletionOr

Also add spec step comments to it while we're here.
This commit is contained in:
Linus Groh 2021-10-03 20:20:19 +01:00
parent d7d73f9100
commit 3655aee543
Notes: sideshowbarker 2024-07-18 03:06:16 +09:00
4 changed files with 20 additions and 12 deletions

View File

@ -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();

View File

@ -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

View File

@ -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);

View File

@ -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;
}