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

LibJS+LibWeb: Reduce use of GlobalObject as an intermediary

- Prefer VM::current_realm() over GlobalObject::associated_realm()
- Prefer VM::heap() over GlobalObject::heap()
- Prefer Cell::vm() over Cell::global_object()
- Prefer Wrapper::vm() over Wrapper::global_object()
- Inline Realm::global_object() calls used to access intrinsics as they
  will later perform a direct lookup without going through the global
  object
This commit is contained in:
Linus Groh 2022-08-22 19:00:49 +01:00
parent e3895e6c80
commit b345a0acca
58 changed files with 157 additions and 231 deletions

View file

@ -532,7 +532,6 @@ static Optional<StringView> resolve_day_period(StringView locale, StringView cal
ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(VM& vm, DateTimeFormat& date_time_format, Vector<PatternPartition> pattern_parts, double time, Unicode::CalendarPattern const* range_format_options)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let x be TimeClip(x).
time = time_clip(time);
@ -546,7 +545,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, *global_object.intl_number_format_constructor(), js_string(vm, locale), options));
auto* number_format = TRY(construct(vm, *realm.global_object().intl_number_format_constructor(), js_string(vm, locale), options));
return static_cast<NumberFormat*>(number_format);
};
@ -849,7 +848,6 @@ ThrowCompletionOr<String> format_date_time(VM& vm, DateTimeFormat& date_time_for
ThrowCompletionOr<Array*> format_date_time_to_parts(VM& vm, DateTimeFormat& date_time_format, double time)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let parts be ? PartitionDateTimePattern(dateTimeFormat, x).
auto parts = TRY(partition_date_time_pattern(vm, date_time_format, time));
@ -863,7 +861,7 @@ ThrowCompletionOr<Array*> format_date_time_to_parts(VM& vm, DateTimeFormat& date
// 4. For each Record { [[Type]], [[Value]] } part in parts, do
for (auto& part : parts) {
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
auto* object = Object::create(realm, global_object.object_prototype());
auto* object = Object::create(realm, realm.global_object().object_prototype());
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
@ -1164,7 +1162,6 @@ ThrowCompletionOr<String> format_date_time_range(VM& vm, DateTimeFormat& date_ti
ThrowCompletionOr<Array*> format_date_time_range_to_parts(VM& vm, DateTimeFormat& date_time_format, double start, double end)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y).
auto parts = TRY(partition_date_time_range_pattern(vm, date_time_format, start, end));
@ -1178,7 +1175,7 @@ ThrowCompletionOr<Array*> format_date_time_range_to_parts(VM& vm, DateTimeFormat
// 4. For each Record { [[Type]], [[Value]], [[Source]] } part in parts, do
for (auto& part : parts) {
// a. Let O be OrdinaryObjectCreate(%ObjectPrototype%).
auto* object = Object::create(realm, global_object.object_prototype());
auto* object = Object::create(realm, realm.global_object().object_prototype());
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));

View file

@ -36,8 +36,8 @@ void DateTimeFormatFunction::initialize(Realm& realm)
ThrowCompletionOr<Value> DateTimeFormatFunction::call()
{
auto& global_object = this->global_object();
auto& vm = global_object.vm();
auto& vm = this->vm();
auto& realm = *vm.current_realm();
auto date = vm.argument(0);
@ -49,7 +49,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, global_object.date_constructor_now_function(), js_undefined())).as_double();
date_value = MUST(JS::call(vm, realm.global_object().date_constructor_now_function(), js_undefined())).as_double();
}
// 4. Else,
else {

View file

@ -307,7 +307,6 @@ static String convert_number_format_pattern_to_duration_format_template(Unicode:
ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM& vm, DurationFormat const& duration_format, Temporal::DurationRecord const& duration)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let result be a new empty List.
Vector<PatternPartition> result;
@ -403,7 +402,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM
}
// o. Let nf be ? Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
auto* number_format = static_cast<Intl::NumberFormat*>(TRY(construct(vm, *global_object.intl_number_format_constructor(), js_string(vm, duration_format.locale()), number_format_options)));
auto* number_format = static_cast<Intl::NumberFormat*>(TRY(construct(vm, *realm.global_object().intl_number_format_constructor(), js_string(vm, duration_format.locale()), number_format_options)));
// FIXME: durationFormat.[[NumberFormat]] is not a thing, the spec likely means 'nf' in this case
// p. Let num be ! FormatNumeric(durationFormat.[[NumberFormat]], value).
@ -432,7 +431,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM
// t. Else,
else {
// i. Let pr be ? Construct(%PluralRules%, « durationFormat.[[Locale]] »).
auto* plural_rules = TRY(construct(vm, *global_object.intl_plural_rules_constructor(), js_string(vm, duration_format.locale())));
auto* plural_rules = TRY(construct(vm, *realm.global_object().intl_plural_rules_constructor(), js_string(vm, duration_format.locale())));
// ii. Let prv be ! ResolvePlural(pr, value).
auto plurality = resolve_plural(static_cast<PluralRules&>(*plural_rules), value);
@ -480,7 +479,7 @@ ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM
}
// 3. Let lf be ? Construct(%ListFormat%, « durationFormat.[[Locale]] »).
auto* list_format = static_cast<Intl::ListFormat*>(TRY(construct(vm, *global_object.intl_list_format_constructor(), js_string(vm, duration_format.locale()))));
auto* list_format = static_cast<Intl::ListFormat*>(TRY(construct(vm, *realm.global_object().intl_list_format_constructor(), js_string(vm, duration_format.locale()))));
// 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

@ -204,7 +204,6 @@ String format_list(ListFormat const& list_format, Vector<String> const& list)
Array* format_list_to_parts(VM& vm, ListFormat const& list_format, Vector<String> const& list)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let parts be ! CreatePartsFromList(listFormat, list).
auto parts = create_parts_from_list(list_format, list);
@ -218,7 +217,7 @@ Array* format_list_to_parts(VM& vm, ListFormat const& list_format, Vector<String
// 4. For each Record { [[Type]], [[Value]] } part in parts, do
for (auto& part : parts) {
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
auto* object = Object::create(realm, global_object.object_prototype());
auto* object = Object::create(realm, realm.global_object().object_prototype());
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));

View file

@ -908,7 +908,6 @@ String format_numeric(VM& vm, NumberFormat& number_format, MathematicalValue num
Array* format_numeric_to_parts(VM& vm, NumberFormat& number_format, MathematicalValue number)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let parts be ? PartitionNumberPattern(numberFormat, x).
// Note: Our implementation of PartitionNumberPattern does not throw.
@ -923,7 +922,7 @@ Array* format_numeric_to_parts(VM& vm, NumberFormat& number_format, Mathematical
// 4. For each Record { [[Type]], [[Value]] } part in parts, do
for (auto& part : parts) {
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
auto* object = Object::create(realm, global_object.object_prototype());
auto* object = Object::create(realm, realm.global_object().object_prototype());
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
@ -1825,7 +1824,6 @@ ThrowCompletionOr<String> format_numeric_range(VM& vm, NumberFormat& number_form
ThrowCompletionOr<Array*> format_numeric_range_to_parts(VM& vm, NumberFormat& number_format, MathematicalValue start, MathematicalValue end)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let parts be ? PartitionNumberRangePattern(numberFormat, x, y).
auto parts = TRY(partition_number_range_pattern(vm, number_format, move(start), move(end)));
@ -1839,7 +1837,7 @@ ThrowCompletionOr<Array*> format_numeric_range_to_parts(VM& vm, NumberFormat& nu
// 4. For each Record { [[Type]], [[Value]] } part in parts, do
for (auto& part : parts) {
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
auto* object = Object::create(realm, global_object.object_prototype());
auto* object = Object::create(realm, realm.global_object().object_prototype());
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));

View file

@ -244,7 +244,6 @@ ThrowCompletionOr<String> format_relative_time(VM& vm, RelativeTimeFormat& relat
ThrowCompletionOr<Array*> format_relative_time_to_parts(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
auto parts = TRY(partition_relative_time_pattern(vm, relative_time_format, value, unit));
@ -258,7 +257,7 @@ ThrowCompletionOr<Array*> format_relative_time_to_parts(VM& vm, RelativeTimeForm
// 4. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
for (auto& part : parts) {
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
auto* object = Object::create(realm, global_object.object_prototype());
auto* object = Object::create(realm, realm.global_object().object_prototype());
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));

View file

@ -79,7 +79,6 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of)
ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
@ -139,11 +138,11 @@ ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(VM& vm, R
relative_time_format.set_numeric(numeric.as_string().string());
// 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
auto* number_format = MUST(construct(vm, *global_object.intl_number_format_constructor(), js_string(vm, locale)));
auto* number_format = MUST(construct(vm, *realm.global_object().intl_number_format_constructor(), js_string(vm, locale)));
relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format));
// 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
auto* plural_rules = MUST(construct(vm, *global_object.intl_plural_rules_constructor(), js_string(vm, locale)));
auto* plural_rules = MUST(construct(vm, *realm.global_object().intl_plural_rules_constructor(), js_string(vm, locale)));
relative_time_format.set_plural_rules(static_cast<PluralRules*>(plural_rules));
// 21. Return relativeTimeFormat.

View file

@ -48,7 +48,6 @@ StringView Segmenter::segmenter_granularity_string() const
Object* create_segment_data_object(VM& vm, Segmenter const& segmenter, Utf16View const& string, double start_index, double end_index)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let len be the length of string.
auto length = string.length_in_code_units();
@ -63,7 +62,7 @@ Object* create_segment_data_object(VM& vm, Segmenter const& segmenter, Utf16View
VERIFY(start_index < end_index);
// 5. Let result be OrdinaryObjectCreate(%Object.prototype%).
auto* result = Object::create(realm, global_object.object_prototype());
auto* result = Object::create(realm, realm.global_object().object_prototype());
// 6. Let segment be the substring of string from startIndex to endIndex.
auto segment = string.substring_view(start_index, end_index - start_index);