summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-07-15 23:54:58 +0100
committerLinus Groh <mail@linusgroh.de>2021-07-16 01:07:01 +0100
commitdb22f8605586fa65ce3f7b1d0de75dd6e0c729bb (patch)
treeb7be3bef6d87cd85563799c89b217d8badcbfcd7 /Userland
parentb81331a1106e66c956462a82189ee26fb954a90f (diff)
downloadserenity-db22f8605586fa65ce3f7b1d0de75dd6e0c729bb.zip
LibJS: Implement Temporal.Duration.prototype.milliseconds
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp14
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.milliseconds.js14
4 files changed, 30 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 47b98cac53..a341e61499 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -237,6 +237,7 @@ namespace JS {
P(map) \
P(max) \
P(message) \
+ P(milliseconds) \
P(min) \
P(minutes) \
P(months) \
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
index 73481b3ce0..e1fb08dc6d 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
@@ -32,6 +32,7 @@ void DurationPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.hours, hours_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.minutes, minutes_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.seconds, seconds_getter, {}, Attribute::Configurable);
+ define_native_accessor(vm.names.milliseconds, milliseconds_getter, {}, Attribute::Configurable);
}
static Duration* typed_this(GlobalObject& global_object)
@@ -138,4 +139,17 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::seconds_getter)
return Value(duration->seconds());
}
+// 7.3.10 get Temporal.Duration.prototype.milliseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.milliseconds
+JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::milliseconds_getter)
+{
+ // 1. Let duration be the this value.
+ // 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
+ auto* duration = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Return duration.[[Milliseconds]].
+ return Value(duration->milliseconds());
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
index eb90e5b5ce..cad8729ab6 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.h
@@ -26,6 +26,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(hours_getter);
JS_DECLARE_NATIVE_FUNCTION(minutes_getter);
JS_DECLARE_NATIVE_FUNCTION(seconds_getter);
+ JS_DECLARE_NATIVE_FUNCTION(milliseconds_getter);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.milliseconds.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.milliseconds.js
new file mode 100644
index 0000000000..01da80b3f1
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.prototype.milliseconds.js
@@ -0,0 +1,14 @@
+describe("correct behavior", () => {
+ test("basic functionality", () => {
+ const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 123);
+ expect(duration.milliseconds).toBe(123);
+ });
+});
+
+test("errors", () => {
+ test("this value must be a Temporal.Duration object", () => {
+ expect(() => {
+ Reflect.get(Temporal.Duration.prototype, "milliseconds", "foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
+ });
+});