LibJS: Implement Temporal.Duration.prototype.sign/blank

This commit is contained in:
Timothy Flynn 2024-11-18 12:31:28 -05:00 committed by Tim Flynn
parent 5fe0d3352d
commit dfaa3bf649
Notes: github-actions[bot] 2024-11-21 00:06:14 +00:00
4 changed files with 66 additions and 0 deletions

View file

@ -31,6 +31,9 @@ void DurationPrototype::initialize(Realm& realm)
define_native_accessor(realm, vm.names.unit, unit##_getter, {}, Attribute::Configurable);
JS_ENUMERATE_DURATION_UNITS
#undef __JS_ENUMERATE
define_native_accessor(realm, vm.names.sign, sign_getter, {}, Attribute::Configurable);
define_native_accessor(realm, vm.names.blank, blank_getter, {}, Attribute::Configurable);
}
// 7.3.3 get Temporal.Duration.prototype.years, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.years
@ -56,4 +59,30 @@ void DurationPrototype::initialize(Realm& realm)
JS_ENUMERATE_DURATION_UNITS
#undef __JS_ENUMERATE
// 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 = TRY(typed_this_object(vm));
// 3. Return 𝔽(DurationSign(duration)).
return duration_sign(duration);
}
// 7.3.14 get Temporal.Duration.prototype.blank, https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.blank
JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::blank_getter)
{
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
auto duration = TRY(typed_this_object(vm));
// 3. If DurationSign(duration) = 0, return true.
if (duration_sign(duration) == 0)
return true;
// 4. Return false.
return false;
}
}

View file

@ -27,6 +27,9 @@ private:
JS_DECLARE_NATIVE_FUNCTION(unit##_getter);
JS_ENUMERATE_DURATION_UNITS
#undef __JS_ENUMERATE
JS_DECLARE_NATIVE_FUNCTION(sign_getter);
JS_DECLARE_NATIVE_FUNCTION(blank_getter);
};
}

View file

@ -0,0 +1,17 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const nonBlankDuration = new Temporal.Duration(123);
expect(nonBlankDuration.blank).toBeFalse();
const blankDuration = new Temporal.Duration(0);
expect(blankDuration.blank).toBeTrue();
});
});
describe("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Reflect.get(Temporal.Duration.prototype, "blank", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
});
});

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);
});
});
describe("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Reflect.get(Temporal.Duration.prototype, "sign", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.Duration");
});
});