ladybird/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h
Linus Groh f9705eb2f4 LibJS: Replace GlobalObject with VM in Intl AOs [Part 1/19]
Instead of passing a GlobalObject everywhere, we will simply pass a VM,
from which we can get everything we need: common names, the current
realm, symbols, arguments, the heap, and a few other things.

In some places we already don't actually need a global object and just
do it for consistency - no more `auto& vm = global_object.vm();`!

This will eventually automatically fix the "wrong realm" issue we have
in some places where we (incorrectly) use the global object from the
allocating object, e.g. in call() / construct() implementations. When
only ever a VM is passed around, this issue can't happen :^)

I've decided to split this change into a series of patches that should
keep each commit down do a somewhat manageable size.
2022-08-23 13:58:30 +01:00

59 lines
1.8 KiB
C++

/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
#include <LibUnicode/Locale.h>
namespace JS::Intl {
class ListFormat final : public Object {
JS_OBJECT(ListFormat, Object);
public:
enum class Type {
Invalid,
Conjunction,
Disjunction,
Unit,
};
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;
Unicode::Style style() const { return m_style; }
void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
StringView style_string() const { return Unicode::style_to_string(m_style); }
private:
String m_locale; // [[Locale]]
Type m_type { Type::Invalid }; // [[Type]]
Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
};
using Placeables = HashMap<StringView, Variant<PatternPartition, Vector<PatternPartition>>>;
Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables);
Vector<PatternPartition> create_parts_from_list(ListFormat const&, Vector<String> const& list);
String format_list(ListFormat const&, Vector<String> const& list);
Array* format_list_to_parts(VM&, ListFormat const&, Vector<String> const& list);
ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
}