Skip to content

Commit

Permalink
LibJS: Implement Temporal.Duration.prototype.blank
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Jul 16, 2021
1 parent 3713164 commit a06b034
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace JS {
P(atanh) \
P(big) \
P(bind) \
P(blank) \
P(blink) \
P(bold) \
P(buffer) \
Expand Down
21 changes: 21 additions & 0 deletions Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void DurationPrototype::initialize(GlobalObject& global_object)
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);
define_native_accessor(vm.names.blank, blank_getter, {}, Attribute::Configurable);
}

static Duration* typed_this(GlobalObject& global_object)
Expand Down Expand Up @@ -194,4 +195,24 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::sign_getter)
return Value(duration_sign(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds()));
}

// 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 = typed_this(global_object);
if (vm.exception())
return {};

// 3. Let sign be ! DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
auto sign = duration_sign(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds());

// 4. If sign = 0, return true.
if (sign == 0)
return Value(true);

// 5. Return false.
return Value(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DurationPrototype final : public Object {
JS_DECLARE_NATIVE_FUNCTION(microseconds_getter);
JS_DECLARE_NATIVE_FUNCTION(nanoseconds_getter);
JS_DECLARE_NATIVE_FUNCTION(sign_getter);
JS_DECLARE_NATIVE_FUNCTION(blank_getter);
};

}
Original file line number Diff line number Diff line change
@@ -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();
});
});

test("errors", () => {
test("this value must be a Temporal.Duration object", () => {
expect(() => {
Reflect.get(Temporal.Duration.prototype, "blank", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Duration");
});
});

0 comments on commit a06b034

Please sign in to comment.