1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00
ladybird/Libraries/LibJS/Runtime/StringIterator.cpp
Aliaksandr Kalenik f405d71657 LibJS: Disable optimization in IteratorNextUnpack if next() is redefined
81b6a11 regressed correctness by always bypassing the `next()` method
resolution for built-in iterators, causing incorrect behavior when
`next()` was redefined on built-in prototypes. This change fixes the
issue by storing a flag on built-in prototypes indicating whether
`next()` has ever been redefined.
2025-05-12 07:41:29 -04:00

52 lines
1.3 KiB
C++

/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Utf8View.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/StringIterator.h>
#include <LibJS/Runtime/StringIteratorPrototype.h>
namespace JS {
GC_DEFINE_ALLOCATOR(StringIterator);
GC::Ref<StringIterator> StringIterator::create(Realm& realm, String string)
{
return realm.create<StringIterator>(move(string), realm.intrinsics().string_iterator_prototype());
}
StringIterator::StringIterator(String string, Object& prototype)
: Object(ConstructWithPrototypeTag::Tag, prototype)
, m_string(move(string))
, m_iterator(Utf8View(m_string).begin())
{
auto& string_iterator_prototype = as<StringIteratorPrototype>(prototype);
m_next_method_was_redefined = string_iterator_prototype.next_method_was_redefined();
}
ThrowCompletionOr<void> StringIterator::next(VM& vm, bool& done, Value& value)
{
if (m_done) {
done = true;
value = js_undefined();
return {};
}
if (m_iterator.done()) {
m_done = true;
done = true;
value = js_undefined();
return {};
}
auto code_point = String::from_code_point(*m_iterator);
++m_iterator;
value = PrimitiveString::create(vm, move(code_point));
return {};
}
}