2021-09-05 23:05:16 -04:00
|
|
|
/*
|
2025-02-28 08:54:42 -05:00
|
|
|
* Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org>
|
2021-09-05 23:05:16 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-09-11 10:57:09 -04:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2021-09-05 23:05:16 -04:00
|
|
|
#include <LibJS/Runtime/Intl/ListFormat.h>
|
2023-07-19 06:54:48 -04:00
|
|
|
#include <LibJS/Runtime/Iterator.h>
|
2025-04-07 15:56:31 -04:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2024-06-23 09:14:27 -04:00
|
|
|
#include <LibUnicode/ListFormat.h>
|
2021-09-05 23:05:16 -04:00
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(ListFormat);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
// 14 ListFormat Objects, https://tc39.es/ecma402/#listformat-objects
|
2021-09-05 23:05:16 -04:00
|
|
|
ListFormat::ListFormat(Object& prototype)
|
2025-04-07 15:56:31 -04:00
|
|
|
: IntlObject(ConstructWithPrototypeTag::Tag, prototype)
|
2021-09-05 23:05:16 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-04-07 15:56:31 -04:00
|
|
|
// 14.2.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.ListFormat-internal-slots
|
|
|
|
ReadonlySpan<StringView> ListFormat::relevant_extension_keys() const
|
|
|
|
{
|
|
|
|
// The value of the [[RelevantExtensionKeys]] internal slot is « ».
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 14.2.3 Internal slots, https://tc39.es/ecma402/#sec-Intl.ListFormat-internal-slots
|
|
|
|
ReadonlySpan<ResolutionOptionDescriptor> ListFormat::resolution_option_descriptors(VM&) const
|
|
|
|
{
|
|
|
|
// The value of the [[ResolutionOptionDescriptors]] internal slot is « ».
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
// 14.5.2 CreatePartsFromList ( listFormat, list ), https://tc39.es/ecma402/#sec-createpartsfromlist
|
2025-07-23 14:42:57 -04:00
|
|
|
Vector<Unicode::ListFormat::Partition> create_parts_from_list(ListFormat const& list_format, ReadonlySpan<Utf16String> list)
|
2021-09-11 10:57:09 -04:00
|
|
|
{
|
2024-06-17 16:46:59 -04:00
|
|
|
return list_format.formatter().format_to_parts(list);
|
2021-09-11 10:57:09 -04:00
|
|
|
}
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
// 14.5.3 FormatList ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlist
|
2025-07-23 14:42:57 -04:00
|
|
|
Utf16String format_list(ListFormat const& list_format, ReadonlySpan<Utf16String> list)
|
2021-09-11 10:57:09 -04:00
|
|
|
{
|
2022-03-28 08:30:48 -04:00
|
|
|
// 1. Let parts be ! CreatePartsFromList(listFormat, list).
|
2024-08-15 15:55:33 -04:00
|
|
|
// 2. Let result be the empty String.
|
2021-09-11 10:57:09 -04:00
|
|
|
// 3. For each Record { [[Type]], [[Value]] } part in parts, do
|
2024-06-08 14:57:25 -04:00
|
|
|
// a. Set result to the string-concatenation of result and part.[[Value]].
|
2021-09-11 10:57:09 -04:00
|
|
|
// 4. Return result.
|
2024-06-17 16:46:59 -04:00
|
|
|
return list_format.formatter().format(list);
|
2021-09-11 10:57:09 -04:00
|
|
|
}
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
// 14.5.4 FormatListToParts ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlisttoparts
|
2025-07-23 14:42:57 -04:00
|
|
|
GC::Ref<Array> format_list_to_parts(VM& vm, ListFormat const& list_format, ReadonlySpan<Utf16String> list)
|
2021-09-11 10:57:09 -04:00
|
|
|
{
|
2022-08-20 08:25:24 +01:00
|
|
|
auto& realm = *vm.current_realm();
|
2021-09-11 10:57:09 -04:00
|
|
|
|
2022-03-28 08:30:48 -04:00
|
|
|
// 1. Let parts be ! CreatePartsFromList(listFormat, list).
|
2023-08-30 11:45:26 -04:00
|
|
|
auto parts = create_parts_from_list(list_format, list);
|
2021-09-11 10:57:09 -04:00
|
|
|
|
2022-03-28 08:30:48 -04:00
|
|
|
// 2. Let result be ! ArrayCreate(0).
|
2022-12-13 20:49:49 +00:00
|
|
|
auto result = MUST(Array::create(realm, 0));
|
2021-09-11 10:57:09 -04:00
|
|
|
|
|
|
|
// 3. Let n be 0.
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
// 4. For each Record { [[Type]], [[Value]] } part in parts, do
|
2021-11-10 12:48:07 -05:00
|
|
|
for (auto& part : parts) {
|
2022-05-02 20:54:39 +02:00
|
|
|
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
|
2022-12-13 20:49:50 +00:00
|
|
|
auto object = Object::create(realm, realm.intrinsics().object_prototype());
|
2021-09-11 10:57:09 -04:00
|
|
|
|
|
|
|
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
|
2023-08-08 18:25:57 +02:00
|
|
|
MUST(object->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, part.type)));
|
2021-09-11 10:57:09 -04:00
|
|
|
|
|
|
|
// c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
|
2022-12-06 22:17:27 +00:00
|
|
|
MUST(object->create_data_property_or_throw(vm.names.value, PrimitiveString::create(vm, move(part.value))));
|
2021-09-11 10:57:09 -04:00
|
|
|
|
|
|
|
// d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
|
2021-10-03 01:18:46 +01:00
|
|
|
MUST(result->create_data_property_or_throw(n, object));
|
2021-09-11 10:57:09 -04:00
|
|
|
|
|
|
|
// e. Increment n by 1.
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Return result.
|
2023-08-30 11:45:26 -04:00
|
|
|
return result;
|
2021-09-11 10:57:09 -04:00
|
|
|
}
|
|
|
|
|
2025-02-28 08:54:42 -05:00
|
|
|
// 14.5.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable
|
2025-07-23 14:42:57 -04:00
|
|
|
ThrowCompletionOr<Vector<Utf16String>> string_list_from_iterable(VM& vm, Value iterable)
|
2021-09-11 10:57:09 -04:00
|
|
|
{
|
|
|
|
// 1. If iterable is undefined, then
|
|
|
|
if (iterable.is_undefined()) {
|
|
|
|
// a. Return a new empty List.
|
2025-07-23 14:42:57 -04:00
|
|
|
return Vector<Utf16String> {};
|
2021-09-11 10:57:09 -04:00
|
|
|
}
|
|
|
|
|
2023-03-23 07:39:35 -04:00
|
|
|
// 2. Let iteratorRecord be ? GetIterator(iterable, sync).
|
|
|
|
auto iterator_record = TRY(get_iterator(vm, iterable, IteratorHint::Sync));
|
2021-09-11 10:57:09 -04:00
|
|
|
|
|
|
|
// 3. Let list be a new empty List.
|
2025-07-23 14:42:57 -04:00
|
|
|
Vector<Utf16String> list;
|
2021-09-11 10:57:09 -04:00
|
|
|
|
2024-02-01 14:59:32 -05:00
|
|
|
// 4. Repeat,
|
|
|
|
while (true) {
|
|
|
|
// a. Let next be ? IteratorStepValue(iteratorRecord).
|
|
|
|
auto next = TRY(iterator_step_value(vm, iterator_record));
|
|
|
|
|
|
|
|
// b. If next is DONE, then
|
|
|
|
if (!next.has_value()) {
|
|
|
|
// a. Return list.
|
|
|
|
return list;
|
|
|
|
}
|
2021-09-11 10:57:09 -04:00
|
|
|
|
2024-02-01 14:59:32 -05:00
|
|
|
// c. If Type(next) is not String, then
|
|
|
|
if (!next->is_string()) {
|
|
|
|
// 1. Let error be ThrowCompletion(a newly created TypeError object).
|
|
|
|
auto error = vm.throw_completion<TypeError>(ErrorType::NotAString, *next);
|
2021-09-11 10:57:09 -04:00
|
|
|
|
2024-02-01 14:59:32 -05:00
|
|
|
// 2. Return ? IteratorClose(iteratorRecord, error).
|
|
|
|
return iterator_close(vm, iterator_record, move(error));
|
2021-09-11 10:57:09 -04:00
|
|
|
}
|
|
|
|
|
2024-02-01 14:59:32 -05:00
|
|
|
// iii. Append next to list.
|
2025-07-23 14:42:57 -04:00
|
|
|
list.append(next->as_string().utf16_string());
|
2024-02-01 14:59:32 -05:00
|
|
|
}
|
2021-09-11 10:57:09 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 23:05:16 -04:00
|
|
|
}
|