From 1da66b5879294bd163157be7fbe347fd63c9ea1e Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 16 Oct 2022 14:57:29 +0200 Subject: [PATCH] LibJS: Implement the RegExpAlloc AO --- .../LibJS/Runtime/RegExpConstructor.cpp | 4 +++- .../Libraries/LibJS/Runtime/RegExpObject.cpp | 23 +++++++++++++++---- .../Libraries/LibJS/Runtime/RegExpObject.h | 5 ++-- .../LibJS/Runtime/StringPrototype.cpp | 6 ++--- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/RegExpConstructor.cpp b/Userland/Libraries/LibJS/Runtime/RegExpConstructor.cpp index 7f6daaeae6..1e57e4897d 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpConstructor.cpp @@ -82,7 +82,9 @@ ThrowCompletionOr RegExpConstructor::construct(FunctionObject&) flags_value = flags; } - return TRY(regexp_create(vm, pattern_value, flags_value)); + auto regexp_object = TRY(regexp_alloc(vm, new_target)); + + return TRY(regexp_object->regexp_initialize(vm, pattern_value, flags_value)).ptr(); } // 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species diff --git a/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp b/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp index 192b682dce..8e8038a5de 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpObject.cpp @@ -5,8 +5,10 @@ */ #include +#include #include #include +#include #include #include #include @@ -154,7 +156,7 @@ void RegExpObject::initialize(Realm& realm) } // 22.2.3.2.2 RegExpInitialize ( obj, pattern, flags ), https://tc39.es/ecma262/#sec-regexpinitialize -ThrowCompletionOr RegExpObject::regexp_initialize(VM& vm, Value pattern, Value flags) +ThrowCompletionOr> RegExpObject::regexp_initialize(VM& vm, Value pattern, Value flags) { String f; if (flags.is_undefined()) { @@ -190,7 +192,7 @@ ThrowCompletionOr RegExpObject::regexp_initialize(VM& vm, Value p TRY(set(vm.names.lastIndex, Value(0), Object::ShouldThrowExceptions::Yes)); - return this; + return NonnullGCPtr { *this }; } // 22.2.3.2.5 EscapeRegExpPattern ( P, F ), https://tc39.es/ecma262/#sec-escaperegexppattern @@ -203,11 +205,24 @@ String RegExpObject::escape_regexp_pattern() const } // 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate -ThrowCompletionOr regexp_create(VM& vm, Value pattern, Value flags) +ThrowCompletionOr> regexp_create(VM& vm, Value pattern, Value flags) { auto& realm = *vm.current_realm(); - auto* regexp_object = RegExpObject::create(realm); + auto regexp_object = MUST(regexp_alloc(vm, *realm.intrinsics().regexp_constructor())); return TRY(regexp_object->regexp_initialize(vm, pattern, flags)); } +// 22.2.3.2 RegExpAlloc ( newTarget ), https://tc39.es/ecma262/#sec-regexpalloc +ThrowCompletionOr> regexp_alloc(VM& vm, FunctionObject& new_target) +{ + // 1. Let obj be ? OrdinaryCreateFromConstructor(newTarget, "%RegExp.prototype%", « [[OriginalSource]], [[OriginalFlags]], [[RegExpRecord]], [[RegExpMatcher]] »). + auto* regexp_object = TRY(ordinary_create_from_constructor(vm, new_target, &Intrinsics::regexp_prototype)); + + // 2. Perform ! DefinePropertyOrThrow(obj, "lastIndex", PropertyDescriptor { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }). + MUST(regexp_object->define_property_or_throw(vm.names.lastIndex, PropertyDescriptor { .writable = true, .enumerable = false, .configurable = false })); + + // 3. Return obj. + return NonnullGCPtr { *regexp_object }; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/RegExpObject.h b/Userland/Libraries/LibJS/Runtime/RegExpObject.h index 3865a0b775..719c766696 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpObject.h +++ b/Userland/Libraries/LibJS/Runtime/RegExpObject.h @@ -14,7 +14,8 @@ namespace JS { -ThrowCompletionOr regexp_create(VM&, Value pattern, Value flags); +ThrowCompletionOr> regexp_create(VM&, Value pattern, Value flags); +ThrowCompletionOr> regexp_alloc(VM&, FunctionObject& new_target); Result, String> regex_flags_from_string(StringView flags); struct ParseRegexPatternError { @@ -39,7 +40,7 @@ public: static RegExpObject* create(Realm&); static RegExpObject* create(Realm&, Regex regex, String pattern, String flags); - ThrowCompletionOr regexp_initialize(VM&, Value pattern, Value flags); + ThrowCompletionOr> regexp_initialize(VM&, Value pattern, Value flags); String escape_regexp_pattern() const; virtual void initialize(Realm&) override; diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 371237d949..2142f0fe55 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -826,7 +826,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match) auto string = TRY(this_object.to_utf16_string(vm)); - auto* rx = TRY(regexp_create(vm, regexp, js_undefined())); + auto rx = TRY(regexp_create(vm, regexp, js_undefined())); return TRY(Value(rx).invoke(vm, *vm.well_known_symbol_match(), js_string(vm, move(string)))); } @@ -850,7 +850,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match_all) auto string = TRY(this_object.to_utf16_string(vm)); - auto* rx = TRY(regexp_create(vm, regexp, js_string(vm, "g"))); + auto rx = TRY(regexp_create(vm, regexp, js_string(vm, "g"))); return TRY(Value(rx).invoke(vm, *vm.well_known_symbol_match_all(), js_string(vm, move(string)))); } @@ -1003,7 +1003,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search) auto string = TRY(this_object.to_utf16_string(vm)); - auto* rx = TRY(regexp_create(vm, regexp, js_undefined())); + auto rx = TRY(regexp_create(vm, regexp, js_undefined())); return TRY(Value(rx).invoke(vm, *vm.well_known_symbol_search(), js_string(vm, move(string)))); }