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

LibJS: Replace GlobalObject with VM in Value AOs [Part 4/19]

This is where the fun begins. :^)
This commit is contained in:
Linus Groh 2022-08-21 14:00:56 +01:00
parent f6c4a0f5d0
commit a022e548b8
129 changed files with 1230 additions and 1325 deletions

View file

@ -186,7 +186,6 @@ bool is_well_formed_unit_identifier(StringView unit_identifier)
ThrowCompletionOr<Vector<String>> canonicalize_locale_list(VM& vm, Value locales)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. If locales is undefined, then
if (locales.is_undefined()) {
@ -206,12 +205,12 @@ ThrowCompletionOr<Vector<String>> canonicalize_locale_list(VM& vm, Value locales
// 4. Else,
else {
// a. Let O be ? ToObject(locales).
object = TRY(locales.to_object(global_object));
object = TRY(locales.to_object(vm));
}
// 5. Let len be ? ToLength(? Get(O, "length")).
auto length_value = TRY(object->get(vm.names.length));
auto length = TRY(length_value.to_length(global_object));
auto length = TRY(length_value.to_length(vm));
// 6. Let k be 0.
// 7. Repeat, while k < len,
@ -241,7 +240,7 @@ ThrowCompletionOr<Vector<String>> canonicalize_locale_list(VM& vm, Value locales
// iv. Else,
else {
// 1. Let tag be ? ToString(kValue).
tag = TRY(key_value.to_string(global_object));
tag = TRY(key_value.to_string(vm));
}
// v. If ! IsStructurallyValidLanguageTag(tag) is false, throw a RangeError exception.
@ -597,7 +596,6 @@ ThrowCompletionOr<Array*> supported_locales(VM& vm, Vector<String> const& reques
ThrowCompletionOr<Object*> coerce_options_to_object(VM& vm, Value options)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. If options is undefined, then
if (options.is_undefined()) {
@ -606,7 +604,7 @@ ThrowCompletionOr<Object*> coerce_options_to_object(VM& vm, Value options)
}
// 2. Return ? ToObject(options).
return TRY(options.to_object(global_object));
return TRY(options.to_object(vm));
}
// NOTE: 9.2.13 GetOption has been removed and is being pulled in from ECMA-262 in the Temporal proposal.
@ -614,9 +612,6 @@ ThrowCompletionOr<Object*> coerce_options_to_object(VM& vm, Value options)
// 1.2.12 GetStringOrBooleanOption ( options, property, values, trueValue, falsyValue, fallback ), https://tc39.es/proposal-intl-numberformat-v3/out/negotiation/proposed.html#sec-getstringorbooleanoption
ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object const& options, PropertyKey const& property, Span<StringView const> values, StringOrBoolean true_value, StringOrBoolean falsy_value, StringOrBoolean fallback)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let value be ? Get(options, property).
auto value = TRY(options.get(property));
@ -636,7 +631,7 @@ ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object c
return falsy_value;
// 6. Let value be ? ToString(value).
auto value_string = TRY(value.to_string(global_object));
auto value_string = TRY(value.to_string(vm));
// 7. If values does not contain an element equal to value, return fallback.
auto it = find(values.begin(), values.end(), value_string);
@ -650,15 +645,12 @@ ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object c
// 9.2.14 DefaultNumberOption ( value, minimum, maximum, fallback ), https://tc39.es/ecma402/#sec-defaultnumberoption
ThrowCompletionOr<Optional<int>> default_number_option(VM& vm, Value value, int minimum, int maximum, Optional<int> fallback)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. If value is undefined, return fallback.
if (value.is_undefined())
return fallback;
// 2. Set value to ? ToNumber(value).
value = TRY(value.to_number(global_object));
value = TRY(value.to_number(vm));
// 3. If value is NaN or less than minimum or greater than maximum, throw a RangeError exception.
if (value.is_nan() || (value.as_double() < minimum) || (value.as_double() > maximum))

View file

@ -51,7 +51,6 @@ double compare_strings(Collator& collator, Utf8View const& x, Utf8View const& y)
ThrowCompletionOr<Value> CollatorCompareFunction::call()
{
auto& vm = this->vm();
auto& global_object = this->global_object();
// 1. Let collator be F.[[Collator]].
// 2. Assert: Type(collator) is Object and collator has an [[InitializedCollator]] internal slot.
@ -59,9 +58,9 @@ ThrowCompletionOr<Value> CollatorCompareFunction::call()
// 4. If y is not provided, let y be undefined.
// 5. Let X be ? ToString(x).
auto x = TRY(vm.argument(0).to_string(global_object));
auto x = TRY(vm.argument(0).to_string(vm));
// 6. Let Y be ? ToString(y).
auto y = TRY(vm.argument(1).to_string(global_object));
auto y = TRY(vm.argument(1).to_string(vm));
// 7. Return CompareStrings(collator, X, Y).
return compare_strings(m_collator, Utf8View(x), Utf8View(y));

View file

@ -17,9 +17,6 @@ namespace JS::Intl {
// 10.1.2 InitializeCollator ( collator, locales, options ), https://tc39.es/ecma402/#sec-initializecollator
static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collator, 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));
@ -66,7 +63,7 @@ static ThrowCompletionOr<Collator*> initialize_collator(VM& vm, Collator& collat
// a. Let numeric be ! ToString(numeric).
// 15. Set opt.[[kn]] to numeric.
if (!numeric.is_undefined())
opt.kn = MUST(numeric.to_string(global_object));
opt.kn = MUST(numeric.to_string(vm));
// 16. Let caseFirst be ? GetOption(options, "caseFirst", "string", « "upper", "lower", "false" », undefined).
// 17. Set opt.[[kf]] to caseFirst.

View file

@ -67,12 +67,11 @@ StringView DateTimeFormat::style_to_string(Style style)
ThrowCompletionOr<Object*> to_date_time_options(VM& vm, Value options_value, OptionRequired required, OptionDefaults defaults)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. If options is undefined, let options be null; otherwise let options be ? ToObject(options).
Object* options = nullptr;
if (!options_value.is_undefined())
options = TRY(options_value.to_object(global_object));
options = TRY(options_value.to_object(vm));
// 2. Let options be OrdinaryObjectCreate(options).
options = Object::create(realm, options);

View file

@ -85,9 +85,6 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of)
// 11.1.2 InitializeDateTimeFormat ( dateTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-initializedatetimeformat
ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeFormat& date_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));
@ -225,7 +222,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(VM& vm, DateTimeF
// 31. Else,
else {
// a. Set timeZone to ? ToString(timeZone).
time_zone = TRY(time_zone_value.to_string(global_object));
time_zone = TRY(time_zone_value.to_string(vm));
// b. If the result of IsValidTimeZoneName(timeZone) is false, then
if (!Temporal::is_valid_time_zone_name(time_zone)) {

View file

@ -54,7 +54,7 @@ ThrowCompletionOr<Value> DateTimeFormatFunction::call()
// 4. Else,
else {
// a. Let x be ? ToNumber(date).
date_value = TRY(date.to_number(global_object)).as_double();
date_value = TRY(date.to_number(vm)).as_double();
}
// 5. Return ? FormatDateTime(dtf, x).

View file

@ -81,7 +81,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_to_parts)
// 4. Else,
else {
// a. Let x be ? ToNumber(date).
date_value = TRY(date.to_number(global_object)).as_double();
date_value = TRY(date.to_number(vm)).as_double();
}
// 5. Return ? FormatDateTimeToParts(dtf, x).
@ -105,10 +105,10 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_range)
return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "endDate"sv);
// 4. Let x be ? ToNumber(startDate).
auto start_date_number = TRY(start_date.to_number(global_object)).as_double();
auto start_date_number = TRY(start_date.to_number(vm)).as_double();
// 5. Let y be ? ToNumber(endDate).
auto end_date_number = TRY(end_date.to_number(global_object)).as_double();
auto end_date_number = TRY(end_date.to_number(vm)).as_double();
// 6. Return ? FormatDateTimeRange(dtf, x, y).
auto formatted = TRY(format_date_time_range(vm, *date_time_format, start_date_number, end_date_number));
@ -132,10 +132,10 @@ JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatPrototype::format_range_to_parts)
return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "endDate"sv);
// 4. Let x be ? ToNumber(startDate).
auto start_date_number = TRY(start_date.to_number(global_object)).as_double();
auto start_date_number = TRY(start_date.to_number(vm)).as_double();
// 5. Let y be ? ToNumber(endDate).
auto end_date_number = TRY(end_date.to_number(global_object)).as_double();
auto end_date_number = TRY(end_date.to_number(vm)).as_double();
// 6. Return ? FormatDateTimeRangeToParts(dtf, x, y).
return TRY(format_date_time_range_to_parts(vm, *date_time_format, start_date_number, end_date_number));

