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.
This commit is contained in:
Timothy Flynn 2021-09-01 21:56:55 -04:00 committed by Linus Groh
parent 940c023e09
commit 990dd037d2
Notes: sideshowbarker 2024-07-18 04:53:23 +09:00
3 changed files with 39 additions and 0 deletions

View file

@ -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());
}
}

View file

@ -17,6 +17,9 @@ public:
explicit LocalePrototype(GlobalObject&);
virtual void initialize(GlobalObject&) override;
virtual ~LocalePrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(to_string);
};
}

View file

@ -0,0 +1,3 @@
test("length is 0", () => {
expect(Intl.Locale.prototype.toString).toHaveLength(0);
});