LibJS: Implement Date.prototype.toTemporalInstant

This commit is contained in:
Timothy Flynn 2024-11-26 10:51:45 -05:00 committed by Tim Flynn
commit 511029807a
Notes: github-actions[bot] 2024-11-26 21:57:38 +00:00
4 changed files with 39 additions and 0 deletions

View file

@ -538,6 +538,7 @@ namespace JS {
P(toSpliced) \
P(toString) \
P(total) \
P(toTemporalInstant) \
P(toTimeString) \
P(toUpperCase) \
P(toUTCString) \

View file

@ -22,6 +22,7 @@
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Temporal/TimeZone.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
@ -86,6 +87,8 @@ void DatePrototype::initialize(Realm& realm)
define_native_function(realm, vm.names.toTimeString, to_time_string, 0, attr);
define_native_function(realm, vm.names.toUTCString, to_utc_string, 0, attr);
define_native_function(realm, vm.names.toTemporalInstant, to_temporal_instant, 0, attr);
define_native_function(realm, vm.names.getYear, get_year, 0, attr);
define_native_function(realm, vm.names.setYear, set_year, 1, attr);
@ -1221,6 +1224,24 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::symbol_to_primitive)
return TRY(this_value.as_object().ordinary_to_primitive(try_first));
}
// 14.8.1 Date.prototype.toTemporalInstant ( ), https://tc39.es/proposal-temporal/#sec-date.prototype.totemporalinstant
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_temporal_instant)
{
// 1. Let dateObject be the this value.
// 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
auto date_object = TRY(typed_this_value(vm));
// 3. Let t be dateObject.[[DateValue]].
auto time = date_object->date_value();
// 4. Let ns be ? NumberToBigInt(t) × (10**6).
auto nanoseconds = TRY(number_to_bigint(vm, Value { time }));
nanoseconds = BigInt::create(vm, nanoseconds->big_integer().multiplied_by(Temporal::NANOSECONDS_PER_MILLISECOND));
// 5. Return ! CreateTemporalInstant(ns).
return MUST(Temporal::create_temporal_instant(vm, nanoseconds));
}
// B.2.4.1 Date.prototype.getYear ( ), https://tc39.es/ecma262/#sec-date.prototype.getyear
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year)
{

View file

@ -65,6 +65,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_time_string);
JS_DECLARE_NATIVE_FUNCTION(to_utc_string);
JS_DECLARE_NATIVE_FUNCTION(to_temporal_instant);
JS_DECLARE_NATIVE_FUNCTION(get_year);
JS_DECLARE_NATIVE_FUNCTION(set_year);
JS_DECLARE_NATIVE_FUNCTION(to_gmt_string);

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const date = new Date("2021-07-09T01:36:00Z");
const instant = date.toTemporalInstant();
expect(instant.epochMilliseconds).toBe(1625794560000);
});
});
describe("errors", () => {
test("this value must be a Date object", () => {
expect(() => {
Date.prototype.toTemporalInstant.call(123);
}).toThrowWithMessage(TypeError, "Not an object of type Date");
});
});