1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:57:35 +00:00

LibJS: Make intrinsics getters return NonnullGCPtr

Some of these are allocated upon initialization of the intrinsics, and
some lazily, but in neither case the getters actually return a nullptr.

This saves us a whole bunch of pointer dereferences (as NonnullGCPtr has
an `operator T&()`), and also has the interesting side effect of forcing
us to explicitly use the FunctionObject& overload of call(), as passing
a NonnullGCPtr is ambigous - it could implicitly be turned into a Value
_or_ a FunctionObject& (so we have to dereference manually).
This commit is contained in:
Linus Groh 2023-04-13 00:47:15 +02:00
parent ed9e2366da
commit b84f8fb55b
182 changed files with 564 additions and 567 deletions

View file

@ -17,7 +17,7 @@ NonnullGCPtr<CollatorCompareFunction> CollatorCompareFunction::create(Realm& rea
}
CollatorCompareFunction::CollatorCompareFunction(Realm& realm, Collator& collator)
: NativeFunction(*realm.intrinsics().function_prototype())
: NativeFunction(realm.intrinsics().function_prototype())
, m_collator(collator)
{
}

View file

@ -131,7 +131,7 @@ static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collat
// 10.1 The Intl.Collator Constructor, https://tc39.es/ecma402/#sec-the-intl-collator-constructor
CollatorConstructor::CollatorConstructor(Realm& realm)
: NativeFunction(realm.vm().names.Collator.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.Collator.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -13,7 +13,7 @@ namespace JS::Intl {
// 10.3 Properties of the Intl.Collator Prototype Object, https://tc39.es/ecma402/#sec-properties-of-the-intl-collator-prototype-object
CollatorPrototype::CollatorPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -551,7 +551,7 @@ ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(VM& vm, Dat
auto const& data_locale = date_time_format.data_locale();
auto construct_number_format = [&](auto& options) -> ThrowCompletionOr<NumberFormat*> {
auto number_format = TRY(construct(vm, *realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, locale), options));
auto number_format = TRY(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, locale), options));
return static_cast<NumberFormat*>(number_format.ptr());
};

View file

@ -19,7 +19,7 @@ namespace JS::Intl {
// 11.1 The Intl.DateTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-datetimeformat-constructor
DateTimeFormatConstructor::DateTimeFormatConstructor(Realm& realm)
: NativeFunction(realm.vm().names.DateTimeFormat.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.DateTimeFormat.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -16,7 +16,7 @@ namespace JS::Intl {
// 11.5.5 DateTime Format Functions, https://tc39.es/ecma402/#sec-datetime-format-functions
NonnullGCPtr<DateTimeFormatFunction> DateTimeFormatFunction::create(Realm& realm, DateTimeFormat& date_time_format)
{
return realm.heap().allocate<DateTimeFormatFunction>(realm, date_time_format, *realm.intrinsics().function_prototype()).release_allocated_value_but_fixme_should_propagate_errors();
return realm.heap().allocate<DateTimeFormatFunction>(realm, date_time_format, realm.intrinsics().function_prototype()).release_allocated_value_but_fixme_should_propagate_errors();
}
DateTimeFormatFunction::DateTimeFormatFunction(DateTimeFormat& date_time_format, Object& prototype)
@ -51,7 +51,7 @@ ThrowCompletionOr<Value> DateTimeFormatFunction::call()
// 3. If date is not provided or is undefined, then
if (date.is_undefined()) {
// a. Let x be ! Call(%Date.now%, undefined).
date_value = MUST(JS::call(vm, realm.intrinsics().date_constructor_now_function(), js_undefined())).as_double();
date_value = MUST(JS::call(vm, *realm.intrinsics().date_constructor_now_function(), js_undefined())).as_double();
}
// 4. Else,
else {

View file

@ -15,7 +15,7 @@ namespace JS::Intl {
// 11.3 Properties of the Intl.DateTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-datetimeformat-prototype-object
DateTimeFormatPrototype::DateTimeFormatPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}
@ -80,7 +80,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_to_parts)
// 3. If date is undefined, then
if (date.is_undefined()) {
// a. Let x be ! Call(%Date.now%, undefined).
date_value = MUST(call(vm, realm.intrinsics().date_constructor_now_function(), js_undefined())).as_double();
date_value = MUST(call(vm, *realm.intrinsics().date_constructor_now_function(), js_undefined())).as_double();
}
// 4. Else,
else {

View file

@ -17,7 +17,7 @@ namespace JS::Intl {
// 12.1 The Intl.DisplayNames Constructor, https://tc39.es/ecma402/#sec-intl-displaynames-constructor
DisplayNamesConstructor::DisplayNamesConstructor(Realm& realm)
: NativeFunction(realm.vm().names.DisplayNames.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.DisplayNames.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -15,7 +15,7 @@ namespace JS::Intl {
// 12.3 Properties of the Intl.DisplayNames Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-displaynames-prototype-object
DisplayNamesPrototype::DisplayNamesPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -438,7 +438,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM
// i. If style is "2-digit" or "numeric", then
if (style == DurationFormat::ValueStyle::TwoDigit || style == DurationFormat::ValueStyle::Numeric) {
// 1. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
auto* number_format = static_cast<NumberFormat*>(MUST(construct(vm, *realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), number_format_options)).ptr());
auto* number_format = static_cast<NumberFormat*>(MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), number_format_options)).ptr());
// 2. Let dataLocale be durationFormat.[[DataLocale]].
auto const& data_locale = duration_format.data_locale();
@ -502,7 +502,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM
MUST(number_format_options->create_data_property_or_throw(vm.names.unitDisplay, MUST_OR_THROW_OOM(PrimitiveString::create(vm, unicode_style))));
// 4. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
auto* number_format = static_cast<NumberFormat*>(MUST(construct(vm, *realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), number_format_options)).ptr());
auto* number_format = static_cast<NumberFormat*>(MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), number_format_options)).ptr());
// 5. Let parts be ! PartitionNumberPattern(nf, 𝔽(value)).
auto parts = MUST_OR_THROW_OOM(partition_number_pattern(vm, *number_format, MathematicalValue(value)));
@ -543,7 +543,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM
MUST(list_format_options->create_data_property_or_throw(vm.names.style, MUST_OR_THROW_OOM(PrimitiveString::create(vm, unicode_list_style))));
// 9. Let lf be ! Construct(%ListFormat%, « durationFormat.[[Locale]], lfOpts »).
auto* list_format = static_cast<ListFormat*>(MUST(construct(vm, *realm.intrinsics().intl_list_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), list_format_options)).ptr());
auto* list_format = static_cast<ListFormat*>(MUST(construct(vm, realm.intrinsics().intl_list_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), list_format_options)).ptr());
// FIXME: CreatePartsFromList expects a list of strings and creates a list of Pattern Partition records, but we already created a list of Pattern Partition records
// so we try to hack something together from it that looks mostly right

