summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-07-11 21:19:19 +0300
committerLinus Groh <mail@linusgroh.de>2021-07-12 19:05:17 +0100
commit84403ab42324fbc79b6e255ce387fc03924e0a59 (patch)
treed1aa49e284c306a6f9f3819d56e9e036a04d06f8 /Userland
parent33cf6274e87ac7fcc4eadd07cc453694b98bd8e8 (diff)
downloadserenity-84403ab42324fbc79b6e255ce387fc03924e0a59.zip
LibJS: Add Temporal.Instant.from()
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp16
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js17
3 files changed, 34 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp
index f59dabdf1a..fcc21a45d4 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.cpp
@@ -27,6 +27,7 @@ void InstantConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.prototype, global_object.temporal_instant_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
+ define_native_function(vm.names.from, from, 1, attr);
define_native_function(vm.names.fromEpochSeconds, from_epoch_seconds, 1, attr);
define_native_function(vm.names.fromEpochMilliseconds, from_epoch_milliseconds, 1, attr);
define_native_function(vm.names.fromEpochMicroseconds, from_epoch_microseconds, 1, attr);
@@ -68,6 +69,21 @@ Value InstantConstructor::construct(FunctionObject& new_target)
return create_temporal_instant(global_object, *epoch_nanoseconds, &new_target);
}
+// 8.2.2 Temporal.Instant.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.from
+JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from)
+{
+ auto item = vm.argument(0);
+
+ // 1. If Type(item) is Object and item has an [[InitializedTemporalInstant]] internal slot, then
+ if (item.is_object() && is<Instant>(item.as_object())) {
+ // a. Return ? CreateTemporalInstant(item.[[Nanoseconds]]).
+ return create_temporal_instant(global_object, *js_bigint(vm.heap(), static_cast<Instant&>(item.as_object()).nanoseconds().big_integer()));
+ }
+
+ // 2. Return ? ToTemporalInstant(item).
+ return to_temporal_instant(global_object, item);
+}
+
// 8.2.3 Temporal.Instant.fromEpochSeconds ( epochSeconds ), https://tc39.es/proposal-temporal/#sec-temporal.instant.fromepochseconds
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h
index 1a7a700234..310e5c7c8e 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/InstantConstructor.h
@@ -24,6 +24,7 @@ public:
private:
virtual bool has_constructor() const override { return true; }
+ JS_DECLARE_NATIVE_FUNCTION(from);
JS_DECLARE_NATIVE_FUNCTION(from_epoch_seconds);
JS_DECLARE_NATIVE_FUNCTION(from_epoch_milliseconds);
JS_DECLARE_NATIVE_FUNCTION(from_epoch_microseconds);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js
new file mode 100644
index 0000000000..ea13fd2fa0
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js
@@ -0,0 +1,17 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.Instant.from).toHaveLength(1);
+ });
+
+ test("Instant instance argument", () => {
+ const instant = new Temporal.Instant(123n);
+ expect(Temporal.Instant.from(instant).epochNanoseconds).toBe(123n);
+ });
+
+ // Un-skip once ParseISODateTime & ParseTemporalTimeZoneString are implemented
+ test.skip("Instant string argument", () => {
+ expect(Temporal.Instant.from("1975-02-02T14:25:36.123456789Z").epochNanoseconds).toBe(
+ 160583136123456789n
+ );
+ });
+});