summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-07-23 15:01:40 +0300
committerLinus Groh <mail@linusgroh.de>2021-07-23 22:00:23 +0100
commit9fa8f19a0fc356d75b8a1b5b01bf0f0573fa94e0 (patch)
tree5a405d6c7ab153adee39d76c2c51cfce96b74a2f /Userland/Libraries
parent8123e957e372ce54898ffb3d8ce776c5164b8e53 (diff)
downloadserenity-9fa8f19a0fc356d75b8a1b5b01bf0f0573fa94e0.zip
LibJS: Implement Temporal.PlainDate.prototype.withCalendar
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp19
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.withCalendar.js13
4 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index fdda3c191a..79f4d389c1 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -382,6 +382,7 @@ namespace JS {
P(warn) \
P(weeks) \
P(with) \
+ P(withCalendar) \
P(writable) \
P(year) \
P(years)
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
index b7bcace16e..d02492eefe 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp
@@ -29,6 +29,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
+ define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
}
@@ -59,6 +60,24 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::calendar_getter)
return Value(&temporal_date->calendar());
}
+// 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
+JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
+{
+ // 1. Let temporalDate be the this value.
+ // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
+ auto* temporal_date = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Let calendar be ? ToTemporalCalendar(calendar).
+ auto calendar = to_temporal_calendar(global_object, vm.argument(0));
+ if (vm.exception())
+ return {};
+
+ // 4. Return ? CreateTemporalDate(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], calendar).
+ return create_temporal_date(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), *calendar);
+}
+
// 3.3.25 Temporal.PlainDate.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.equals
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::equals)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
index 4182ae0af1..a94d9ec9d6 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h
@@ -21,6 +21,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
+ JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.withCalendar.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.withCalendar.js
new file mode 100644
index 0000000000..449e7089ea
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.withCalendar.js
@@ -0,0 +1,13 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.PlainDate.prototype.withCalendar).toHaveLength(1);
+ });
+
+ test("basic functionality", () => {
+ const calendar = { hello: "friends" };
+ const firstPlainDate = new Temporal.PlainDate(1, 1, 1);
+ expect(firstPlainDate.calendar).not.toBe(calendar);
+ const secondPlainDate = firstPlainDate.withCalendar(calendar);
+ expect(secondPlainDate.calendar).toBe(calendar);
+ });
+});