1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-03 14:38:18 +00:00

LibJS: Make Temporal's get_option() take a PropertyName directly

Instead of constructing a String and converting that to a PropertyName
on the fly, we can just leverage CommonPropertyNames, add a couple more
and directly pass ready-to-use PropertyNames with pre-allocated Strings.
This commit is contained in:
Linus Groh 2021-08-18 20:27:44 +01:00 committed by Andreas Kling
parent 705e5a3d87
commit 310192f918
3 changed files with 16 additions and 8 deletions

View file

@ -290,6 +290,7 @@ namespace JS {
P(of) \ P(of) \
P(offset) \ P(offset) \
P(offsetNanoseconds) \ P(offsetNanoseconds) \
P(overflow) \
P(ownKeys) \ P(ownKeys) \
P(padEnd) \ P(padEnd) \
P(padStart) \ P(padStart) \
@ -320,6 +321,8 @@ namespace JS {
P(revocable) \ P(revocable) \
P(revoke) \ P(revoke) \
P(round) \ P(round) \
P(roundingIncrement) \
P(roundingMode) \
P(seal) \ P(seal) \
P(second) \ P(second) \
P(seconds) \ P(seconds) \
@ -358,6 +361,7 @@ namespace JS {
P(size) \ P(size) \
P(slice) \ P(slice) \
P(small) \ P(small) \
P(smallestUnit) \
P(some) \ P(some) \
P(sort) \ P(sort) \
P(source) \ P(source) \

View file

@ -8,6 +8,7 @@
#include <AK/CharacterTypes.h> #include <AK/CharacterTypes.h>
#include <AK/DateTimeLexer.h> #include <AK/DateTimeLexer.h>
#include <LibJS/Runtime/IteratorOperations.h> #include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/PropertyName.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h> #include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Duration.h> #include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/PlainDate.h> #include <LibJS/Runtime/Temporal/PlainDate.h>
@ -97,8 +98,10 @@ Object* get_options_object(GlobalObject& global_object, Value options)
} }
// 13.3 GetOption ( options, property, types, values, fallback ), https://tc39.es/proposal-temporal/#sec-getoption // 13.3 GetOption ( options, property, types, values, fallback ), https://tc39.es/proposal-temporal/#sec-getoption
Value get_option(GlobalObject& global_object, Object& options, String const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback) Value get_option(GlobalObject& global_object, Object& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback)
{ {
VERIFY(property.is_string());
auto& vm = global_object.vm(); auto& vm = global_object.vm();
// 1. Assert: Type(options) is Object. // 1. Assert: Type(options) is Object.
@ -138,7 +141,7 @@ Value get_option(GlobalObject& global_object, Object& options, String const& pro
return {}; return {};
// b. If value is NaN, throw a RangeError exception. // b. If value is NaN, throw a RangeError exception.
if (value.is_nan()) { if (value.is_nan()) {
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, vm.names.NaN.as_string(), property); vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, vm.names.NaN.as_string(), property.as_string());
return {}; return {};
} }
} }
@ -155,7 +158,7 @@ Value get_option(GlobalObject& global_object, Object& options, String const& pro
VERIFY(value.is_string()); VERIFY(value.is_string());
// a. If values does not contain value, throw a RangeError exception. // a. If values does not contain value, throw a RangeError exception.
if (!values.contains_slow(value.as_string().string())) { if (!values.contains_slow(value.as_string().string())) {
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property); vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, value.as_string().string(), property.as_string());
return {}; return {};
} }
} }
@ -170,7 +173,7 @@ Optional<String> to_temporal_overflow(GlobalObject& global_object, Object& norma
auto& vm = global_object.vm(); auto& vm = global_object.vm();
// 1. Return ? GetOption(normalizedOptions, "overflow", « String », « "constrain", "reject" », "constrain"). // 1. Return ? GetOption(normalizedOptions, "overflow", « String », « "constrain", "reject" », "constrain").
auto option = get_option(global_object, normalized_options, "overflow", { OptionType::String }, { "constrain"sv, "reject"sv }, js_string(vm, "constrain")); auto option = get_option(global_object, normalized_options, vm.names.overflow, { OptionType::String }, { "constrain"sv, "reject"sv }, js_string(vm, "constrain"));
if (vm.exception()) if (vm.exception())
return {}; return {};
@ -183,7 +186,8 @@ Optional<String> to_temporal_rounding_mode(GlobalObject& global_object, Object&
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto option = get_option(global_object, normalized_options, "roundingMode", { OptionType::String }, { "ceil"sv, "floor"sv, "trunc"sv, "halfExpand"sv }, js_string(vm, fallback)); // 1. Return ? GetOption(normalizedOptions, "roundingMode", « String », « "ceil", "floor", "trunc", "halfExpand" », fallback).
auto option = get_option(global_object, normalized_options, vm.names.roundingMode, { OptionType::String }, { "ceil"sv, "floor"sv, "trunc"sv, "halfExpand"sv }, js_string(vm, fallback));
if (vm.exception()) if (vm.exception())
return {}; return {};
@ -219,7 +223,7 @@ u64 to_temporal_rounding_increment(GlobalObject& global_object, Object& normaliz
} }
// 5. Let increment be ? GetOption(normalizedOptions, "roundingIncrement", « Number », empty, 1). // 5. Let increment be ? GetOption(normalizedOptions, "roundingIncrement", « Number », empty, 1).
auto increment_value = get_option(global_object, normalized_options, "roundingIncrement", { OptionType::Number }, {}, Value(1)); auto increment_value = get_option(global_object, normalized_options, vm.names.roundingIncrement, { OptionType::Number }, {}, Value(1));
if (vm.exception()) if (vm.exception())
return {}; return {};
VERIFY(increment_value.is_number()); VERIFY(increment_value.is_number());
@ -267,7 +271,7 @@ Optional<String> to_smallest_temporal_unit(GlobalObject& global_object, Object&
// 1. Assert: disallowedUnits does not contain fallback. // 1. Assert: disallowedUnits does not contain fallback.
// 2. Let smallestUnit be ? GetOption(normalizedOptions, "smallestUnit", « String », « "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback). // 2. Let smallestUnit be ? GetOption(normalizedOptions, "smallestUnit", « String », « "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback).
auto smallest_unit_value = get_option(global_object, normalized_options, "smallestUnit"sv, { OptionType::String }, { "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, fallback.has_value() ? js_string(vm, *fallback) : js_undefined()); auto smallest_unit_value = get_option(global_object, normalized_options, vm.names.smallestUnit, { OptionType::String }, { "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, fallback.has_value() ? js_string(vm, *fallback) : js_undefined());
if (vm.exception()) if (vm.exception())
return {}; return {};

View file

@ -61,7 +61,7 @@ struct TemporalTimeZone {
MarkedValueList iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types); MarkedValueList iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types);
Object* get_options_object(GlobalObject&, Value options); Object* get_options_object(GlobalObject&, Value options);
Value get_option(GlobalObject&, Object& options, String const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback); Value get_option(GlobalObject&, Object& options, PropertyName const& property, Vector<OptionType> const& types, Vector<StringView> const& values, Value fallback);
Optional<String> to_temporal_overflow(GlobalObject&, Object& normalized_options); Optional<String> to_temporal_overflow(GlobalObject&, Object& normalized_options);
Optional<String> to_temporal_rounding_mode(GlobalObject&, Object& normalized_options, String const& fallback); Optional<String> to_temporal_rounding_mode(GlobalObject&, Object& normalized_options, String const& fallback);
u64 to_temporal_rounding_increment(GlobalObject&, Object& normalized_options, Optional<double> dividend, bool inclusive); u64 to_temporal_rounding_increment(GlobalObject&, Object& normalized_options, Optional<double> dividend, bool inclusive);