mirror of
https://github.com/RGBCube/serenity
synced 2025-07-19 11:47:39 +00:00
LibJS: Implement a nearly empty Intl.ListFormat object
This adds plumbing for the Intl.ListFormat object, constructor, and prototype.
This commit is contained in:
parent
4ad2159812
commit
8e75e5fabb
12 changed files with 279 additions and 0 deletions
|
@ -77,6 +77,9 @@ set(SOURCES
|
||||||
Runtime/Intl/DisplayNamesConstructor.cpp
|
Runtime/Intl/DisplayNamesConstructor.cpp
|
||||||
Runtime/Intl/DisplayNamesPrototype.cpp
|
Runtime/Intl/DisplayNamesPrototype.cpp
|
||||||
Runtime/Intl/Intl.cpp
|
Runtime/Intl/Intl.cpp
|
||||||
|
Runtime/Intl/ListFormat.cpp
|
||||||
|
Runtime/Intl/ListFormatConstructor.cpp
|
||||||
|
Runtime/Intl/ListFormatPrototype.cpp
|
||||||
Runtime/Intl/Locale.cpp
|
Runtime/Intl/Locale.cpp
|
||||||
Runtime/Intl/LocaleConstructor.cpp
|
Runtime/Intl/LocaleConstructor.cpp
|
||||||
Runtime/Intl/LocalePrototype.cpp
|
Runtime/Intl/LocalePrototype.cpp
|
||||||
|
|
|
@ -78,6 +78,7 @@
|
||||||
|
|
||||||
#define JS_ENUMERATE_INTL_OBJECTS \
|
#define JS_ENUMERATE_INTL_OBJECTS \
|
||||||
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
|
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
|
||||||
|
__JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor) \
|
||||||
__JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor)
|
__JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor)
|
||||||
|
|
||||||
#define JS_ENUMERATE_TEMPORAL_OBJECTS \
|
#define JS_ENUMERATE_TEMPORAL_OBJECTS \
|
||||||
|
|
|
@ -44,6 +44,8 @@
|
||||||
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
||||||
#include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
|
#include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
|
||||||
#include <LibJS/Runtime/Intl/Intl.h>
|
#include <LibJS/Runtime/Intl/Intl.h>
|
||||||
|
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
|
||||||
|
#include <LibJS/Runtime/Intl/ListFormatPrototype.h>
|
||||||
#include <LibJS/Runtime/Intl/LocaleConstructor.h>
|
#include <LibJS/Runtime/Intl/LocaleConstructor.h>
|
||||||
#include <LibJS/Runtime/Intl/LocalePrototype.h>
|
#include <LibJS/Runtime/Intl/LocalePrototype.h>
|
||||||
#include <LibJS/Runtime/IteratorPrototype.h>
|
#include <LibJS/Runtime/IteratorPrototype.h>
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
||||||
#include <LibJS/Runtime/Intl/Intl.h>
|
#include <LibJS/Runtime/Intl/Intl.h>
|
||||||
|
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
|
||||||
#include <LibJS/Runtime/Intl/LocaleConstructor.h>
|
#include <LibJS/Runtime/Intl/LocaleConstructor.h>
|
||||||
|
|
||||||
namespace JS::Intl {
|
namespace JS::Intl {
|
||||||
|
@ -30,6 +31,7 @@ void Intl::initialize(GlobalObject& global_object)
|
||||||
|
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
|
define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
|
||||||
|
define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
|
||||||
define_direct_property(vm.names.Locale, global_object.intl_locale_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);
|
define_native_function(vm.names.getCanonicalLocales, get_canonical_locales, 1, attr);
|
||||||
|
|
71
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp
Normal file
71
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.cpp
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Intl/ListFormat.h>
|
||||||
|
|
||||||
|
namespace JS::Intl {
|
||||||
|
|
||||||
|
// 13 ListFomat Objects, https://tc39.es/ecma402/#listformat-objects
|
||||||
|
ListFormat::ListFormat(Object& prototype)
|
||||||
|
: Object(prototype)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListFormat::set_type(StringView type)
|
||||||
|
{
|
||||||
|
if (type == "conjunction"sv) {
|
||||||
|
m_type = Type::Conjunction;
|
||||||
|
} else if (type == "disjunction"sv) {
|
||||||
|
m_type = Type::Disjunction;
|
||||||
|
} else if (type == "unit"sv) {
|
||||||
|
m_type = Type::Unit;
|
||||||
|
} else {
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StringView ListFormat::type_string() const
|
||||||
|
{
|
||||||
|
switch (m_type) {
|
||||||
|
case Type::Conjunction:
|
||||||
|
return "conjunction"sv;
|
||||||
|
case Type::Disjunction:
|
||||||
|
return "disjunction"sv;
|
||||||
|
case Type::Unit:
|
||||||
|
return "unit"sv;
|
||||||
|
default:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListFormat::set_style(StringView style)
|
||||||
|
{
|
||||||
|
if (style == "narrow"sv) {
|
||||||
|
m_style = Style::Narrow;
|
||||||
|
} else if (style == "short"sv) {
|
||||||
|
m_style = Style::Short;
|
||||||
|
} else if (style == "long"sv) {
|
||||||
|
m_style = Style::Long;
|
||||||
|
} else {
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StringView ListFormat::style_string() const
|
||||||
|
{
|
||||||
|
switch (m_style) {
|
||||||
|
case Style::Narrow:
|
||||||
|
return "narrow"sv;
|
||||||
|
case Style::Short:
|
||||||
|
return "short"sv;
|
||||||
|
case Style::Long:
|
||||||
|
return "long"sv;
|
||||||
|
default:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h
Normal file
53
Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/StringView.h>
|
||||||
|
#include <LibJS/Runtime/Object.h>
|
||||||
|
|
||||||
|
namespace JS::Intl {
|
||||||
|
|
||||||
|
class ListFormat final : public Object {
|
||||||
|
JS_OBJECT(ListFormat, Object);
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum class Type {
|
||||||
|
Invalid,
|
||||||
|
Conjunction,
|
||||||
|
Disjunction,
|
||||||
|
Unit,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Style {
|
||||||
|
Invalid,
|
||||||
|
Narrow,
|
||||||
|
Short,
|
||||||
|
Long,
|
||||||
|
};
|
||||||
|
|
||||||
|
ListFormat(Object& prototype);
|
||||||
|
virtual ~ListFormat() override = default;
|
||||||
|
|
||||||
|
String const& locale() const { return m_locale; }
|
||||||
|
void set_locale(String locale) { m_locale = move(locale); }
|
||||||
|
|
||||||
|
Type type() const { return m_type; }
|
||||||
|
void set_type(StringView type);
|
||||||
|
StringView type_string() const;
|
||||||
|
|
||||||
|
Style style() const { return m_style; }
|
||||||
|
void set_style(StringView style);
|
||||||
|
StringView style_string() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
String m_locale; // [[Locale]]
|
||||||
|
Type m_type { Type::Invalid }; // [[Type]]
|
||||||
|
Style m_style { Style::Invalid }; // [[Style]]
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -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/ListFormat.h>
|
||||||
|
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
|
||||||
|
|
||||||
|
namespace JS::Intl {
|
||||||
|
|
||||||
|
// 13.2 The Intl.ListFormat Constructor, https://tc39.es/ecma402/#sec-intl-listformat-constructor
|
||||||
|
ListFormatConstructor::ListFormatConstructor(GlobalObject& global_object)
|
||||||
|
: NativeFunction(vm().names.ListFormat.as_string(), *global_object.function_prototype())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListFormatConstructor::initialize(GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
NativeFunction::initialize(global_object);
|
||||||
|
|
||||||
|
auto& vm = this->vm();
|
||||||
|
|
||||||
|
// 13.3.1 Intl.ListFormat.prototype, https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype
|
||||||
|
define_direct_property(vm.names.prototype, global_object.intl_list_format_prototype(), 0);
|
||||||
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 13.2.1 Intl.ListFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat
|
||||||
|
Value ListFormatConstructor::call()
|
||||||
|
{
|
||||||
|
// 1. If NewTarget is undefined, throw a TypeError exception.
|
||||||
|
vm().throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.ListFormat");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 13.2.1 Intl.ListFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat
|
||||||
|
Value ListFormatConstructor::construct(FunctionObject& new_target)
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
auto& global_object = this->global_object();
|
||||||
|
|
||||||
|
// 2. Let listFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%ListFormat.prototype%", « [[InitializedListFormat]], [[Locale]], [[Type]], [[Style]], [[Templates]] »).
|
||||||
|
auto* list_format = ordinary_create_from_constructor<ListFormat>(global_object, new_target, &GlobalObject::intl_list_format_prototype);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return list_format;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 ListFormatConstructor final : public NativeFunction {
|
||||||
|
JS_OBJECT(ListFormatConstructor, NativeFunction);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ListFormatConstructor(GlobalObject&);
|
||||||
|
virtual void initialize(GlobalObject&) override;
|
||||||
|
virtual ~ListFormatConstructor() override = default;
|
||||||
|
|
||||||
|
virtual Value call() override;
|
||||||
|
virtual Value construct(FunctionObject& new_target) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Intl/ListFormatPrototype.h>
|
||||||
|
|
||||||
|
namespace JS::Intl {
|
||||||
|
|
||||||
|
// 13.4 Properties of the Intl.ListFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-listformat-prototype-object
|
||||||
|
ListFormatPrototype::ListFormatPrototype(GlobalObject& global_object)
|
||||||
|
: Object(*global_object.object_prototype())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListFormatPrototype::initialize(GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
Object::initialize(global_object);
|
||||||
|
|
||||||
|
auto& vm = this->vm();
|
||||||
|
|
||||||
|
// 13.4.2 Intl.ListFormat.prototype [ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype-toStringTag
|
||||||
|
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.ListFormat"), Attribute::Configurable);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
22
Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h
Normal file
22
Userland/Libraries/LibJS/Runtime/Intl/ListFormatPrototype.h
Normal 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 ListFormatPrototype final : public Object {
|
||||||
|
JS_OBJECT(ListFormatPrototype, Object);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ListFormatPrototype(GlobalObject&);
|
||||||
|
virtual void initialize(GlobalObject&) override;
|
||||||
|
virtual ~ListFormatPrototype() override = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(Intl.ListFormat.prototype[Symbol.toStringTag]).toBe("Intl.ListFormat");
|
||||||
|
});
|
|
@ -0,0 +1,13 @@
|
||||||
|
describe("errors", () => {
|
||||||
|
test("called without new", () => {
|
||||||
|
expect(() => {
|
||||||
|
Intl.ListFormat();
|
||||||
|
}).toThrowWithMessage(TypeError, "Intl.ListFormat constructor must be called with 'new'");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("normal behavior", () => {
|
||||||
|
test("length is 0", () => {
|
||||||
|
expect(Intl.ListFormat).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue