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

LibJS: Implement a nearly empty Intl.Locale object

This adds plumbing for the Intl.Locale object, constructor, and
prototype.
This commit is contained in:
Timothy Flynn 2021-09-02 08:32:43 -04:00 committed by Linus Groh
parent e7d3483618
commit 2c10e9fdd3
12 changed files with 235 additions and 2 deletions

View file

@ -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

View file

@ -77,7 +77,8 @@
__JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double)
#define JS_ENUMERATE_INTL_OBJECTS \
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor)
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
__JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor)
#define JS_ENUMERATE_TEMPORAL_OBJECTS \
__JS_ENUMERATE(Calendar, calendar, CalendarPrototype, CalendarConstructor) \

View file

@ -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>

View file

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

View file

@ -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)
{
}
}

View file

@ -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]]
};
}

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,3 @@
test("basic functionality", () => {
expect(Intl.Locale.prototype[Symbol.toStringTag]).toBe("Intl.Locale");
});

View file

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