Pārlūkot izejas kodu

LibJS: Start implementing Intl Segments objects

Idan Horowitz 3 gadi atpakaļ
vecāks
revīzija
bbacea255f

+ 2 - 0
Userland/Libraries/LibJS/CMakeLists.txt

@@ -118,6 +118,8 @@ set(SOURCES
     Runtime/Intl/Segmenter.cpp
     Runtime/Intl/Segmenter.cpp
     Runtime/Intl/SegmenterConstructor.cpp
     Runtime/Intl/SegmenterConstructor.cpp
     Runtime/Intl/SegmenterPrototype.cpp
     Runtime/Intl/SegmenterPrototype.cpp
+    Runtime/Intl/Segments.cpp
+    Runtime/Intl/SegmentsPrototype.cpp
     Runtime/IteratorOperations.cpp
     Runtime/IteratorOperations.cpp
     Runtime/IteratorPrototype.cpp
     Runtime/IteratorPrototype.cpp
     Runtime/JSONObject.cpp
     Runtime/JSONObject.cpp

+ 4 - 0
Userland/Libraries/LibJS/Forward.h

@@ -218,6 +218,10 @@ namespace Intl {
     class PrototypeName;
     class PrototypeName;
 JS_ENUMERATE_INTL_OBJECTS
 JS_ENUMERATE_INTL_OBJECTS
 #undef __JS_ENUMERATE
 #undef __JS_ENUMERATE
+
+// Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
+class Segments;
+class SegmentsPrototype;
 };
 };
 
 
 namespace Temporal {
 namespace Temporal {

+ 4 - 0
Userland/Libraries/LibJS/Runtime/GlobalObject.cpp

@@ -69,6 +69,7 @@
 #include <LibJS/Runtime/Intl/RelativeTimeFormatPrototype.h>
 #include <LibJS/Runtime/Intl/RelativeTimeFormatPrototype.h>
 #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
 #include <LibJS/Runtime/Intl/SegmenterConstructor.h>
 #include <LibJS/Runtime/Intl/SegmenterPrototype.h>
 #include <LibJS/Runtime/Intl/SegmenterPrototype.h>
+#include <LibJS/Runtime/Intl/SegmentsPrototype.h>
 #include <LibJS/Runtime/IteratorPrototype.h>
 #include <LibJS/Runtime/IteratorPrototype.h>
 #include <LibJS/Runtime/JSONObject.h>
 #include <LibJS/Runtime/JSONObject.h>
 #include <LibJS/Runtime/MapConstructor.h>
 #include <LibJS/Runtime/MapConstructor.h>
@@ -181,6 +182,8 @@ void GlobalObject::initialize_global_object()
 
 
     m_async_from_sync_iterator_prototype = heap().allocate<AsyncFromSyncIteratorPrototype>(*this, *this);
     m_async_from_sync_iterator_prototype = heap().allocate<AsyncFromSyncIteratorPrototype>(*this, *this);
 
 
+    m_intl_segments_prototype = heap().allocate<Intl::SegmentsPrototype>(*this, *this);
+
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
 #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
     if (!m_##snake_name##_prototype)                                                     \
     if (!m_##snake_name##_prototype)                                                     \
         m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
         m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
@@ -323,6 +326,7 @@ void GlobalObject::visit_edges(Visitor& visitor)
     visitor.visit(m_proxy_constructor);
     visitor.visit(m_proxy_constructor);
     visitor.visit(m_generator_prototype);
     visitor.visit(m_generator_prototype);
     visitor.visit(m_async_from_sync_iterator_prototype);
     visitor.visit(m_async_from_sync_iterator_prototype);
+    visitor.visit(m_intl_segments_prototype);
     visitor.visit(m_array_prototype_values_function);
     visitor.visit(m_array_prototype_values_function);
     visitor.visit(m_date_constructor_now_function);
     visitor.visit(m_date_constructor_now_function);
     visitor.visit(m_eval_function);
     visitor.visit(m_eval_function);

+ 6 - 0
Userland/Libraries/LibJS/Runtime/GlobalObject.h

@@ -38,6 +38,9 @@ public:
     GeneratorPrototype* generator_prototype() { return m_generator_prototype; }
     GeneratorPrototype* generator_prototype() { return m_generator_prototype; }
     AsyncFromSyncIteratorPrototype* async_from_sync_iterator_prototype() { return m_async_from_sync_iterator_prototype; }
     AsyncFromSyncIteratorPrototype* async_from_sync_iterator_prototype() { return m_async_from_sync_iterator_prototype; }
 
 
+    // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
+    Intl::SegmentsPrototype* intl_segments_prototype() { return m_intl_segments_prototype; }
+
     FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; }
     FunctionObject* array_prototype_values_function() const { return m_array_prototype_values_function; }
     FunctionObject* date_constructor_now_function() const { return m_date_constructor_now_function; }
     FunctionObject* date_constructor_now_function() const { return m_date_constructor_now_function; }
     FunctionObject* eval_function() const { return m_eval_function; }
     FunctionObject* eval_function() const { return m_eval_function; }
@@ -106,6 +109,9 @@ private:
     GeneratorPrototype* m_generator_prototype { nullptr };
     GeneratorPrototype* m_generator_prototype { nullptr };
     AsyncFromSyncIteratorPrototype* m_async_from_sync_iterator_prototype { nullptr };
     AsyncFromSyncIteratorPrototype* m_async_from_sync_iterator_prototype { nullptr };
 
 
+    // Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor
+    Intl::SegmentsPrototype* m_intl_segments_prototype { nullptr };
+
     FunctionObject* m_array_prototype_values_function { nullptr };
     FunctionObject* m_array_prototype_values_function { nullptr };
     FunctionObject* m_date_constructor_now_function { nullptr };
     FunctionObject* m_date_constructor_now_function { nullptr };
     FunctionObject* m_eval_function { nullptr };
     FunctionObject* m_eval_function { nullptr };

+ 38 - 0
Userland/Libraries/LibJS/Runtime/Intl/Segments.cpp

@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Intl/Segments.h>
+#include <LibJS/Runtime/Intl/SegmentsPrototype.h>
+
+namespace JS::Intl {
+
+// 18.5.1 CreateSegmentsObject ( segmenter, string ), https://tc39.es/ecma402/#sec-createsegmentsobject
+Segments* Segments::create(GlobalObject& global_object, Segmenter& segmenter, String string)
+{
+    // 1. Let internalSlotsList be « [[SegmentsSegmenter]], [[SegmentsString]] ».
+    // 2. Let segments be ! OrdinaryObjectCreate(%SegmentsPrototype%, internalSlotsList).
+    // 3. Set segments.[[SegmentsSegmenter]] to segmenter.
+    // 4. Set segments.[[SegmentsString]] to string.
+    // 5. Return segments.
+    return global_object.heap().allocate<Segments>(global_object, global_object, segmenter, move(string));
+}
+
+// 18.5 Segments Objects, https://tc39.es/ecma402/#sec-segments-objects
+Segments::Segments(GlobalObject& global_object, Segmenter& segmenter, String string)
+    : Object(*global_object.intl_segments_prototype())
+    , m_segments_segmenter(segmenter)
+    , m_segments_string(move(string))
+{
+}
+
+void Segments::visit_edges(Cell::Visitor& visitor)
+{
+    Base::visit_edges(visitor);
+    visitor.visit(&m_segments_segmenter);
+}
+
+}

+ 35 - 0
Userland/Libraries/LibJS/Runtime/Intl/Segments.h

@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/String.h>
+#include <LibJS/Runtime/Intl/Segmenter.h>
+#include <LibJS/Runtime/Object.h>
+
+namespace JS::Intl {
+
+class Segments final : public Object {
+    JS_OBJECT(Segments, Object);
+
+public:
+    static Segments* create(GlobalObject&, Segmenter&, String);
+
+    Segments(GlobalObject&, Segmenter&, String);
+    virtual ~Segments() override = default;
+
+    Segmenter& segments_segmenter() const { return m_segments_segmenter; }
+
+    String const& segments_string() const { return m_segments_string; }
+
+private:
+    virtual void visit_edges(Cell::Visitor&) override;
+
+    Segmenter& m_segments_segmenter; // [[SegmentsSegmenter]]
+    String m_segments_string;        // [[SegmentsString]]
+};
+
+}

+ 24 - 0
Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.cpp

@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Intl/Segments.h>
+#include <LibJS/Runtime/Intl/SegmentsPrototype.h>
+
+namespace JS::Intl {
+
+// 18.5.2 The %SegmentsPrototype% Object, https://tc39.es/ecma402/#sec-%segmentsprototype%-object
+SegmentsPrototype::SegmentsPrototype(GlobalObject& global_object)
+    : PrototypeObject(*global_object.object_prototype())
+{
+}
+
+void SegmentsPrototype::initialize(GlobalObject& global_object)
+{
+    Object::initialize(global_object);
+}
+
+}

+ 23 - 0
Userland/Libraries/LibJS/Runtime/Intl/SegmentsPrototype.h

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibJS/Runtime/Intl/Segments.h>
+#include <LibJS/Runtime/PrototypeObject.h>
+
+namespace JS::Intl {
+
+class SegmentsPrototype final : public PrototypeObject<SegmentsPrototype, Segments> {
+    JS_PROTOTYPE_OBJECT(SegmentsPrototype, Segments, Segments);
+
+public:
+    explicit SegmentsPrototype(GlobalObject&);
+    virtual void initialize(GlobalObject&) override;
+    virtual ~SegmentsPrototype() override = default;
+};
+
+}