mirror of
https://github.com/RGBCube/serenity
synced 2025-07-19 19:07:34 +00:00
LibJS: Make RegExp() constructor spec-compliant
- Default values should depend on arguments being undefined, not being missing - "(?:)" for empty pattern happens in RegExp.prototype.source, not the constructor
This commit is contained in:
parent
99536449d5
commit
5cb45e4feb
2 changed files with 21 additions and 8 deletions
|
@ -56,14 +56,18 @@ Value RegExpConstructor::call()
|
||||||
Value RegExpConstructor::construct(Function&)
|
Value RegExpConstructor::construct(Function&)
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
auto& vm = this->vm();
|
||||||
if (!vm.argument_count())
|
String pattern = "";
|
||||||
return RegExpObject::create(global_object(), "(?:)", "");
|
String flags = "";
|
||||||
auto pattern = vm.argument(0).to_string(global_object());
|
if (!vm.argument(0).is_undefined()) {
|
||||||
|
pattern = vm.argument(0).to_string(global_object());
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
auto flags = vm.argument_count() > 1 ? vm.argument(1).to_string(global_object()) : "";
|
}
|
||||||
|
if (!vm.argument(1).is_undefined()) {
|
||||||
|
flags = vm.argument(1).to_string(global_object());
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
}
|
||||||
return RegExpObject::create(global_object(), pattern, flags);
|
return RegExpObject::create(global_object(), pattern, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
Libraries/LibJS/Tests/builtins/RegExp/RegExp.js
Normal file
9
Libraries/LibJS/Tests/builtins/RegExp/RegExp.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
// FIXME: update when toString is spec-compliant
|
||||||
|
expect(RegExp().toString()).toBe("//");
|
||||||
|
expect(RegExp(undefined).toString()).toBe("//");
|
||||||
|
expect(RegExp("foo").toString()).toBe("/foo/");
|
||||||
|
expect(RegExp("foo", undefined).toString()).toBe("/foo/");
|
||||||
|
expect(RegExp("foo", "g").toString()).toBe("/foo/g");
|
||||||
|
expect(RegExp(undefined, "g").toString()).toBe("//g");
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue