mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
LibJS: Implement Temporal.Instant.prototype.add/subtract/equals
This commit is contained in:
parent
615ad70030
commit
1d67f28e72
Notes:
github-actions[bot]
2024-11-25 12:34:16 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/1d67f28e72d Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2557 Reviewed-by: https://github.com/shannonbooth ✅
7 changed files with 226 additions and 0 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <LibJS/Runtime/BigInt.h>
|
||||
#include <LibJS/Runtime/Date.h>
|
||||
#include <LibJS/Runtime/Realm.h>
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/Temporal/Instant.h>
|
||||
#include <LibJS/Runtime/Temporal/InstantConstructor.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDateTime.h>
|
||||
|
@ -164,6 +165,20 @@ i8 compare_epoch_nanoseconds(Crypto::SignedBigInteger const& epoch_nanoseconds_o
|
|||
return 0;
|
||||
}
|
||||
|
||||
// 8.5.5 AddInstant ( epochNanoseconds, timeDuration ), https://tc39.es/proposal-temporal/#sec-temporal-addinstant
|
||||
ThrowCompletionOr<Crypto::SignedBigInteger> add_instant(VM& vm, Crypto::SignedBigInteger const& epoch_nanoseconds, TimeDuration const& time_duration)
|
||||
{
|
||||
// 1. Let result be AddTimeDurationToEpochNanoseconds(timeDuration, epochNanoseconds).
|
||||
auto result = add_time_duration_to_epoch_nanoseconds(time_duration, epoch_nanoseconds);
|
||||
|
||||
// 2. If IsValidEpochNanoseconds(result) is false, throw a RangeError exception.
|
||||
if (!is_valid_epoch_nanoseconds(result))
|
||||
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
|
||||
|
||||
// 3. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// 8.5.7 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant
|
||||
Crypto::SignedBigInteger round_temporal_instant(Crypto::SignedBigInteger const& nanoseconds, u64 increment, Unit unit, RoundingMode rounding_mode)
|
||||
{
|
||||
|
@ -213,4 +228,31 @@ String temporal_instant_to_string(Instant const& instant, Optional<StringView> t
|
|||
return MUST(String::formatted("{}{}", date_time_string, time_zone_string));
|
||||
}
|
||||
|
||||
// 8.5.10 AddDurationToInstant ( operation, instant, temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtoinstant
|
||||
ThrowCompletionOr<GC::Ref<Instant>> add_duration_to_instant(VM& vm, ArithmeticOperation operation, Instant const& instant, Value temporal_duration_like)
|
||||
{
|
||||
// 1. Let duration be ? ToTemporalDuration(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration(vm, temporal_duration_like));
|
||||
|
||||
// 2. If operation is SUBTRACT, set duration to CreateNegatedTemporalDuration(duration).
|
||||
if (operation == ArithmeticOperation::Subtract)
|
||||
duration = create_negated_temporal_duration(vm, duration);
|
||||
|
||||
// 3. Let largestUnit be DefaultTemporalLargestUnit(duration).
|
||||
auto largest_unit = default_temporal_largest_unit(duration);
|
||||
|
||||
// 4. If TemporalUnitCategory(largestUnit) is DATE, throw a RangeError exception.
|
||||
if (temporal_unit_category(largest_unit) == UnitCategory::Date)
|
||||
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidLargestUnit, temporal_unit_to_string(largest_unit));
|
||||
|
||||
// 5. Let internalDuration be ToInternalDurationRecordWith24HourDays(duration).
|
||||
auto internal_duration = to_internal_duration_record_with_24_hour_days(vm, duration);
|
||||
|
||||
// 6. Let ns be ? AddInstant(instant.[[EpochNanoseconds]], internalDuration.[[Time]]).
|
||||
auto nanoseconds = TRY(add_instant(vm, instant.epoch_nanoseconds()->big_integer(), internal_duration.time));
|
||||
|
||||
// 7. Return ! CreateTemporalInstant(ns).
|
||||
return MUST(create_temporal_instant(vm, BigInt::create(vm, move(nanoseconds))));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,7 +60,9 @@ bool is_valid_epoch_nanoseconds(Crypto::SignedBigInteger const& epoch_nanosecond
|
|||
ThrowCompletionOr<GC::Ref<Instant>> create_temporal_instant(VM&, BigInt const& epoch_nanoseconds, GC::Ptr<FunctionObject> new_target = {});
|
||||
ThrowCompletionOr<GC::Ref<Instant>> to_temporal_instant(VM&, Value item);
|
||||
i8 compare_epoch_nanoseconds(Crypto::SignedBigInteger const& epoch_nanoseconds_one, Crypto::SignedBigInteger const& epoch_nanoseconds_two);
|
||||
ThrowCompletionOr<Crypto::SignedBigInteger> add_instant(VM&, Crypto::SignedBigInteger const& epoch_nanoseconds, TimeDuration const&);
|
||||
Crypto::SignedBigInteger round_temporal_instant(Crypto::SignedBigInteger const& nanoseconds, u64 increment, Unit, RoundingMode);
|
||||
String temporal_instant_to_string(Instant const&, Optional<StringView> time_zone, SecondsStringPrecision::Precision);
|
||||
ThrowCompletionOr<GC::Ref<Instant>> add_duration_to_instant(VM&, ArithmeticOperation, Instant const&, Value temporal_duration_like);
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,9 @@ void InstantPrototype::initialize(Realm& realm)
|
|||
define_native_accessor(realm, vm.names.epochNanoseconds, epoch_nanoseconds_getter, {}, Attribute::Configurable);
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(realm, vm.names.add, add, 1, attr);
|
||||
define_native_function(realm, vm.names.subtract, subtract, 1, attr);
|
||||
define_native_function(realm, vm.names.equals, equals, 1, attr);
|
||||
define_native_function(realm, vm.names.toString, to_string, 0, attr);
|
||||
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
|
||||
|
@ -66,6 +69,50 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_nanoseconds_getter)
|
|||
return instant->epoch_nanoseconds();
|
||||
}
|
||||
|
||||
// 8.3.5 Temporal.Instant.prototype.add ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.add
|
||||
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::add)
|
||||
{
|
||||
auto temporal_duration_like = vm.argument(0);
|
||||
|
||||
// 1. Let instant be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
|
||||
auto instant = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? AddDurationToInstant(ADD, instant, temporalDurationLike).
|
||||
return TRY(add_duration_to_instant(vm, ArithmeticOperation::Add, instant, temporal_duration_like));
|
||||
}
|
||||
|
||||
// 8.3.6 Temporal.Instant.prototype.subtract ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.subtract
|
||||
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::subtract)
|
||||
{
|
||||
auto temporal_duration_like = vm.argument(0);
|
||||
|
||||
// 1. Let instant be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
|
||||
auto instant = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ? AddDurationToInstant(SUBTRACT, instant, temporalDurationLike).
|
||||
return TRY(add_duration_to_instant(vm, ArithmeticOperation::Subtract, instant, temporal_duration_like));
|
||||
}
|
||||
|
||||
// 8.3.10 Temporal.Instant.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.equals
|
||||
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::equals)
|
||||
{
|
||||
// 1. Let instant be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
|
||||
auto instant = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Set other to ? ToTemporalInstant(other).
|
||||
auto other = TRY(to_temporal_instant(vm, vm.argument(0)));
|
||||
|
||||
// 4. If instant.[[EpochNanoseconds]] ≠ other.[[EpochNanoseconds]], return false.
|
||||
if (instant->epoch_nanoseconds()->big_integer() != other->epoch_nanoseconds()->big_integer())
|
||||
return false;
|
||||
|
||||
// 5. Return true.
|
||||
return true;
|
||||
}
|
||||
|
||||
// 8.3.11 Temporal.Instant.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tostring
|
||||
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_string)
|
||||
{
|
||||
|
|
|
@ -25,6 +25,9 @@ private:
|
|||
|
||||
JS_DECLARE_NATIVE_FUNCTION(epoch_milliseconds_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(epoch_nanoseconds_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.prototype.add).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 1, 2, 3, 4, 5, 6);
|
||||
expect(instant.add(duration).epochNanoseconds).toBe(1625618644004005006n);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.add.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, positive", () => {
|
||||
const instant = new Temporal.Instant(8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
|
||||
expect(() => {
|
||||
instant.add(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, negative", () => {
|
||||
const instant = new Temporal.Instant(-8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
|
||||
expect(() => {
|
||||
instant.add(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("disallowed fields", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
for (const [args, property] of [
|
||||
[[123, 0, 0, 0], "year"],
|
||||
[[0, 123, 0, 0], "month"],
|
||||
[[0, 0, 123, 0], "week"],
|
||||
[[0, 0, 0, 123], "day"],
|
||||
]) {
|
||||
const duration = new Temporal.Duration(...args);
|
||||
expect(() => {
|
||||
instant.add(duration);
|
||||
}).toThrowWithMessage(RangeError, `Largest unit must not be ${property}`);
|
||||
}
|
||||
});
|
||||
});
|
|
@ -0,0 +1,20 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.prototype.equals).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant1 = new Temporal.Instant(111n);
|
||||
expect(instant1.equals(instant1));
|
||||
const instant2 = new Temporal.Instant(999n);
|
||||
expect(!instant1.equals(instant2));
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.equals.call("foo", 1, 2);
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,56 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.prototype.subtract).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 1, 2, 3, 4, 5, 6);
|
||||
expect(instant.subtract(duration).epochNanoseconds).toBe(1625611197995994994n);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("this value must be a Temporal.Instant object", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.prototype.subtract.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Instant");
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, positive", () => {
|
||||
const instant = new Temporal.Instant(8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
|
||||
expect(() => {
|
||||
instant.subtract(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("invalid nanoseconds value, negative", () => {
|
||||
const instant = new Temporal.Instant(-8_640_000_000_000_000_000_000n);
|
||||
const duration = new Temporal.Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
|
||||
expect(() => {
|
||||
instant.subtract(duration);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("disallowed fields", () => {
|
||||
const instant = new Temporal.Instant(1625614921000000000n);
|
||||
for (const [args, property] of [
|
||||
[[123, 0, 0, 0], "year"],
|
||||
[[0, 123, 0, 0], "month"],
|
||||
[[0, 0, 123, 0], "week"],
|
||||
[[0, 0, 0, 123], "day"],
|
||||
]) {
|
||||
const duration = new Temporal.Duration(...args);
|
||||
expect(() => {
|
||||
instant.subtract(duration);
|
||||
}).toThrowWithMessage(RangeError, `Largest unit must not be ${property}`);
|
||||
}
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue