1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:57:45 +00:00

LibJS: Convert RegExpStringIterator.prototype to be a PrototypeObject

This commit is contained in:
Timothy Flynn 2021-09-11 15:37:03 -04:00 committed by Andreas Kling
parent 65b0c26c44
commit b749194e70
2 changed files with 15 additions and 18 deletions

View file

@ -7,14 +7,13 @@
#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h> #include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/RegExpPrototype.h> #include <LibJS/Runtime/RegExpPrototype.h>
#include <LibJS/Runtime/RegExpStringIterator.h>
#include <LibJS/Runtime/RegExpStringIteratorPrototype.h> #include <LibJS/Runtime/RegExpStringIteratorPrototype.h>
#include <LibJS/Runtime/Utf16String.h> #include <LibJS/Runtime/Utf16String.h>
namespace JS { namespace JS {
RegExpStringIteratorPrototype::RegExpStringIteratorPrototype(GlobalObject& global_object) RegExpStringIteratorPrototype::RegExpStringIteratorPrototype(GlobalObject& global_object)
: Object(*global_object.iterator_prototype()) : PrototypeObject(*global_object.iterator_prototype())
{ {
} }
@ -34,27 +33,24 @@ void RegExpStringIteratorPrototype::initialize(GlobalObject& global_object)
JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next) JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next)
{ {
// For details, see the 'closure' of: https://tc39.es/ecma262/#sec-createregexpstringiterator // For details, see the 'closure' of: https://tc39.es/ecma262/#sec-createregexpstringiterator
auto this_value = vm.this_value(global_object); auto* iterator = typed_this_value(global_object);
if (!this_value.is_object() || !is<RegExpStringIterator>(this_value.as_object())) { if (vm.exception())
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObjectOfType, "RegExp String Iterator");
return {}; return {};
}
auto& iterator = static_cast<RegExpStringIterator&>(this_value.as_object()); if (iterator->done())
if (iterator.done())
return create_iterator_result_object(global_object, js_undefined(), true); return create_iterator_result_object(global_object, js_undefined(), true);
auto match = regexp_exec(global_object, iterator.regexp_object(), iterator.string()); auto match = regexp_exec(global_object, iterator->regexp_object(), iterator->string());
if (vm.exception()) if (vm.exception())
return {}; return {};
if (match.is_null()) { if (match.is_null()) {
iterator.set_done(); iterator->set_done();
return create_iterator_result_object(global_object, js_undefined(), true); return create_iterator_result_object(global_object, js_undefined(), true);
} }
if (!iterator.global()) { if (!iterator->global()) {
iterator.set_done(); iterator->set_done();
return create_iterator_result_object(global_object, match, false); return create_iterator_result_object(global_object, match, false);
} }
@ -69,16 +65,16 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpStringIteratorPrototype::next)
return {}; return {};
if (match_string.is_empty()) { if (match_string.is_empty()) {
auto last_index_value = iterator.regexp_object().get(vm.names.lastIndex); auto last_index_value = iterator->regexp_object().get(vm.names.lastIndex);
if (vm.exception()) if (vm.exception())
return {}; return {};
auto last_index = last_index_value.to_length(global_object); auto last_index = last_index_value.to_length(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
last_index = advance_string_index(iterator.string().view(), last_index, iterator.unicode()); last_index = advance_string_index(iterator->string().view(), last_index, iterator->unicode());
iterator.regexp_object().set(vm.names.lastIndex, Value(last_index), Object::ShouldThrowExceptions::Yes); iterator->regexp_object().set(vm.names.lastIndex, Value(last_index), Object::ShouldThrowExceptions::Yes);
if (vm.exception()) if (vm.exception())
return {}; return {};
} }

View file

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