From 8333055c3d9e0a05c058cbc4d00845711deb9593 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 1 Jan 2021 17:00:20 +0100 Subject: [PATCH] LibJS: Use RTTI for inheritance checks This replaces the hand-rolled string-based inheritance check tech. --- Applications/Spreadsheet/JSIntegration.cpp | 2 +- Libraries/LibJS/Runtime/Object.h | 11 ++++------- Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Applications/Spreadsheet/JSIntegration.cpp b/Applications/Spreadsheet/JSIntegration.cpp index a10d8a884d..12a249593e 100644 --- a/Applications/Spreadsheet/JSIntegration.cpp +++ b/Applications/Spreadsheet/JSIntegration.cpp @@ -419,7 +419,7 @@ JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet) if (!this_object) return {}; - if (!this_object->inherits("WorkbookObject")) { + if (!is(this_object)) { vm.throw_exception(global_object, JS::ErrorType::NotA, "WorkbookObject"); return {}; } diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 94b93c1211..50d756ab27 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -39,11 +39,10 @@ namespace JS { -#define JS_OBJECT(class_, base_class) \ -public: \ - using Base = base_class; \ - virtual const char* class_name() const override { return #class_; } \ - virtual bool inherits(const StringView& class_name) const override { return class_name == #class_ || Base::inherits(class_name); } +#define JS_OBJECT(class_, base_class) \ +public: \ + using Base = base_class; \ + virtual const char* class_name() const override { return #class_; } struct PropertyDescriptor { PropertyAttributes attributes; @@ -67,8 +66,6 @@ public: virtual void initialize(GlobalObject&) override; virtual ~Object(); - virtual bool inherits(const StringView& class_name) const { return class_name == this->class_name(); } - enum class PropertyKind { Key, Value, diff --git a/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 5e314a8674..90cf49ef19 100644 --- a/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -639,7 +639,7 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob auto* this_object = vm.this_value(global_object).to_object(global_object); if (!this_object) return {}; - if (!this_object->inherits("@wrapper_class@")) { + if (!is<@wrapper_class@>(this_object)) { vm.throw_exception(global_object, JS::ErrorType::NotA, "@fully_qualified_name@"); return nullptr; } @@ -694,7 +694,7 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob if (vm.exception()) @return_statement@ - if (!@cpp_name@_object->inherits("@parameter.type.name@Wrapper")) { + if (!is<@parameter.type.name@Wrapper>(@cpp_name@_object)) { vm.throw_exception(global_object, JS::ErrorType::NotA, "@parameter.type.name@"); @return_statement@ }