diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index 4cd9a8fb45..fb50d719d0 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -154,7 +154,7 @@ public: template Value throw_exception(Args&&... args) { - return throw_exception(heap().allocate(forward(args)...)); + return throw_exception(T::create(global_object(), forward(args)...)); } Value throw_exception(Exception*); diff --git a/Libraries/LibJS/Runtime/Error.cpp b/Libraries/LibJS/Runtime/Error.cpp index 3b986da50c..9fdf490dda 100644 --- a/Libraries/LibJS/Runtime/Error.cpp +++ b/Libraries/LibJS/Runtime/Error.cpp @@ -26,27 +26,38 @@ #include #include +#include namespace JS { -Error::Error(const FlyString& name, const String& message) +Error* Error::create(GlobalObject& global_object, const FlyString& name, const String& message) +{ + auto& interpreter = global_object.interpreter(); + return interpreter.heap().allocate(name, message, *interpreter.error_prototype()); +} + +Error::Error(const FlyString& name, const String& message, Object& prototype) : m_name(name) , m_message(message) { - set_prototype(interpreter().error_prototype()); + set_prototype(&prototype); } Error::~Error() { } -#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ - ClassName::ClassName(const String& message) \ - : Error(#ClassName, message) \ - { \ - set_prototype(interpreter().snake_name##_prototype()); \ - } \ - ClassName::~ClassName() {} \ +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + ClassName* ClassName::create(GlobalObject& global_object, const String& message) \ + { \ + auto& interpreter = global_object.interpreter(); \ + return interpreter.heap().allocate(message, *interpreter.snake_name##_prototype()); \ + } \ + ClassName::ClassName(const String& message, Object& prototype) \ + : Error(#ClassName, message, prototype) \ + { \ + } \ + ClassName::~ClassName() {} \ const char* ClassName::class_name() const { return #ClassName; } JS_ENUMERATE_ERROR_SUBCLASSES diff --git a/Libraries/LibJS/Runtime/Error.h b/Libraries/LibJS/Runtime/Error.h index ac15e7bbbd..785541ac0a 100644 --- a/Libraries/LibJS/Runtime/Error.h +++ b/Libraries/LibJS/Runtime/Error.h @@ -33,7 +33,9 @@ namespace JS { class Error : public Object { public: - Error(const FlyString& name, const String& message); + static Error* create(GlobalObject&, const FlyString& name, const String& message); + + Error(const FlyString& name, const String& message, Object& prototype); virtual ~Error() override; const FlyString& name() const { return m_name; } @@ -52,7 +54,9 @@ private: #define DECLARE_ERROR_SUBCLASS(ClassName, snake_name, PrototypeName, ConstructorName) \ class ClassName final : public Error { \ public: \ - ClassName(const String& message); \ + static ClassName* create(GlobalObject&, const String& message); \ + \ + ClassName(const String& message, Object& prototype); \ virtual ~ClassName() override; \ \ private: \ diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp index 082d3d3944..3932911d8d 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.cpp +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -51,7 +51,7 @@ Value ErrorConstructor::construct(Interpreter& interpreter) String message = ""; if (!interpreter.call_frame().arguments.is_empty() && !interpreter.call_frame().arguments[0].is_undefined()) message = interpreter.call_frame().arguments[0].to_string(); - return interpreter.heap().allocate("Error", message); + return Error::create(interpreter.global_object(), "Error", message); } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ @@ -70,7 +70,7 @@ Value ErrorConstructor::construct(Interpreter& interpreter) String message = ""; \ if (!interpreter.call_frame().arguments.is_empty() && !interpreter.call_frame().arguments[0].is_undefined()) \ message = interpreter.call_frame().arguments[0].to_string(); \ - return interpreter.heap().allocate(message); \ + return ClassName::create(interpreter.global_object(), message); \ } JS_ENUMERATE_ERROR_SUBCLASSES diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Libraries/LibJS/Runtime/FunctionConstructor.cpp index fc93e3d13c..beb2ea842d 100644 --- a/Libraries/LibJS/Runtime/FunctionConstructor.cpp +++ b/Libraries/LibJS/Runtime/FunctionConstructor.cpp @@ -70,7 +70,7 @@ Value FunctionConstructor::construct(Interpreter& interpreter) auto function_expression = parser.parse_function_node(); if (parser.has_errors()) { // FIXME: The parser should expose parsing error strings rather than just fprintf()'ing them - return interpreter.heap().allocate("SyntaxError", ""); + return Error::create(interpreter.global_object(), "SyntaxError", ""); } return function_expression->execute(interpreter); }