View file

@ -43,7 +43,7 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of)
auto* display_names = TRY(typed_this_object(vm));
// 3. Let code be ? ToString(code).
auto code_string = TRY(code.to_string(global_object));
auto code_string = TRY(code.to_string(vm));
code = js_string(vm, move(code_string));
// 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code).

View file

@ -112,7 +112,7 @@ JS_DEFINE_NATIVE_FUNCTION(Intl::supported_values_of)
auto& realm = *global_object.associated_realm();
// 1. Let key be ? ToString(key).
auto key = TRY(vm.argument(0).to_string(global_object));
auto key = TRY(vm.argument(0).to_string(vm));
Span<StringView const> list;

View file

@ -275,7 +275,7 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
// 9. Else,
else {
// a. Let tag be ? ToString(tag).
tag = TRY(tag_value.to_string(global_object));
tag = TRY(tag_value.to_string(vm));
}
// 10. Set options to ? CoerceOptionsToObject(options).
@ -313,7 +313,7 @@ ThrowCompletionOr<Object*> LocaleConstructor::construct(FunctionObject& new_targ
// 24. If kn is not undefined, set kn to ! ToString(kn).
// 25. Set opt.[[kn]] to kn.
if (!kn.is_undefined())
opt.kn = TRY(kn.to_string(global_object));
opt.kn = TRY(kn.to_string(vm));
// 26. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
// 27. If numberingSystem is not undefined, then

View file

@ -1580,11 +1580,9 @@ int compute_exponent_for_magnitude(NumberFormat& number_format, int magnitude)
// 1.1.18 ToIntlMathematicalValue ( value ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-tointlmathematicalvalue
ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM& vm, Value value)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let primValue be ? ToPrimitive(value, number).
auto primitive_value = TRY(value.to_primitive(global_object, Value::PreferredType::Number));
auto primitive_value = TRY(value.to_primitive(vm, Value::PreferredType::Number));
// 2. If Type(primValue) is BigInt, return the mathematical value of primValue.
if (primitive_value.is_bigint())
@ -1594,7 +1592,7 @@ ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM& vm, Value va
// We short-circuit some of these steps to avoid known pitfalls.
// See: https://github.com/tc39/proposal-intl-numberformat-v3/pull/82
if (!primitive_value.is_string()) {
auto number = TRY(primitive_value.to_number(global_object));
auto number = TRY(primitive_value.to_number(vm));
return number.as_double();
}
@ -1606,7 +1604,7 @@ ThrowCompletionOr<MathematicalValue> to_intl_mathematical_value(VM& vm, Value va
// 5. If the grammar cannot interpret str as an expansion of StringNumericLiteral, return not-a-number.
// 6. Let mv be the MV, a mathematical value, of ? ToNumber(str), as described in 7.1.4.1.1.
auto mathematical_value = TRY(primitive_value.to_number(global_object)).as_double();
auto mathematical_value = TRY(primitive_value.to_number(vm)).as_double();
// 7. If mv is 0 and the first non white space code point in str is -, return negative-zero.
if (mathematical_value == 0.0 && string.view().trim_whitespace(TrimMode::Left).starts_with('-'))

View file

@ -41,7 +41,7 @@ JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::select)
auto* plural_rules = TRY(typed_this_object(vm));
// 3. Let n be ? ToNumber(value).
auto number = TRY(vm.argument(0).to_number(global_object));
auto number = TRY(vm.argument(0).to_number(vm));
// 4. Return ! ResolvePlural(pr, n).
auto plurality = resolve_plural(*plural_rules, number);
@ -65,10 +65,10 @@ JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::select_range)
return vm.throw_completion<TypeError>(ErrorType::IsUndefined, "end"sv);
// 4. Let x be ? ToNumber(start).
auto x = TRY(start.to_number(global_object));
auto x = TRY(start.to_number(vm));
// 5. Let y be ? ToNumber(end).
auto y = TRY(end.to_number(global_object));
auto y = TRY(end.to_number(vm));
// 6. Return ? ResolvePluralRange(pr, x, y).
auto plurality = TRY(resolve_plural_range(vm, *plural_rules, x, y));

