diff --git a/Libraries/LibJS/Runtime/RegExpObject.cpp b/Libraries/LibJS/Runtime/RegExpObject.cpp index 88dbcab7ea..4aab7dff13 100644 --- a/Libraries/LibJS/Runtime/RegExpObject.cpp +++ b/Libraries/LibJS/Runtime/RegExpObject.cpp @@ -49,9 +49,4 @@ RegExpObject::~RegExpObject() { } -Value RegExpObject::to_string() const -{ - return js_string(heap(), String::formatted("/{}/{}", content(), flags())); -} - } diff --git a/Libraries/LibJS/Runtime/RegExpObject.h b/Libraries/LibJS/Runtime/RegExpObject.h index 61d44b2a6f..e15694bf5a 100644 --- a/Libraries/LibJS/Runtime/RegExpObject.h +++ b/Libraries/LibJS/Runtime/RegExpObject.h @@ -43,8 +43,6 @@ public: const String& content() const { return m_content; } const String& flags() const { return m_flags; } - Value to_string() const override; - private: virtual bool is_regexp_object() const override { return true; } diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 7aa784a883..7be563acf5 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -25,10 +25,8 @@ */ #include -#include -#include +#include #include -#include #include #include @@ -39,8 +37,36 @@ RegExpPrototype::RegExpPrototype(GlobalObject& global_object) { } +void RegExpPrototype::initialize(GlobalObject& global_object) +{ + auto& vm = this->vm(); + Object::initialize(global_object); + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.toString, to_string, 0, attr); +} + RegExpPrototype::~RegExpPrototype() { } +static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object) +{ + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return nullptr; + if (!this_object->is_regexp_object()) { + vm.throw_exception(global_object, ErrorType::NotA, "RegExp"); + return nullptr; + } + return static_cast(this_object); +} + +JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string) +{ + auto* regexp_object = regexp_object_from(vm, global_object); + if (!regexp_object) + return {}; + return js_string(vm, String::formatted("/{}/{}", regexp_object->content(), regexp_object->flags())); +} + } diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.h b/Libraries/LibJS/Runtime/RegExpPrototype.h index bfd42abd16..1949dea6b6 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.h +++ b/Libraries/LibJS/Runtime/RegExpPrototype.h @@ -35,7 +35,11 @@ class RegExpPrototype final : public RegExpObject { public: explicit RegExpPrototype(GlobalObject&); + virtual void initialize(GlobalObject&) override; virtual ~RegExpPrototype() override; + +private: + JS_DECLARE_NATIVE_FUNCTION(to_string); }; } diff --git a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.toString.js b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.toString.js new file mode 100644 index 0000000000..5185fcc036 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.toString.js @@ -0,0 +1,5 @@ +test("basic functionality", () => { + expect(RegExp.prototype.toString).toHaveLength(0); + + expect(/test/g.toString()).toBe("/test/g"); +});