From 44a8b55c5060e9b2eb0b188b1ec792068571d86d Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 8 Aug 2021 18:35:29 +0100 Subject: [PATCH] LibJS: Add preparation for Intl constructors and prototypes Add a JS_ENUMERATE_INTL_OBJECTS macro and use it to generate: - Forward declarations - CommonPropertyNames class name members - Constructor and prototype GlobalObject members, getters, visitors, and initialize_constructor() calls --- Userland/Libraries/LibJS/Forward.h | 11 +++++++++++ .../LibJS/Runtime/CommonPropertyNames.h | 3 +++ .../Libraries/LibJS/Runtime/GlobalObject.cpp | 18 ++++++++++++++++++ .../Libraries/LibJS/Runtime/GlobalObject.h | 12 ++++++++++++ 4 files changed, 44 insertions(+) diff --git a/Userland/Libraries/LibJS/Forward.h b/Userland/Libraries/LibJS/Forward.h index 7912e5562e..b16094535f 100644 --- a/Userland/Libraries/LibJS/Forward.h +++ b/Userland/Libraries/LibJS/Forward.h @@ -76,6 +76,8 @@ __JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \ __JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double) +#define JS_ENUMERATE_INTL_OBJECTS + #define JS_ENUMERATE_TEMPORAL_OBJECTS \ __JS_ENUMERATE(Calendar, calendar, CalendarPrototype, CalendarConstructor) \ __JS_ENUMERATE(Duration, duration, DurationPrototype, DurationConstructor) \ @@ -197,6 +199,15 @@ JS_ENUMERATE_NATIVE_ERRORS JS_ENUMERATE_TYPED_ARRAYS #undef __JS_ENUMERATE +namespace Intl { +#define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName) \ + class ClassName; \ + class ConstructorName; \ + class PrototypeName; +JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE +}; + namespace Temporal { #define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName) \ class ClassName; \ diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 47d3d1f80e..7d74ae6959 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -440,6 +440,9 @@ struct CommonPropertyNames { #define __JS_ENUMERATE(x, a, b, c, t) PropertyName x { #x, PropertyName::StringMayBeNumber::No }; JS_ENUMERATE_BUILTIN_TYPES #undef __JS_ENUMERATE +#define __JS_ENUMERATE(x, a, b, c) PropertyName x { #x, PropertyName::StringMayBeNumber::No }; + JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE #define __JS_ENUMERATE(x, a, b, c) PropertyName x { #x, PropertyName::StringMayBeNumber::No }; JS_ENUMERATE_TEMPORAL_OBJECTS #undef __JS_ENUMERATE diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index c6f0439f98..09890aab8c 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -154,6 +154,18 @@ void GlobalObject::initialize_global_object() JS_ENUMERATE_BUILTIN_TYPES #undef __JS_ENUMERATE +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + if (!m_intl_##snake_name##_prototype) \ + m_intl_##snake_name##_prototype = heap().allocate(*this, *this); + JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE + + // Must be allocated before `Intl::Intl` below. +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + initialize_constructor(vm.names.ClassName, m_intl_##snake_name##_constructor, m_intl_##snake_name##_prototype); + JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE + #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ if (!m_temporal_##snake_name##_prototype) \ m_temporal_##snake_name##_prototype = heap().allocate(*this, *this); @@ -276,6 +288,12 @@ void GlobalObject::visit_edges(Visitor& visitor) JS_ENUMERATE_BUILTIN_TYPES #undef __JS_ENUMERATE +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + visitor.visit(m_intl_##snake_name##_constructor); \ + visitor.visit(m_intl_##snake_name##_prototype); + JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE + #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ visitor.visit(m_temporal_##snake_name##_constructor); \ visitor.visit(m_temporal_##snake_name##_prototype); diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.h b/Userland/Libraries/LibJS/Runtime/GlobalObject.h index 9da096a31c..869e93c7b5 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.h @@ -47,6 +47,12 @@ public: JS_ENUMERATE_BUILTIN_TYPES #undef __JS_ENUMERATE +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + Intl::ConstructorName* intl_##snake_name##_constructor() { return m_intl_##snake_name##_constructor; } \ + Object* intl_##snake_name##_prototype() { return m_intl_##snake_name##_prototype; } + JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE + #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ Temporal::ConstructorName* temporal_##snake_name##_constructor() { return m_temporal_##snake_name##_constructor; } \ Object* temporal_##snake_name##_prototype() { return m_temporal_##snake_name##_prototype; } @@ -107,6 +113,12 @@ private: JS_ENUMERATE_BUILTIN_TYPES #undef __JS_ENUMERATE +#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ + Intl::ConstructorName* m_intl_##snake_name##_constructor { nullptr }; \ + Object* m_intl_##snake_name##_prototype { nullptr }; + JS_ENUMERATE_INTL_OBJECTS +#undef __JS_ENUMERATE + #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ Temporal::ConstructorName* m_temporal_##snake_name##_constructor { nullptr }; \ Object* m_temporal_##snake_name##_prototype { nullptr };