diff options
12 files changed, 235 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt index b697e29417..7901a39830 100644 --- a/Userland/Libraries/LibJS/CMakeLists.txt +++ b/Userland/Libraries/LibJS/CMakeLists.txt @@ -77,6 +77,9 @@ set(SOURCES Runtime/Intl/DisplayNamesConstructor.cpp Runtime/Intl/DisplayNamesPrototype.cpp Runtime/Intl/Intl.cpp + Runtime/Intl/Locale.cpp + Runtime/Intl/LocaleConstructor.cpp + Runtime/Intl/LocalePrototype.cpp Runtime/IteratorOperations.cpp Runtime/IteratorPrototype.cpp Runtime/JSONObject.cpp diff --git a/Userland/Libraries/LibJS/Forward.h b/Userland/Libraries/LibJS/Forward.h index 0d02c35536..a25a4eb32e 100644 --- a/Userland/Libraries/LibJS/Forward.h +++ b/Userland/Libraries/LibJS/Forward.h @@ -76,8 +76,9 @@ __JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \ __JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double) -#define JS_ENUMERATE_INTL_OBJECTS \ - __JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) +#define JS_ENUMERATE_INTL_OBJECTS \ + __JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \ + __JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor) #define JS_ENUMERATE_TEMPORAL_OBJECTS \ __JS_ENUMERATE(Calendar, calendar, CalendarPrototype, CalendarConstructor) \ diff --git a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp index cd453c1511..0eaf20a4f7 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -44,6 +44,8 @@ #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h> #include <LibJS/Runtime/Intl/DisplayNamesPrototype.h> #include <LibJS/Runtime/Intl/Intl.h> +#include <LibJS/Runtime/Intl/LocaleConstructor.h> +#include <LibJS/Runtime/Intl/LocalePrototype.h> #include <LibJS/Runtime/IteratorPrototype.h> #include <LibJS/Runtime/JSONObject.h> #include <LibJS/Runtime/MapConstructor.h> diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp index a540568b81..e4d2873baf 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/Intl.cpp @@ -9,6 +9,7 @@ #include <LibJS/Runtime/Intl/AbstractOperations.h> #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h> #include <LibJS/Runtime/Intl/Intl.h> +#include <LibJS/Runtime/Intl/LocaleConstructor.h> namespace JS::Intl { @@ -29,6 +30,7 @@ void Intl::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr); + define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr); define_native_function(vm.names.getCanonicalLocales, get_canonical_locales, 1, attr); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp new file mode 100644 index 0000000000..22151f7a98 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Intl/Locale.h> + +namespace JS::Intl { + +// 14 Locale Objects, https://tc39.es/ecma402/#locale-objects +Locale::Locale(Object& prototype) + : Object(prototype) +{ +} + +} diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h new file mode 100644 index 0000000000..eeab41b5ae --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Optional.h> +#include <AK/String.h> +#include <LibJS/Runtime/Object.h> +#include <LibJS/Runtime/Value.h> + +namespace JS::Intl { + +class Locale final : public Object { + JS_OBJECT(Locale, Object); + +public: + Locale(Object& prototype); + virtual ~Locale() override = default; + + String const& locale() const { return m_locale; } + void set_locale(String locale) { m_locale = move(locale); } + + bool has_calendar() const { return m_calendar.has_value(); } + String const& calendar() const { return m_calendar.value(); } + void set_calendar(String calendar) { m_calendar = move(calendar); } + + bool has_case_first() const { return m_case_first.has_value(); } + String const& case_first() const { return m_case_first.value(); } + void set_case_first(String case_first) { m_case_first = move(case_first); } + + bool has_collation() const { return m_collation.has_value(); } + String const& collation() const { return m_collation.value(); } + void set_collation(String collation) { m_collation = move(collation); } + + bool has_hour_cycle() const { return m_hour_cycle.has_value(); } + String const& hour_cycle() const { return m_hour_cycle.value(); } + void set_hour_cycle(String hour_cycle) { m_hour_cycle = move(hour_cycle); } + + bool has_numbering_system() const { return m_numbering_system.has_value(); } + String const& numbering_system() const { return m_numbering_system.value(); } + void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); } + + bool numeric() const { return m_numeric; } + void set_numeric(bool numeric) { m_numeric = numeric; } + +private: + String m_locale; // [[Locale]] + Optional<String> m_calendar; // [[Calendar]] + Optional<String> m_case_first; // [[CaseFirst]] + Optional<String> m_collation; // [[Collation]] + Optional<String> m_hour_cycle; // [[HourCycle]] + Optional<String> m_numbering_system; // [[NumberingSystem]] + bool m_numeric; // [[Numeric]] +}; + +} diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp new file mode 100644 index 0000000000..8db4311478 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Intl/Locale.h> +#include <LibJS/Runtime/Intl/LocaleConstructor.h> + +namespace JS::Intl { + +// 14.1 The Intl.Locale Constructor, https://tc39.es/ecma402/#sec-intl-locale-constructor +LocaleConstructor::LocaleConstructor(GlobalObject& global_object) + : NativeFunction(vm().names.Locale.as_string(), *global_object.function_prototype()) +{ +} + +void LocaleConstructor::initialize(GlobalObject& global_object) +{ + NativeFunction::initialize(global_object); + + auto& vm = this->vm(); + + // 14.2.1 Intl.Locale.prototype, https://tc39.es/ecma402/#sec-Intl.Locale.prototype + define_direct_property(vm.names.prototype, global_object.intl_locale_prototype(), 0); + define_direct_property(vm.names.length, Value(1), Attribute::Configurable); +} + +// 14.1.3 Intl.Locale ( tag [ , options ] ), https://tc39.es/ecma402/#sec-Intl.Locale +Value LocaleConstructor::call() +{ + // 1. If NewTarget is undefined, throw a TypeError exception. + vm().throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.Locale"); + return {}; +} + +// 14.1.3 Intl.Locale ( tag [ , options ] ), https://tc39.es/ecma402/#sec-Intl.Locale +Value LocaleConstructor::construct(FunctionObject& new_target) +{ + auto& vm = this->vm(); + auto& global_object = this->global_object(); + + // 6. Let locale be ? OrdinaryCreateFromConstructor(NewTarget, "%Locale.prototype%", internalSlotsList). + auto* locale = ordinary_create_from_constructor<Locale>(global_object, new_target, &GlobalObject::intl_locale_prototype); + if (vm.exception()) + return {}; + + return locale; +} + +} diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.h new file mode 100644 index 0000000000..f7e5d68c9e --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocaleConstructor.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibJS/Runtime/NativeFunction.h> + +namespace JS::Intl { + +class LocaleConstructor final : public NativeFunction { + JS_OBJECT(LocaleConstructor, NativeFunction); + +public: + explicit LocaleConstructor(GlobalObject&); + virtual void initialize(GlobalObject&) override; + virtual ~LocaleConstructor() override = default; + + virtual Value call() override; + virtual Value construct(FunctionObject& new_target) override; + +private: + virtual bool has_constructor() const override { return true; } +}; + +} diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp new file mode 100644 index 0000000000..b22c18b6c7 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/TypeCasts.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Intl/LocalePrototype.h> + +namespace JS::Intl { + +// 14.3 Properties of the Intl.Locale Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-locale-prototype-object +LocalePrototype::LocalePrototype(GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void LocalePrototype::initialize(GlobalObject& global_object) +{ + Object::initialize(global_object); + + auto& vm = this->vm(); + + // 14.3.2 Intl.Locale.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.Locale.prototype-@@tostringtag + define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.Locale"), Attribute::Configurable); +} + +} diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h new file mode 100644 index 0000000000..46b956ebe3 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibJS/Runtime/Object.h> + +namespace JS::Intl { + +class LocalePrototype final : public Object { + JS_OBJECT(LocalePrototype, Object); + +public: + explicit LocalePrototype(GlobalObject&); + virtual void initialize(GlobalObject&) override; + virtual ~LocalePrototype() override = default; +}; + +} diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.@@toStringTag.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.@@toStringTag.js new file mode 100644 index 0000000000..771a594af2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.@@toStringTag.js @@ -0,0 +1,3 @@ +test("basic functionality", () => { + expect(Intl.Locale.prototype[Symbol.toStringTag]).toBe("Intl.Locale"); +}); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.js new file mode 100644 index 0000000000..ceb74a117f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.js @@ -0,0 +1,13 @@ +describe("errors", () => { + test("called without new", () => { + expect(() => { + Intl.Locale(); + }).toThrowWithMessage(TypeError, "Intl.Locale constructor must be called with 'new'"); + }); +}); + +describe("normal behavior", () => { + test("length is 1", () => { + expect(Intl.Locale).toHaveLength(1); + }); +}); |