summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-07-18 21:44:05 +0100
committerLinus Groh <mail@linusgroh.de>2021-07-19 00:34:28 +0100
commit5c7788587324dea861c9fcc1db2d1ec3048aeaa6 (patch)
tree328a014975c37f99a8c8564c8c23682cbb0cfd07 /Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp
parent7355c23e170b609ab1a3ac653f3428c4c6168c7b (diff)
downloadserenity-5c7788587324dea861c9fcc1db2d1ec3048aeaa6.zip
LibJS: Implement Temporal.Duration.from()
...with ParseTemporalDurationString currently TODO()'d.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp
index 41a03f5473..5f565987b7 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationConstructor.cpp
@@ -25,6 +25,9 @@ void DurationConstructor::initialize(GlobalObject& global_object)
// 7.2.1 Temporal.Duration.prototype, https://tc39.es/proposal-temporal/#sec-temporal-duration-prototype
define_direct_property(vm.names.prototype, global_object.temporal_duration_prototype(), 0);
+ u8 attr = Attribute::Writable | Attribute::Configurable;
+ define_native_function(vm.names.from, from, 1, attr);
+
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
}
@@ -99,4 +102,21 @@ Value DurationConstructor::construct(FunctionObject& new_target)
return create_temporal_duration(global_object, y, mo, w, d, h, m, s, ms, mis, ns, &new_target);
}
+// 7.2.2 Temporal.Duration.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.duration.from
+JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
+{
+ auto item = vm.argument(0);
+
+ // 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
+ if (item.is_object() && is<Duration>(item.as_object())) {
+ auto& duration = static_cast<Duration&>(item.as_object());
+
+ // a. Return ? CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]], item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]], item.[[Microseconds]], item.[[Nanoseconds]]).
+ return create_temporal_duration(global_object, duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds());
+ }
+
+ // 2. Return ? ToTemporalDuration(item).
+ return to_temporal_duration(global_object, item);
+}
+
}