mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 05:39:11 +00:00
LibJS: Implement Date.prototype.toTemporalInstant
This commit is contained in:
parent
8e8c133db5
commit
511029807a
Notes:
github-actions[bot]
2024-11-26 21:57:38 +00:00
Author: https://github.com/trflynn89
Commit: 511029807a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2594
4 changed files with 39 additions and 0 deletions
|
@ -538,6 +538,7 @@ namespace JS {
|
||||||
P(toSpliced) \
|
P(toSpliced) \
|
||||||
P(toString) \
|
P(toString) \
|
||||||
P(total) \
|
P(total) \
|
||||||
|
P(toTemporalInstant) \
|
||||||
P(toTimeString) \
|
P(toTimeString) \
|
||||||
P(toUpperCase) \
|
P(toUpperCase) \
|
||||||
P(toUTCString) \
|
P(toUTCString) \
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||||
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
||||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||||
|
#include <LibJS/Runtime/Temporal/Instant.h>
|
||||||
#include <LibJS/Runtime/Temporal/TimeZone.h>
|
#include <LibJS/Runtime/Temporal/TimeZone.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
#include <LibJS/Runtime/ValueInlines.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.toTimeString, to_time_string, 0, attr);
|
||||||
define_native_function(realm, vm.names.toUTCString, to_utc_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.getYear, get_year, 0, attr);
|
||||||
define_native_function(realm, vm.names.setYear, set_year, 1, 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));
|
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
|
// B.2.4.1 Date.prototype.getYear ( ), https://tc39.es/ecma262/#sec-date.prototype.getyear
|
||||||
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year)
|
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year)
|
||||||
{
|
{
|
||||||
|
|
|
@ -65,6 +65,8 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_time_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_time_string);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_utc_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(get_year);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(set_year);
|
JS_DECLARE_NATIVE_FUNCTION(set_year);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(to_gmt_string);
|
JS_DECLARE_NATIVE_FUNCTION(to_gmt_string);
|
||||||
|
|
|
@ -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");
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue