mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-08 00:50:32 +00:00
LibJS: Implement RegExp.prototype.compile
This is an Annex B extension to RegExp.prototype.
This commit is contained in:
parent
562d4e497b
commit
6337eb52d8
|
@ -94,6 +94,7 @@ namespace JS {
|
|||
P(clz32) \
|
||||
P(codePointAt) \
|
||||
P(compareExchange) \
|
||||
P(compile) \
|
||||
P(concat) \
|
||||
P(configurable) \
|
||||
P(console) \
|
||||
|
|
|
@ -61,6 +61,7 @@
|
|||
M(NotASymbol, "{} is not a symbol") \
|
||||
M(NotIterable, "{} is not iterable") \
|
||||
M(NotObjectCoercible, "{} cannot be converted to an object") \
|
||||
M(NotUndefined, "{} is not undefined") \
|
||||
M(ObjectDefineOwnPropertyReturnedFalse, "Object's [[DefineOwnProperty]] method returned false") \
|
||||
M(ObjectDeleteReturnedFalse, "Object's [[Delete]] method returned false") \
|
||||
M(ObjectFreezeFailed, "Could not freeze object") \
|
||||
|
|
|
@ -35,6 +35,7 @@ void RegExpPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(vm.names.test, test, 1, attr);
|
||||
define_native_function(vm.names.exec, exec, 1, attr);
|
||||
define_native_function(vm.names.compile, compile, 2, attr);
|
||||
|
||||
define_native_function(*vm.well_known_symbol_match(), symbol_match, 1, attr);
|
||||
define_native_function(*vm.well_known_symbol_match_all(), symbol_match_all, 1, attr);
|
||||
|
@ -920,4 +921,34 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
|||
return array;
|
||||
}
|
||||
|
||||
// B.2.4.1 RegExp.prototype.compile ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp.prototype.compile
|
||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::compile)
|
||||
{
|
||||
auto* regexp_object = regexp_object_from(vm, global_object);
|
||||
if (!regexp_object)
|
||||
return {};
|
||||
|
||||
auto pattern = vm.argument(0);
|
||||
auto flags = vm.argument(1);
|
||||
|
||||
Value pattern_value;
|
||||
Value flags_value;
|
||||
|
||||
if (pattern.is_object() && is<RegExpObject>(pattern.as_object())) {
|
||||
if (!flags.is_undefined()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotUndefined, flags.to_string_without_side_effects());
|
||||
return {};
|
||||
}
|
||||
|
||||
auto& regexp_pattern = static_cast<RegExpObject&>(pattern.as_object());
|
||||
pattern_value = js_string(vm, regexp_pattern.pattern());
|
||||
flags_value = js_string(vm, regexp_pattern.flags());
|
||||
} else {
|
||||
pattern_value = pattern;
|
||||
flags_value = flags;
|
||||
}
|
||||
|
||||
return regexp_object->regexp_initialize(global_object, pattern_value, flags_value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(symbol_replace);
|
||||
JS_DECLARE_NATIVE_FUNCTION(symbol_search);
|
||||
JS_DECLARE_NATIVE_FUNCTION(symbol_split);
|
||||
JS_DECLARE_NATIVE_FUNCTION(compile);
|
||||
|
||||
#define __JS_ENUMERATE(_, flag_name, ...) \
|
||||
JS_DECLARE_NATIVE_GETTER(flag_name);
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
test("basic functionality", () => {
|
||||
let re = /foo/;
|
||||
expect(re.compile.length).toBe(2);
|
||||
|
||||
re.compile("bar");
|
||||
expect(re.test("foo")).toBeFalse();
|
||||
expect(re.test("bar")).toBeTrue();
|
||||
|
||||
expect(re.unicode).toBeFalse();
|
||||
re.compile("bar", "u");
|
||||
expect(re.unicode).toBeTrue();
|
||||
|
||||
re.compile(/baz/g);
|
||||
expect(re.global).toBeTrue();
|
||||
expect(re.test("bar")).toBeFalse();
|
||||
expect(re.test("baz")).toBeTrue();
|
||||
});
|
Loading…
Reference in a new issue