From 990dd037d2ba029332619641ed43d4896b4a679d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 1 Sep 2021 21:56:55 -0400 Subject: LibJS: Implement Intl.Locale.prototype.toString() This isn't particularly testable yet without the Intl.Locale constructor but having this defined will make testing the constructor possible. So more specific tests for this prototype will come later. --- .../LibJS/Runtime/Intl/LocalePrototype.cpp | 33 ++++++++++++++++++++++ .../Libraries/LibJS/Runtime/Intl/LocalePrototype.h | 3 ++ .../Intl/Locale/Locale.prototype.toString.js | 3 ++ 3 files changed, 39 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.toString.js (limited to 'Userland') diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index b22c18b6c7..03344f21ec 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -6,10 +6,27 @@ #include #include +#include #include namespace JS::Intl { +static Locale* typed_this(GlobalObject& global_object) +{ + auto& vm = global_object.vm(); + + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return nullptr; + + if (!is(this_object)) { + vm.throw_exception(global_object, ErrorType::NotA, "Intl.Locale"); + return nullptr; + } + + return static_cast(this_object); +} + // 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()) @@ -22,8 +39,24 @@ void LocalePrototype::initialize(GlobalObject& global_object) auto& vm = this->vm(); + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.toString, to_string, 0, attr); + // 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); } +// 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString +JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::to_string) +{ + // 1. Let loc be the this value. + // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). + auto* locale_object = typed_this(global_object); + if (!locale_object) + return {}; + + // 3. Return loc.[[Locale]]. + return js_string(vm, locale_object->locale()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index 46b956ebe3..21de9dabd7 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -17,6 +17,9 @@ public: explicit LocalePrototype(GlobalObject&); virtual void initialize(GlobalObject&) override; virtual ~LocalePrototype() override = default; + +private: + JS_DECLARE_NATIVE_FUNCTION(to_string); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.toString.js new file mode 100644 index 0000000000..caeac0c9ac --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.toString.js @@ -0,0 +1,3 @@ +test("length is 0", () => { + expect(Intl.Locale.prototype.toString).toHaveLength(0); +}); -- cgit v1.2.3