1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:37:34 +00:00

LibJS: Convert ArrayIterator.prototype to be a PrototypeObject

This commit is contained in:
Timothy Flynn 2021-09-11 16:03:50 -04:00 committed by Andreas Kling
parent bd4c116d08
commit 4d1d0f05a9
2 changed files with 13 additions and 16 deletions

View file

@ -6,7 +6,6 @@
#include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayIterator.h>
#include <LibJS/Runtime/ArrayIteratorPrototype.h> #include <LibJS/Runtime/ArrayIteratorPrototype.h>
#include <LibJS/Runtime/Error.h> #include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
@ -16,7 +15,7 @@
namespace JS { namespace JS {
ArrayIteratorPrototype::ArrayIteratorPrototype(GlobalObject& global_object) ArrayIteratorPrototype::ArrayIteratorPrototype(GlobalObject& global_object)
: Object(*global_object.iterator_prototype()) : PrototypeObject(*global_object.iterator_prototype())
{ {
} }
@ -39,21 +38,18 @@ ArrayIteratorPrototype::~ArrayIteratorPrototype()
// FIXME: This seems to be CreateArrayIterator (https://tc39.es/ecma262/#sec-createarrayiterator) instead of %ArrayIteratorPrototype%.next. // FIXME: This seems to be CreateArrayIterator (https://tc39.es/ecma262/#sec-createarrayiterator) instead of %ArrayIteratorPrototype%.next.
JS_DEFINE_NATIVE_FUNCTION(ArrayIteratorPrototype::next) JS_DEFINE_NATIVE_FUNCTION(ArrayIteratorPrototype::next)
{ {
auto this_value = vm.this_value(global_object); auto* iterator = typed_this_value(global_object);
if (!this_value.is_object() || !is<ArrayIterator>(this_value.as_object())) { if (vm.exception())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOfType, "Array Iterator");
return {}; return {};
}
auto& this_object = this_value.as_object(); auto target_array = iterator->array();
auto& iterator = static_cast<ArrayIterator&>(this_object);
auto target_array = iterator.array();
if (target_array.is_undefined()) if (target_array.is_undefined())
return create_iterator_result_object(global_object, js_undefined(), true); return create_iterator_result_object(global_object, js_undefined(), true);
VERIFY(target_array.is_object()); VERIFY(target_array.is_object());
auto& array = target_array.as_object(); auto& array = target_array.as_object();
auto index = iterator.index(); auto index = iterator->index();
auto iteration_kind = iterator.iteration_kind(); auto iteration_kind = iterator->iteration_kind();
size_t length; size_t length;
@ -73,11 +69,11 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayIteratorPrototype::next)
} }
if (index >= length) { if (index >= length) {
iterator.m_array = js_undefined(); iterator->m_array = js_undefined();
return create_iterator_result_object(global_object, js_undefined(), true); return create_iterator_result_object(global_object, js_undefined(), true);
} }
iterator.m_index++; iterator->m_index++;
if (iteration_kind == Object::PropertyKind::Key) if (iteration_kind == Object::PropertyKind::Key)
return create_iterator_result_object(global_object, Value(static_cast<i32>(index)), false); return create_iterator_result_object(global_object, Value(static_cast<i32>(index)), false);

View file

@ -6,12 +6,13 @@
#pragma once #pragma once
#include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/ArrayIterator.h>
#include <LibJS/Runtime/PrototypeObject.h>
namespace JS { namespace JS {
class ArrayIteratorPrototype final : public Object { class ArrayIteratorPrototype final : public PrototypeObject<ArrayIteratorPrototype, ArrayIterator> {
JS_OBJECT(ArrayIteratorPrototype, Object) JS_PROTOTYPE_OBJECT(ArrayIteratorPrototype, ArrayIterator, ArrayIterator);
public: public:
ArrayIteratorPrototype(GlobalObject&); ArrayIteratorPrototype(GlobalObject&);