diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-18 20:27:44 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-19 00:14:17 +0200 |
commit | 310192f918ef013ab19dbdc01397910598ac3e8c (patch) | |
tree | 67d95e53c642a1775888735b98ceb15d9e3e3c03 /Userland/Libraries/LibJS | |
parent | 705e5a3d8779b81f66d016678f398e1cacb46569 (diff) | |
download | serenity-310192f918ef013ab19dbdc01397910598ac3e8c.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibJS')
3 files changed, 16 insertions, 8 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 3475e85da4..47a2130986 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -290,6 +290,7 @@ namespace JS { P(of) \ P(offset) \ P(offsetNanoseconds) \ + P(overflow) \ P(ownKeys) \ P(padEnd) \ P(padStart) \ @@ -320,6 +321,8 @@ namespace JS { P(revocable) \ P(revoke) \ P(round) \ + P(roundingIncrement) \ + P(roundingMode) \ P(seal) \ P(second) \ P(seconds) \ @@ -358,6 +361,7 @@ namespace JS { P(size) \ P(slice) \ P(small) \ + P(smallestUnit) \ P(some) \ P(sort) \ P(source) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 93e6fb4f1f..2a7b357da9 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -8,6 +8,7 @@ #include <AK/CharacterTypes.h> #include <AK/DateTimeLexer.h> #include <LibJS/Runtime/IteratorOperations.h> +#include <LibJS/Runtime/PropertyName.h> #include <LibJS/Runtime/Temporal/AbstractOperations.h> #include <LibJS/Runtime/Temporal/Duration.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 -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(); // 1. Assert: Type(options) is Object. @@ -138,7 +141,7 @@ Value get_option(GlobalObject& global_object, Object& options, String const& pro return {}; // b. If value is NaN, throw a RangeError exception. 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 {}; } } @@ -155,7 +158,7 @@ Value get_option(GlobalObject& global_object, Object& options, String const& pro VERIFY(value.is_string()); // a. If values does not contain value, throw a RangeError exception. 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 {}; } } @@ -170,7 +173,7 @@ Optional<String> to_temporal_overflow(GlobalObject& global_object, Object& norma auto& vm = global_object.vm(); // 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()) return {}; @@ -183,7 +186,8 @@ Optional<String> to_temporal_rounding_mode(GlobalObject& global_object, Object& { 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()) 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). - 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()) return {}; 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. // 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()) return {}; diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index 50dc40376d..c74f36c7e7 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -61,7 +61,7 @@ struct TemporalTimeZone { MarkedValueList iterable_to_list_of_type(GlobalObject&, Value items, Vector<OptionType> const& element_types); 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_rounding_mode(GlobalObject&, Object& normalized_options, String const& fallback); u64 to_temporal_rounding_increment(GlobalObject&, Object& normalized_options, Optional<double> dividend, bool inclusive); |