1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 12:17:35 +00:00

LibJS: Convert Intl.Locale.prototype to be a PrototypeObject

This commit is contained in:
Linus Groh 2021-09-13 18:07:28 +01:00
parent c277658ca6
commit b0c1179ff8
2 changed files with 22 additions and 37 deletions

View file

@ -12,25 +12,9 @@
namespace JS::Intl { 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::NotAnObjectOfType, "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 // 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) LocalePrototype::LocalePrototype(GlobalObject& global_object)
: Object(*global_object.object_prototype()) : PrototypeObject(*global_object.object_prototype())
{ {
} }
@ -65,7 +49,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::maximize)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -85,7 +69,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::minimize)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -105,7 +89,7 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::to_string)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -118,7 +102,7 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::base_name)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -142,15 +126,15 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::base_name)
// 14.3.9 get Intl.Locale.prototype.collation, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.collation // 14.3.9 get Intl.Locale.prototype.collation, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.collation
// 14.3.10 get Intl.Locale.prototype.hourCycle, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.hourCycle // 14.3.10 get Intl.Locale.prototype.hourCycle, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.hourCycle
// 14.3.12 get Intl.Locale.prototype.numberingSystem, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.numberingSystem // 14.3.12 get Intl.Locale.prototype.numberingSystem, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.numberingSystem
#define __JS_ENUMERATE(keyword) \ #define __JS_ENUMERATE(keyword) \
JS_DEFINE_NATIVE_GETTER(LocalePrototype::keyword) \ JS_DEFINE_NATIVE_GETTER(LocalePrototype::keyword) \
{ \ { \
auto* locale_object = typed_this(global_object); \ auto* locale_object = typed_this_object(global_object); \
if (!locale_object) \ if (!locale_object) \
return {}; \ return {}; \
if (!locale_object->has_##keyword()) \ if (!locale_object->has_##keyword()) \
return js_undefined(); \ return js_undefined(); \
return js_string(vm, locale_object->keyword()); \ return js_string(vm, locale_object->keyword()); \
} }
JS_ENUMERATE_LOCALE_KEYWORD_PROPERTIES JS_ENUMERATE_LOCALE_KEYWORD_PROPERTIES
#undef __JS_ENUMERATE #undef __JS_ENUMERATE
@ -160,7 +144,7 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::numeric)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -173,7 +157,7 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::language)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -192,7 +176,7 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::script)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};
@ -215,7 +199,7 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::region)
{ {
// 1. Let loc be the this value. // 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object); auto* locale_object = typed_this_object(global_object);
if (!locale_object) if (!locale_object)
return {}; return {};

View file

@ -6,12 +6,13 @@
#pragma once #pragma once
#include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/Intl/Locale.h>
#include <LibJS/Runtime/PrototypeObject.h>
namespace JS::Intl { namespace JS::Intl {
class LocalePrototype final : public Object { class LocalePrototype final : public PrototypeObject<LocalePrototype, Locale> {
JS_OBJECT(LocalePrototype, Object); JS_PROTOTYPE_OBJECT(LocalePrototype, Locale, Intl.Locale);
public: public:
explicit LocalePrototype(GlobalObject&); explicit LocalePrototype(GlobalObject&);