LibJS: Implement Temporal.Duration.prototype.sign

This commit is contained in:
Linus Groh 2021-07-16 00:12:03 +01:00
parent be5254dcac
commit 3713164fa3
Notes: sideshowbarker 2024-07-18 08:57:34 +09:00
3 changed files with 32 additions and 0 deletions

View file

@ -35,6 +35,7 @@ void DurationPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.milliseconds, milliseconds_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.microseconds, microseconds_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.nanoseconds, nanoseconds_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.sign, sign_getter, {}, Attribute::Configurable);
}
static Duration* typed_this(GlobalObject& global_object)
@ -180,4 +181,17 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::nanoseconds_getter)
return Value(duration->nanoseconds());
}
// 7.3.13 get Temporal.Duration.prototype.sign, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.sign
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::sign_getter)
{
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
auto* duration = typed_this(global_object);
if (vm.exception())
return {};
// 3. Return ! DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
return Value(duration_sign(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds()));
}
}

View file

@ -29,6 +29,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(milliseconds_getter);
JS_DECLARE_NATIVE_FUNCTION(microseconds_getter);
JS_DECLARE_NATIVE_FUNCTION(nanoseconds_getter);
JS_DECLARE_NATIVE_FUNCTION(sign_getter);
};
}

View file

@ -0,0 +1,17 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const positiveDuration = new Temporal.Duration(123);
expect(positiveDuration.sign).toBe(1);
const negativeDuration = new Temporal.Duration(-123);
expect(negativeDuration.sign).toBe(-1);
});
});
test("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Reflect.get(Temporal.Duration.prototype, "sign", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
});
});