1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:37:45 +00:00

LibJS+LibWeb: Add JS::Object::inherits(class_name)

To allow implementing the DOM class hierarchy in JS bindings, this
patch adds an inherits() function that can be used to ask an Object
if it inherits from a specific C++ class (by name).

The necessary overrides are baked into each Object subclass by the
new JS_OBJECT macro, which works similarly to C_OBJECT in LibCore.

Thanks to @Dexesttp for suggesting this approach. :^)
This commit is contained in:
Andreas Kling 2020-06-21 15:14:02 +02:00
parent 1914f52371
commit af51dc105a
57 changed files with 122 additions and 106 deletions

View file

@ -32,6 +32,8 @@
namespace JS {
class Error : public Object {
JS_OBJECT(Error, Object);
public:
static Error* create(GlobalObject&, const FlyString& name, const String& message);
@ -45,7 +47,6 @@ public:
private:
virtual bool is_error() const final { return true; }
virtual const char* class_name() const override { return "Error"; }
FlyString m_name;
String m_message;
@ -53,14 +54,13 @@ private:
#define DECLARE_ERROR_SUBCLASS(ClassName, snake_name, PrototypeName, ConstructorName) \
class ClassName final : public Error { \
JS_OBJECT(ClassName, Error); \
\
public: \
static ClassName* create(GlobalObject&, const String& message); \
\
ClassName(const String& message, Object& prototype); \
virtual ~ClassName() override; \
\
private: \
virtual const char* class_name() const override; \
};
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \