summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp88
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp28
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h1
5 files changed, 119 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 31a5cbaa2c..3475e85da4 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -266,6 +266,7 @@ namespace JS {
P(log10) \
P(map) \
P(max) \
+ P(mergeFields) \
P(message) \
P(microsecond) \
P(microseconds) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
index 592cea56e6..8fee077dbe 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp
@@ -968,4 +968,92 @@ u8 iso_day(Object& temporal_object)
VERIFY_NOT_REACHED();
}
+// 12.1.45 DefaultMergeFields ( fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-defaultmergefields
+Object* default_merge_fields(GlobalObject& global_object, Object& fields, Object& additional_fields)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Let merged be ! OrdinaryObjectCreate(%Object.prototype%).
+ auto* merged = Object::create(global_object, global_object.object_prototype());
+
+ // 2. Let originalKeys be ? EnumerableOwnPropertyNames(fields, key).
+ auto original_keys = fields.enumerable_own_property_names(Object::PropertyKind::Key);
+ if (vm.exception())
+ return {};
+
+ // 3. For each element nextKey of originalKeys, do
+ for (auto& next_key : original_keys) {
+ // a. If nextKey is not "month" or "monthCode", then
+ if (next_key.as_string().string() != vm.names.month.as_string() && next_key.as_string().string() != vm.names.monthCode.as_string()) {
+ auto property_name = PropertyName::from_value(global_object, next_key);
+
+ // i. Let propValue be ? Get(fields, nextKey).
+ auto prop_value = fields.get(property_name);
+ if (vm.exception())
+ return {};
+
+ // ii. If propValue is not undefined, then
+ if (!prop_value.is_undefined()) {
+ // 1. Perform ! CreateDataPropertyOrThrow(merged, nextKey, propValue).
+ merged->create_data_property_or_throw(property_name, prop_value);
+ }
+ }
+ }
+
+ // 4. Let newKeys be ? EnumerableOwnPropertyNames(additionalFields, key).
+ auto new_keys = additional_fields.enumerable_own_property_names(Object::PropertyKind::Key);
+ if (vm.exception())
+ return {};
+
+ // IMPLEMENTATION DEFINED: This is an optimization, so we don't have to iterate new_keys three times (worst case), but only once.
+ bool new_keys_contains_month_or_month_code_property = false;
+
+ // 5. For each element nextKey of newKeys, do
+ for (auto& next_key : new_keys) {
+ auto property_name = PropertyName::from_value(global_object, next_key);
+
+ // a. Let propValue be ? Get(additionalFields, nextKey).
+ auto prop_value = additional_fields.get(property_name);
+ if (vm.exception())
+ return {};
+
+ // b. If propValue is not undefined, then
+ if (!prop_value.is_undefined()) {
+ // i. Perform ! CreateDataPropertyOrThrow(merged, nextKey, propValue).
+ merged->create_data_property_or_throw(property_name, prop_value);
+ }
+
+ // See comment above.
+ new_keys_contains_month_or_month_code_property |= next_key.as_string().string() == vm.names.month.as_string() || next_key.as_string().string() == vm.names.monthCode.as_string();
+ }
+
+ // 6. If newKeys does not contain either "month" or "monthCode", then
+ if (!new_keys_contains_month_or_month_code_property) {
+ // a. Let month be ? Get(fields, "month").
+ auto month = fields.get(vm.names.month);
+ if (vm.exception())
+ return {};
+
+ // b. If month is not undefined, then
+ if (!month.is_undefined()) {
+ // i. Perform ! CreateDataPropertyOrThrow(merged, "month", month).
+ merged->create_data_property_or_throw(vm.names.month, month);
+ }
+
+ // c. Let monthCode be ? Get(fields, "monthCode").
+ auto month_code = fields.get(vm.names.monthCode);
+ if (vm.exception())
+ return {};
+
+ // d. If monthCode is not undefined, then
+ if (!month_code.is_undefined()) {
+ // i. Perform ! CreateDataPropertyOrThrow(merged, "monthCode", monthCode).
+ merged->create_data_property_or_throw(vm.names.monthCode, month_code);
+ }
+ }
+
+ // 7. Return merged.
+ return merged;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
index 8e6c4d1d53..f6823de4c2 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h
@@ -69,5 +69,6 @@ i32 iso_year(Object& temporal_object);
u8 iso_month(Object& temporal_object);
String iso_month_code(Object& temporal_object);
u8 iso_day(Object& temporal_object);
+Object* default_merge_fields(GlobalObject&, Object& fields, Object& additional_fields);
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
index 352623e330..a93692335e 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
@@ -50,6 +50,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.monthsInYear, months_in_year, 1, attr);
define_native_function(vm.names.inLeapYear, in_leap_year, 1, attr);
define_native_function(vm.names.fields, fields, 1, attr);
+ define_native_function(vm.names.mergeFields, merge_fields, 2, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
}
@@ -507,6 +508,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::fields)
return Array::create_from(global_object, field_names);
}
+// 12.4.22 Temporal.Calendar.prototype.mergeFields ( fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.mergefields
+// NOTE: This is the minimum mergeFields implementation for engines without ECMA-402.
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::merge_fields)
+{
+ // 1. Let calendar be the this value.
+ // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
+ auto* calendar = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Assert: calendar.[[Identifier]] is "iso8601".
+ VERIFY(calendar->identifier() == "iso8601"sv);
+
+ // 4. Set fields to ? ToObject(fields).
+ auto* fields = vm.argument(0).to_object(global_object);
+ if (vm.exception())
+ return {};
+
+ // 5. Set additionalFields to ? ToObject(additionalFields).
+ auto* additional_fields = vm.argument(1).to_object(global_object);
+ if (vm.exception())
+ return {};
+
+ // 6. Return ? DefaultMergeFields(fields, additionalFields).
+ return default_merge_fields(global_object, *fields, *additional_fields);
+}
+
// 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
index b044cea2c6..6a0090b66e 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
@@ -36,6 +36,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(months_in_year);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year);
JS_DECLARE_NATIVE_FUNCTION(fields);
+ JS_DECLARE_NATIVE_FUNCTION(merge_fields);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
};