diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-01 21:56:55 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-02 17:56:42 +0100 |
commit | 990dd037d2ba029332619641ed43d4896b4a679d (patch) | |
tree | aba95cb2dd198996a8361fa9bf39ff2f8b59e264 /Userland | |
parent | 940c023e09f4750615b97d1eaef7b2b6c997438f (diff) | |
download | serenity-990dd037d2ba029332619641ed43d4896b4a679d.zip |
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.
Diffstat (limited to 'Userland')
3 files changed, 39 insertions, 0 deletions
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 <AK/TypeCasts.h> #include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Intl/Locale.h> #include <LibJS/Runtime/Intl/LocalePrototype.h> 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<Locale>(this_object)) { + vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Intl.Locale"); + return nullptr; + } + + return static_cast<Locale*>(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); +}); |