mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:47:34 +00:00
LibJS: Implement the RegExpAlloc AO
This commit is contained in:
parent
db31c22491
commit
1da66b5879
4 changed files with 28 additions and 10 deletions
|
@ -82,7 +82,9 @@ ThrowCompletionOr<Object*> RegExpConstructor::construct(FunctionObject&)
|
||||||
flags_value = flags;
|
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
|
// 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species
|
||||||
|
|
|
@ -5,8 +5,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/PrimitiveString.h>
|
#include <LibJS/Runtime/PrimitiveString.h>
|
||||||
|
#include <LibJS/Runtime/RegExpConstructor.h>
|
||||||
#include <LibJS/Runtime/RegExpObject.h>
|
#include <LibJS/Runtime/RegExpObject.h>
|
||||||
#include <LibJS/Runtime/StringPrototype.h>
|
#include <LibJS/Runtime/StringPrototype.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
@ -154,7 +156,7 @@ void RegExpObject::initialize(Realm& realm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 22.2.3.2.2 RegExpInitialize ( obj, pattern, flags ), https://tc39.es/ecma262/#sec-regexpinitialize
|
// 22.2.3.2.2 RegExpInitialize ( obj, pattern, flags ), https://tc39.es/ecma262/#sec-regexpinitialize
|
||||||
ThrowCompletionOr<RegExpObject*> RegExpObject::regexp_initialize(VM& vm, Value pattern, Value flags)
|
ThrowCompletionOr<NonnullGCPtr<RegExpObject>> RegExpObject::regexp_initialize(VM& vm, Value pattern, Value flags)
|
||||||
{
|
{
|
||||||
String f;
|
String f;
|
||||||
if (flags.is_undefined()) {
|
if (flags.is_undefined()) {
|
||||||
|
@ -190,7 +192,7 @@ ThrowCompletionOr<RegExpObject*> RegExpObject::regexp_initialize(VM& vm, Value p
|
||||||
|
|
||||||
TRY(set(vm.names.lastIndex, Value(0), Object::ShouldThrowExceptions::Yes));
|
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
|
// 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
|
// 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate
|
||||||
ThrowCompletionOr<RegExpObject*> regexp_create(VM& vm, Value pattern, Value flags)
|
ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_create(VM& vm, Value pattern, Value flags)
|
||||||
{
|
{
|
||||||
auto& realm = *vm.current_realm();
|
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));
|
return TRY(regexp_object->regexp_initialize(vm, pattern, flags));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 22.2.3.2 RegExpAlloc ( newTarget ), https://tc39.es/ecma262/#sec-regexpalloc
|
||||||
|
ThrowCompletionOr<NonnullGCPtr<RegExpObject>> 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<RegExpObject>(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 };
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,8 @@
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
ThrowCompletionOr<RegExpObject*> regexp_create(VM&, Value pattern, Value flags);
|
ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_create(VM&, Value pattern, Value flags);
|
||||||
|
ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_alloc(VM&, FunctionObject& new_target);
|
||||||
|
|
||||||
Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(StringView flags);
|
Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(StringView flags);
|
||||||
struct ParseRegexPatternError {
|
struct ParseRegexPatternError {
|
||||||
|
@ -39,7 +40,7 @@ public:
|
||||||
static RegExpObject* create(Realm&);
|
static RegExpObject* create(Realm&);
|
||||||
static RegExpObject* create(Realm&, Regex<ECMA262> regex, String pattern, String flags);
|
static RegExpObject* create(Realm&, Regex<ECMA262> regex, String pattern, String flags);
|
||||||
|
|
||||||
ThrowCompletionOr<RegExpObject*> regexp_initialize(VM&, Value pattern, Value flags);
|
ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_initialize(VM&, Value pattern, Value flags);
|
||||||
String escape_regexp_pattern() const;
|
String escape_regexp_pattern() const;
|
||||||
|
|
||||||
virtual void initialize(Realm&) override;
|
virtual void initialize(Realm&) override;
|
||||||
|
|
|
@ -826,7 +826,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
|
||||||
|
|
||||||
auto string = TRY(this_object.to_utf16_string(vm));
|
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))));
|
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 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))));
|
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 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))));
|
return TRY(Value(rx).invoke(vm, *vm.well_known_symbol_search(), js_string(vm, move(string))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue