diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-10 14:06:52 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-10 14:06:52 +0200 |
commit | cb0dfd8f724d466c5145d52433f8953898e22eaf (patch) | |
tree | 7fecc56f46b51f9e587f37e36656682b982be75c /Libraries/LibJS/Runtime | |
parent | 0bdfb952c5d6ebe4499fe70259263db453101ef8 (diff) | |
download | serenity-cb0dfd8f724d466c5145d52433f8953898e22eaf.zip |
LibJS: Use enumerator macros for boilerplate code around native types
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r-- | Libraries/LibJS/Runtime/Error.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Error.h | 22 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ErrorConstructor.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ErrorConstructor.h | 28 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ErrorPrototype.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ErrorPrototype.h | 22 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/GlobalObject.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/GlobalObject.h | 32 |
8 files changed, 72 insertions, 92 deletions
diff --git a/Libraries/LibJS/Runtime/Error.cpp b/Libraries/LibJS/Runtime/Error.cpp index 417f3735f8..3b986da50c 100644 --- a/Libraries/LibJS/Runtime/Error.cpp +++ b/Libraries/LibJS/Runtime/Error.cpp @@ -40,16 +40,16 @@ Error::~Error() { } -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - TitleCase::TitleCase(const String& message) \ - : Error(#TitleCase, message) \ - { \ - set_prototype(interpreter().snake_case##_prototype()); \ - } \ - TitleCase::~TitleCase() {} \ - const char* TitleCase::class_name() const { return #TitleCase; } +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + ClassName::ClassName(const String& message) \ + : Error(#ClassName, message) \ + { \ + set_prototype(interpreter().snake_name##_prototype()); \ + } \ + ClassName::~ClassName() {} \ + const char* ClassName::class_name() const { return #ClassName; } JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/Error.h b/Libraries/LibJS/Runtime/Error.h index fc42332baf..76615b4b9e 100644 --- a/Libraries/LibJS/Runtime/Error.h +++ b/Libraries/LibJS/Runtime/Error.h @@ -47,18 +47,18 @@ private: String m_message; }; -#define DECLARE_ERROR_SUBCLASS(TitleCase, snake_case) \ - class TitleCase final : public Error { \ - public: \ - TitleCase(const String& message); \ - virtual ~TitleCase() override; \ - \ - private: \ - virtual const char* class_name() const override; \ +#define DECLARE_ERROR_SUBCLASS(ClassName, snake_name, PrototypeName, ConstructorName) \ + class ClassName final : public Error { \ + public: \ + ClassName(const String& message); \ + virtual ~ClassName() override; \ + \ + private: \ + virtual const char* class_name() const override; \ }; -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - DECLARE_ERROR_SUBCLASS(TitleCase, snake_case) +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + DECLARE_ERROR_SUBCLASS(ClassName, snake_name, PrototypeName, ConstructorName) JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.cpp b/Libraries/LibJS/Runtime/ErrorConstructor.cpp index a5a2a625a8..b62018fe02 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.cpp +++ b/Libraries/LibJS/Runtime/ErrorConstructor.cpp @@ -53,28 +53,26 @@ Value ErrorConstructor::construct(Interpreter& interpreter) return interpreter.heap().allocate<Error>("Error", message); } -#define DEFINE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case) \ - TitleCase##Constructor::TitleCase##Constructor() \ +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + ConstructorName::ConstructorName() \ { \ - put("prototype", interpreter().snake_case##_prototype()); \ + put("prototype", interpreter().snake_name##_prototype()); \ put("length", Value(1)); \ } \ - TitleCase##Constructor::~TitleCase##Constructor() {} \ - Value TitleCase##Constructor::call(Interpreter& interpreter) \ + ConstructorName::~ConstructorName() {} \ + Value ConstructorName::call(Interpreter& interpreter) \ { \ return construct(interpreter); \ } \ - Value TitleCase##Constructor::construct(Interpreter& interpreter) \ + Value ConstructorName::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<TitleCase>(message); \ + return interpreter.heap().allocate<ClassName>(message); \ } -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - DEFINE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case) JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/ErrorConstructor.h b/Libraries/LibJS/Runtime/ErrorConstructor.h index 39c0cdf075..2e6c4ac337 100644 --- a/Libraries/LibJS/Runtime/ErrorConstructor.h +++ b/Libraries/LibJS/Runtime/ErrorConstructor.h @@ -44,22 +44,22 @@ private: virtual const char* class_name() const override { return "ErrorConstructor"; } }; -#define DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case) \ - class TitleCase##Constructor final : public NativeFunction { \ - public: \ - TitleCase##Constructor(); \ - virtual ~TitleCase##Constructor() override; \ - virtual Value call(Interpreter&) override; \ - virtual Value construct(Interpreter&) override; \ - \ - private: \ - virtual bool has_constructor() const override { return true; } \ - virtual const char* class_name() const override { return #TitleCase "Constructor"; } \ +#define DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName) \ + class ConstructorName final : public NativeFunction { \ + public: \ + ConstructorName(); \ + virtual ~ConstructorName() override; \ + virtual Value call(Interpreter&) override; \ + virtual Value construct(Interpreter&) override; \ + \ + private: \ + virtual bool has_constructor() const override { return true; } \ + virtual const char* class_name() const override { return #ClassName "Constructor"; } \ }; -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(TitleCase, snake_case) +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + DECLARE_ERROR_SUBCLASS_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName) JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.cpp b/Libraries/LibJS/Runtime/ErrorPrototype.cpp index f35d25c4a3..1e193a567b 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ErrorPrototype.cpp @@ -88,17 +88,15 @@ Value ErrorPrototype::to_string(Interpreter& interpreter) return js_string(interpreter, String::format("%s: %s", name.characters(), message.characters())); } -#define DEFINE_ERROR_SUBCLASS_PROTOTYPE(TitleCase, snake_case) \ - TitleCase::TitleCase() \ - { \ - set_prototype(interpreter().error_prototype()); \ - } \ - TitleCase::~TitleCase() {} \ - const char* TitleCase::class_name() const { return #TitleCase; } +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + PrototypeName::PrototypeName() \ + { \ + set_prototype(interpreter().error_prototype()); \ + } \ + PrototypeName::~PrototypeName() {} \ + const char* PrototypeName::class_name() const { return #PrototypeName; } -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - DEFINE_ERROR_SUBCLASS_PROTOTYPE(TitleCase##Prototype, snake_case##_prototype) JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.h b/Libraries/LibJS/Runtime/ErrorPrototype.h index c8ca9f7e85..ffc13a8345 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.h +++ b/Libraries/LibJS/Runtime/ErrorPrototype.h @@ -44,19 +44,19 @@ private: static Value message_getter(Interpreter&); }; -#define DECLARE_ERROR_SUBCLASS_PROTOTYPE(TitleCase, snake_case) \ - class TitleCase final : public Object { \ - public: \ - TitleCase(); \ - virtual ~TitleCase() override; \ - \ - private: \ - virtual const char* class_name() const override; \ +#define DECLARE_ERROR_SUBCLASS_PROTOTYPE(ClassName, snake_name, PrototypeName, ConstructorName) \ + class PrototypeName final : public Object { \ + public: \ + PrototypeName(); \ + virtual ~PrototypeName() override; \ + \ + private: \ + virtual const char* class_name() const override; \ }; -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - DECLARE_ERROR_SUBCLASS_PROTOTYPE(TitleCase##Prototype, snake_case##_prototype) +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + DECLARE_ERROR_SUBCLASS_PROTOTYPE(ClassName, snake_name, PrototypeName, ConstructorName) JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index fdb97aa982..b7eb4f3d05 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -73,10 +73,10 @@ GlobalObject::GlobalObject() add_constructor("Number", m_number_constructor, *interpreter().number_prototype()); add_constructor("Object", m_object_constructor, *interpreter().object_prototype()); -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - add_constructor(#TitleCase, m_##snake_case##_constructor, *interpreter().snake_case##_prototype()); +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + add_constructor(#ClassName, m_##snake_name##_constructor, *interpreter().snake_name##_prototype()); JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#undef __JS_ENUMERATE } GlobalObject::~GlobalObject() diff --git a/Libraries/LibJS/Runtime/GlobalObject.h b/Libraries/LibJS/Runtime/GlobalObject.h index 101403003a..14f7bcf530 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Libraries/LibJS/Runtime/GlobalObject.h @@ -35,18 +35,10 @@ public: explicit GlobalObject(); virtual ~GlobalObject() override; - ArrayConstructor* array_constructor() { return m_array_constructor; } - BooleanConstructor* boolean_constructor() { return m_boolean_constructor; } - DateConstructor* date_constructor() { return m_date_constructor; } - FunctionConstructor* function_constructor() { return m_function_constructor; } - NumberConstructor* number_constructor() { return m_number_constructor; }; - ObjectConstructor* object_constructor() { return m_object_constructor; } - ErrorConstructor* error_constructor() { return m_error_constructor; } - -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - TitleCase##Constructor* snake_case##_constructor() { return m_##snake_case##_constructor; } - JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + ConstructorName* snake_name##_constructor() { return m_##snake_name##_constructor; } + JS_ENUMERATE_BUILTIN_TYPES +#undef __JS_ENUMERATE protected: virtual void visit_children(Visitor&) override; @@ -60,18 +52,10 @@ private: template<typename ConstructorType> void add_constructor(const FlyString& property_name, ConstructorType*&, Object& prototype); - ArrayConstructor* m_array_constructor { nullptr }; - BooleanConstructor* m_boolean_constructor { nullptr }; - DateConstructor* m_date_constructor { nullptr }; - FunctionConstructor* m_function_constructor { nullptr }; - NumberConstructor* m_number_constructor { nullptr }; - ObjectConstructor* m_object_constructor { nullptr }; - ErrorConstructor* m_error_constructor { nullptr }; - -#define __JS_ENUMERATE_ERROR_SUBCLASS(TitleCase, snake_case) \ - TitleCase##Constructor* m_##snake_case##_constructor; - JS_ENUMERATE_ERROR_SUBCLASSES -#undef __JS_ENUMERATE_ERROR_SUBCLASS +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + ConstructorName* m_##snake_name##_constructor { nullptr }; + JS_ENUMERATE_BUILTIN_TYPES +#undef __JS_ENUMERATE }; } |