mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:47:34 +00:00
LibJS: Use enumerator macros for boilerplate code around native types
This commit is contained in:
parent
0bdfb952c5
commit
cb0dfd8f72
11 changed files with 124 additions and 170 deletions
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue