diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-17 19:31:48 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-18 10:28:22 +0200 |
commit | 205ac0090da21c593ac7cef7db348412e4f52168 (patch) | |
tree | 9b1d0532af723658f66dc61a99b738a3156c3816 | |
parent | 0df4d2823a0721668dfefbff68a49070c1a1bf0f (diff) | |
download | serenity-205ac0090da21c593ac7cef7db348412e4f52168.zip |
LibJS: Pass prototype to Error constructors
-rw-r--r-- | Libraries/LibJS/Interpreter.h | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Error.cpp | 29 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Error.h | 8 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ErrorConstructor.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/FunctionConstructor.cpp | 2 |
5 files changed, 30 insertions, 15 deletions
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<typename T, typename... Args> Value throw_exception(Args&&... args) { - return throw_exception(heap().allocate<T>(forward<Args>(args)...)); + return throw_exception(T::create(global_object(), forward<Args>(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 <LibJS/Interpreter.h> #include <LibJS/Runtime/Error.h> +#include <LibJS/Runtime/GlobalObject.h> 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<Error>(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<ClassName>(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>("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<ClassName>(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<FunctionExpression>(); if (parser.has_errors()) { // FIXME: The parser should expose parsing error strings rather than just fprintf()'ing them - return interpreter.heap().allocate<Error>("SyntaxError", ""); + return Error::create(interpreter.global_object(), "SyntaxError", ""); } return function_expression->execute(interpreter); } |