View file

@ -93,9 +93,6 @@ ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(VM& vm, StringV
// 17.5.2 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(VM& vm, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Assert: relativeTimeFormat has an [[InitializedRelativeTimeFormat]] internal slot.
// 2. Assert: Type(value) is Number.
// 3. Assert: Type(unit) is String.
@ -144,7 +141,7 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_patt
// 16. If numeric is equal to "auto", then
if (relative_time_format.numeric() == RelativeTimeFormat::Numeric::Auto) {
// a. Let valueString be ToString(value).
auto value_string = MUST(Value(value).to_string(global_object));
auto value_string = MUST(Value(value).to_string(vm));
// b. If patterns has a field [[<valueString>]], then
if (auto patterns = find_patterns_for_tense_or_number(value_string); !patterns.is_empty()) {

View file

@ -39,10 +39,10 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::format)
auto* relative_time_format = TRY(typed_this_object(vm));
// 3. Let value be ? ToNumber(value).
auto value = TRY(vm.argument(0).to_number(global_object));
auto value = TRY(vm.argument(0).to_number(vm));
// 4. Let unit be ? ToString(unit).
auto unit = TRY(vm.argument(1).to_string(global_object));
auto unit = TRY(vm.argument(1).to_string(vm));
// 5. Return ? FormatRelativeTime(relativeTimeFormat, value, unit).
auto formatted = TRY(format_relative_time(vm, *relative_time_format, value.as_double(), unit));
@ -57,10 +57,10 @@ JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatPrototype::format_to_parts)
auto* relative_time_format = TRY(typed_this_object(vm));
// 3. Let value be ? ToNumber(value).
auto value = TRY(vm.argument(0).to_number(global_object));
auto value = TRY(vm.argument(0).to_number(vm));
// 4. Let unit be ? ToString(unit).
auto unit = TRY(vm.argument(1).to_string(global_object));
auto unit = TRY(vm.argument(1).to_string(vm));
// 5. Return ? FormatRelativeTimeToParts(relativeTimeFormat, value, unit).
return TRY(format_relative_time_to_parts(vm, *relative_time_format, value.as_double(), unit));

View file

@ -65,7 +65,7 @@ JS_DEFINE_NATIVE_FUNCTION(SegmenterPrototype::segment)
auto* segmenter = TRY(typed_this_object(vm));
// 3. Let string be ? ToString(string).
auto string = TRY(vm.argument(0).to_utf16_string(global_object));
auto string = TRY(vm.argument(0).to_utf16_string(vm));
// 4. Return ! CreateSegmentsObject(segmenter, string).
return Segments::create(realm, *segmenter, move(string));

View file

@ -45,7 +45,7 @@ JS_DEFINE_NATIVE_FUNCTION(SegmentsPrototype::containing)
auto length = string.length_in_code_units();
// 6. Let n be ? ToIntegerOrInfinity(index).
auto n = TRY(vm.argument(0).to_integer_or_infinity(global_object));
auto n = TRY(vm.argument(0).to_integer_or_infinity(vm));
// 7. If n < 0 or n ≥ len, return undefined.
if (n < 0 || n >= length)