1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:17:46 +00:00

LibJS: Add TypedArray support to ArrayIterator

ArrayIteratorPrototype::next seems to implement CreateArrayIterator,
which is an issue for a separate PR.
This commit is contained in:
Luke 2021-06-26 00:58:33 +01:00 committed by Linus Groh
parent 787f80d114
commit 293974c1cb

View file

@ -4,12 +4,14 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayIterator.h>
#include <LibJS/Runtime/ArrayIteratorPrototype.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/TypedArray.h>
namespace JS {
@ -34,6 +36,7 @@ ArrayIteratorPrototype::~ArrayIteratorPrototype()
}
// 23.1.5.2.1 %ArrayIteratorPrototype%.next ( ), https://tc39.es/ecma262/#sec-%arrayiteratorprototype%.next
// FIXME: This seems to be CreateArrayIterator (https://tc39.es/ecma262/#sec-createarrayiterator) instead of %ArrayIteratorPrototype%.next.
JS_DEFINE_NATIVE_FUNCTION(ArrayIteratorPrototype::next)
{
auto this_value = vm.this_value(global_object);
@ -51,8 +54,23 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayIteratorPrototype::next)
auto index = iterator.index();
auto iteration_kind = iterator.iteration_kind();
// FIXME: Typed array check
auto length = array.indexed_properties().array_like_size();
size_t length;
if (array.is_typed_array()) {
auto& typed_array = static_cast<TypedArrayBase&>(array);
if (typed_array.viewed_array_buffer()->is_detached()) {
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
length = typed_array.array_length();
} else {
length = length_of_array_like(global_object, array);
if (vm.exception())
return {};
}
if (index >= length) {
iterator.m_array = js_undefined();