1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +00:00

LibJS: Add Temporal.Instant.from()

This commit is contained in:
Idan Horowitz 2021-07-11 21:19:19 +03:00 committed by Linus Groh
parent 33cf6274e8
commit 84403ab423
3 changed files with 34 additions and 0 deletions

View file

@ -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)
{

View file

@ -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);

View file

@ -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
);
});
});