ソースを参照

LibJS: Implement Temporal.PlainTime.prototype.microsecond

Linus Groh 4 年 前
コミット
65b90e93ad

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -262,6 +262,7 @@ namespace JS {
     P(map)                                   \
     P(max)                                   \
     P(message)                               \
+    P(microsecond)                           \
     P(microseconds)                          \
     P(millisecond)                           \
     P(milliseconds)                          \

+ 14 - 0
Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp

@@ -31,6 +31,7 @@ void PlainTimePrototype::initialize(GlobalObject& global_object)
     define_native_accessor(vm.names.minute, minute_getter, {}, Attribute::Configurable);
     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);
 
     u8 attr = Attribute::Writable | Attribute::Configurable;
     define_native_function(vm.names.valueOf, value_of, 0, attr);
@@ -114,6 +115,19 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::millisecond_getter)
     return Value(temporal_time->iso_millisecond());
 }
 
+// 4.3.8 get Temporal.PlainTime.prototype.microsecond, https://tc39.es/proposal-temporal/#sec-get-temporal.plaintime.prototype.microsecond
+JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::microsecond_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.[[ISOMicrosecond]]).
+    return Value(temporal_time->iso_microsecond());
+}
+
 // 4.3.23 Temporal.PlainTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.valueof
 JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::value_of)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h

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

+ 14 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.microsecond.js

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