View file

@ -16,7 +16,7 @@ namespace JS::Intl {
// 1.2 The Intl.DurationFormat Constructor, https://tc39.es/proposal-intl-duration-format/#sec-intl-durationformat-constructor
DurationFormatConstructor::DurationFormatConstructor(Realm& realm)
: NativeFunction(realm.vm().names.DurationFormat.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.DurationFormat.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -14,7 +14,7 @@ namespace JS::Intl {
// 1.4 Properties of the Intl.DurationFormat Prototype Object, https://tc39.es/proposal-intl-duration-format/#sec-properties-of-intl-durationformat-prototype-object
DurationFormatPrototype::DurationFormatPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -28,7 +28,7 @@ namespace JS::Intl {
// 8 The Intl Object, https://tc39.es/ecma402/#intl-object
Intl::Intl(Realm& realm)
: Object(ConstructWithPrototypeTag::Tag, *realm.intrinsics().object_prototype())
: Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
{
}

View file

@ -16,7 +16,7 @@ namespace JS::Intl {
// 13.1 The Intl.ListFormat Constructor, https://tc39.es/ecma402/#sec-intl-listformat-constructor
ListFormatConstructor::ListFormatConstructor(Realm& realm)
: NativeFunction(realm.vm().names.ListFormat.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.ListFormat.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -14,7 +14,7 @@ namespace JS::Intl {
// 13.3 Properties of the Intl.ListFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-listformat-prototype-object
ListFormatPrototype::ListFormatPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -16,7 +16,7 @@ namespace JS::Intl {
ThrowCompletionOr<NonnullGCPtr<Locale>> Locale::create(Realm& realm, ::Locale::LocaleID locale_id)
{
auto locale = MUST_OR_THROW_OOM(realm.heap().allocate<Locale>(realm, *realm.intrinsics().intl_locale_prototype()));
auto locale = MUST_OR_THROW_OOM(realm.heap().allocate<Locale>(realm, realm.intrinsics().intl_locale_prototype()));
locale->set_locale(TRY_OR_THROW_OOM(realm.vm(), locale_id.to_string()));
for (auto& extension : locale_id.extensions) {

View file

@ -218,7 +218,7 @@ static ThrowCompletionOr<LocaleAndKeys> apply_unicode_extension_to_tag(VM& vm, S
// 14.1 The Intl.Locale Constructor, https://tc39.es/ecma402/#sec-intl-locale-constructor
LocaleConstructor::LocaleConstructor(Realm& realm)
: NativeFunction(realm.vm().names.Locale.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.Locale.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -15,7 +15,7 @@ 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(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -15,7 +15,7 @@ namespace JS::Intl {
// 15.1 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
NumberFormatConstructor::NumberFormatConstructor(Realm& realm)
: NativeFunction(realm.vm().names.NumberFormat.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.NumberFormat.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -13,7 +13,7 @@ namespace JS::Intl {
// 15.5.2 Number Format Functions, https://tc39.es/ecma402/#sec-number-format-functions
NonnullGCPtr<NumberFormatFunction> NumberFormatFunction::create(Realm& realm, NumberFormat& number_format)
{
return realm.heap().allocate<NumberFormatFunction>(realm, number_format, *realm.intrinsics().function_prototype()).release_allocated_value_but_fixme_should_propagate_errors();
return realm.heap().allocate<NumberFormatFunction>(realm, number_format, realm.intrinsics().function_prototype()).release_allocated_value_but_fixme_should_propagate_errors();
}
NumberFormatFunction::NumberFormatFunction(NumberFormat& number_format, Object& prototype)

View file

@ -15,7 +15,7 @@ namespace JS::Intl {
// 15.3 Properties of the Intl.NumberFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-numberformat-prototype-object
NumberFormatPrototype::NumberFormatPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -17,7 +17,7 @@ namespace JS::Intl {
// 16.1 The Intl.PluralRules Constructor, https://tc39.es/ecma402/#sec-intl-pluralrules-constructor
PluralRulesConstructor::PluralRulesConstructor(Realm& realm)
: NativeFunction(realm.vm().names.PluralRules.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.PluralRules.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -14,7 +14,7 @@ namespace JS::Intl {
// 16.3 Properties of the Intl.PluralRules Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-pluralrules-prototype-object
PluralRulesPrototype::PluralRulesPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -20,7 +20,7 @@ namespace JS::Intl {
// 17.1 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor
RelativeTimeFormatConstructor::RelativeTimeFormatConstructor(Realm& realm)
: NativeFunction(realm.vm().names.RelativeTimeFormat.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.RelativeTimeFormat.as_string(), realm.intrinsics().function_prototype())
{
}
@ -140,11 +140,11 @@ ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, R
relative_time_format.set_numeric(TRY(numeric.as_string().utf8_string_view()));
// 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
auto number_format = MUST(construct(vm, *realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, locale)));
auto number_format = MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, locale)));
relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format.ptr()));
// 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
auto plural_rules = MUST(construct(vm, *realm.intrinsics().intl_plural_rules_constructor(), PrimitiveString::create(vm, locale)));
auto plural_rules = MUST(construct(vm, realm.intrinsics().intl_plural_rules_constructor(), PrimitiveString::create(vm, locale)));
relative_time_format.set_plural_rules(static_cast<PluralRules*>(plural_rules.ptr()));
// 21. Return relativeTimeFormat.

View file

@ -12,7 +12,7 @@ namespace JS::Intl {
// 17.3 Properties of the Intl.RelativeTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-relativetimeformat-prototype-object
RelativeTimeFormatPrototype::RelativeTimeFormatPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -24,7 +24,7 @@ NonnullGCPtr<SegmentIterator> SegmentIterator::create(Realm& realm, Segmenter& s
// 18.6 Segment Iterator Objects, https://tc39.es/ecma402/#sec-segment-iterator-objects
SegmentIterator::SegmentIterator(Realm& realm, Segmenter& segmenter, Utf16View const& string, Segments const& segments)
: Object(ConstructWithPrototypeTag::Tag, *realm.intrinsics().intl_segment_iterator_prototype())
: Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().intl_segment_iterator_prototype())
, m_iterating_segmenter(segmenter)
, m_iterated_string(string)
, m_segments(segments)

View file

@ -14,7 +14,7 @@ namespace JS::Intl {
// 18.6.2 The %SegmentIteratorPrototype% Object, https://tc39.es/ecma402/#sec-%segmentiteratorprototype%-object
SegmentIteratorPrototype::SegmentIteratorPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().iterator_prototype())
: PrototypeObject(realm.intrinsics().iterator_prototype())
{
}

View file

@ -17,7 +17,7 @@ namespace JS::Intl {
// 18.1 The Intl.Segmenter Constructor, https://tc39.es/ecma402/#sec-intl-segmenter-constructor
SegmenterConstructor::SegmenterConstructor(Realm& realm)
: NativeFunction(realm.vm().names.Segmenter.as_string(), *realm.intrinsics().function_prototype())
: NativeFunction(realm.vm().names.Segmenter.as_string(), realm.intrinsics().function_prototype())
{
}

View file

@ -13,7 +13,7 @@ namespace JS::Intl {
// 18.3 Properties of the Intl.Segmenter Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-segmenter-prototype-object
SegmenterPrototype::SegmenterPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}

View file

@ -23,7 +23,7 @@ NonnullGCPtr<Segments> Segments::create(Realm& realm, Segmenter& segmenter, Utf1
// 18.5 Segments Objects, https://tc39.es/ecma402/#sec-segments-objects
Segments::Segments(Realm& realm, Segmenter& segmenter, Utf16String string)
: Object(ConstructWithPrototypeTag::Tag, *realm.intrinsics().intl_segments_prototype())
: Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().intl_segments_prototype())
, m_segments_segmenter(segmenter)
, m_segments_string(move(string))
{

View file

@ -13,7 +13,7 @@ namespace JS::Intl {
// 18.5.2 The %SegmentsPrototype% Object, https://tc39.es/ecma402/#sec-%segmentsprototype%-object
SegmentsPrototype::SegmentsPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
: PrototypeObject(realm.intrinsics().object_prototype())
{
}