mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
LibJS: Make RegExp.prototype.toString() spec-compliant
It should use the 'source' and 'flags' properties of the object, and therefore work with non-RegExp objects as well.
This commit is contained in:
parent
ee66eaa1b0
commit
b6e5442d55
2 changed files with 19 additions and 3 deletions
|
@ -248,10 +248,25 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
|||
|
||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
|
||||
{
|
||||
auto* regexp_object = regexp_object_from(vm, global_object);
|
||||
if (!regexp_object)
|
||||
auto this_object = this_object_from(vm, global_object);
|
||||
if (!this_object)
|
||||
return {};
|
||||
return js_string(vm, String::formatted("/{}/{}", regexp_object->pattern(), regexp_object->flags()));
|
||||
|
||||
auto source_attr = this_object->get(vm.names.source).value_or(js_undefined());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto pattern = source_attr.to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto flags_attr = this_object->get(vm.names.flags).value_or(js_undefined());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto flags = flags_attr.to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
return js_string(vm, String::formatted("/{}/{}", pattern, flags));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,4 +2,5 @@ test("basic functionality", () => {
|
|||
expect(RegExp.prototype.toString).toHaveLength(0);
|
||||
|
||||
expect(/test/g.toString()).toBe("/test/g");
|
||||
expect(RegExp.prototype.toString.call({ source: "test", flags: "g" })).toBe("/test/g");
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue