ladybird/Userland/Libraries/LibJS/Runtime/Intl/ListFormat.h
Andreas Kling 3c74dc9f4d LibJS: Segregate GC-allocated objects by type
This patch adds two macros to declare per-type allocators:

- JS_DECLARE_ALLOCATOR(TypeName)
- JS_DEFINE_ALLOCATOR(TypeName)

When used, they add a type-specific CellAllocator that the Heap will
delegate allocation requests to.

The result of this is that GC objects of the same type always end up
within the same HeapBlock, drastically reducing the ability to perform
type confusion attacks.

It also improves HeapBlock utilization, since each block now has cells
sized exactly to the type used within that block. (Previously we only
had a handful of block sizes available, and most GC allocations ended
up with a large amount of slack in their tails.)

There is a small performance hit from this, but I'm sure we can make
up for it elsewhere.

Note that the old size-based allocators still exist, and we fall back
to them for any type that doesn't have its own CellAllocator.
2023-11-19 12:10:31 +01:00

61 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 <LibLocale/Locale.h>
namespace JS::Intl {
class ListFormat final : public Object {
JS_OBJECT(ListFormat, Object);
JS_DECLARE_ALLOCATOR(ListFormat);
public:
enum class Type {
Invalid,
Conjunction,
Disjunction,
Unit,
};
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;
::Locale::Style style() const { return m_style; }
void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
StringView style_string() const { return ::Locale::style_to_string(m_style); }
private:
explicit ListFormat(Object& prototype);
String m_locale; // [[Locale]]
Type m_type { Type::Invalid }; // [[Type]]
::Locale::Style m_style { ::Locale::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);
NonnullGCPtr<Array> format_list_to_parts(VM&, ListFormat const&, Vector<String> const& list);
ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM&, Value iterable);
}