1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibJS: Implement Temporal.PlainTime.prototype.nanosecond

This commit is contained in:
Linus Groh 2021-07-28 19:07:29 +01:00
parent 65b90e93ad
commit d3bed13f4b
4 changed files with 30 additions and 0 deletions

View file

@ -275,6 +275,7 @@ namespace JS {
P(monthsInYear) \
P(multiline) \
P(name) \
P(nanosecond) \
P(nanoseconds) \
P(negated) \
P(next) \

View file

@ -32,6 +32,7 @@ void PlainTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.second, second_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.millisecond, millisecond_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.microsecond, microsecond_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.valueOf, value_of, 0, attr);
@ -128,6 +129,19 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::microsecond_getter)
return Value(temporal_time->iso_microsecond());
}
// 4.3.9 get Temporal.PlainTime.prototype.nanosecond, https://tc39.es/proposal-temporal/#sec-get-temporal.plaintime.prototype.nanosecond
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::nanosecond_getter)
{
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
auto* temporal_time = typed_this(global_object);
if (vm.exception())
return {};
// 3. Return 𝔽(temporalTime.[[ISONanosecond]]).
return Value(temporal_time->iso_nanosecond());
}
// 4.3.23 Temporal.PlainTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::value_of)
{

View file

@ -25,6 +25,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(second_getter);
JS_DECLARE_NATIVE_FUNCTION(millisecond_getter);
JS_DECLARE_NATIVE_FUNCTION(microsecond_getter);
JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter);
JS_DECLARE_NATIVE_FUNCTION(value_of);
};

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime(0, 0, 0, 0, 0, 12);
expect(plainTime.nanosecond).toBe(12);
});
});
test("errors", () => {
test("this value must be a Temporal.PlainTime object", () => {
expect(() => {
Reflect.get(Temporal.PlainTime.prototype, "nanosecond", "foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainTime");
});
});