Skip to content

Commit

Permalink
LibJS: Implement RegExp.prototype.compile
Browse files Browse the repository at this point in the history
This is an Annex B extension to RegExp.prototype.
  • Loading branch information
trflynn89 authored and awesomekling committed Aug 20, 2021
1 parent 562d4e4 commit 6337eb5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace JS {
P(clz32) \
P(codePointAt) \
P(compareExchange) \
P(compile) \
P(concat) \
P(configurable) \
P(console) \
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/ErrorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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") \
Expand Down
31 changes: 31 additions & 0 deletions Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

}
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/RegExpPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RegExpPrototype final : public Object {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 6337eb5

Please sign in to comment.