Title: [286149] trunk
Revision
286149
Author
[email protected]
Date
2021-11-24 07:45:40 -0800 (Wed, 24 Nov 2021)

Log Message

[JSC] Implement Date.prototype.toTemporalInstant()
https://bugs.webkit.org/show_bug.cgi?id=232075

Patch by Aditi Singh <[email protected]> on 2021-11-24
Reviewed by Yusuke Suzuki.

JSTests:

* stress/date-to-temporal-instant.js: Added.
(shouldBe):
(shouldThrow):

Source/_javascript_Core:

* runtime/CommonIdentifiers.h:
* runtime/DatePrototype.cpp:
(JSC::DatePrototype::finishCreation):
(JSC::JSC_DEFINE_HOST_FUNCTION):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (286148 => 286149)


--- trunk/JSTests/ChangeLog	2021-11-24 15:04:31 UTC (rev 286148)
+++ trunk/JSTests/ChangeLog	2021-11-24 15:45:40 UTC (rev 286149)
@@ -1,3 +1,14 @@
+2021-11-24  Aditi Singh  <[email protected]>
+
+        [JSC] Implement Date.prototype.toTemporalInstant()
+        https://bugs.webkit.org/show_bug.cgi?id=232075
+
+        Reviewed by Yusuke Suzuki.
+
+        * stress/date-to-temporal-instant.js: Added.
+        (shouldBe):
+        (shouldThrow):
+
 2021-11-19  Asumu Takikawa  <[email protected]>
 
         Fix WebAssembly memory.fill out of bounds error message

Added: trunk/JSTests/stress/date-to-temporal-instant.js (0 => 286149)


--- trunk/JSTests/stress/date-to-temporal-instant.js	                        (rev 0)
+++ trunk/JSTests/stress/date-to-temporal-instant.js	2021-11-24 15:45:40 UTC (rev 286149)
@@ -0,0 +1,100 @@
+//@ requireOptions("--useTemporal=1")
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error(`expected ${expected} but got ${actual}`);
+}
+
+function shouldThrow(func, errorType, message) {
+    let error;
+    try {
+        func();
+    } catch (e) {
+        error = e;
+    }
+
+    if (!(error instanceof errorType))
+        throw new Error(`Expected ${errorType.name}!`);
+    if (message !== undefined) {
+        if (Object.prototype.toString.call(message) === '[object RegExp]') {
+            if (!message.test(String(error)))
+                throw new Error(`expected '${String(error)}' to match ${message}!`);
+        } else {
+            shouldBe(String(error), message);
+        }
+    }
+}
+
+
+// Invalid Date
+
+{
+    const date = new Date(NaN);
+    shouldThrow(() => {date.toTemporalInstant()}, RangeError)
+}
+
+// Epoch 
+{
+    const epochDateInstant = new Date(0).toTemporalInstant();
+    const epochInstant = new Temporal.Instant(0n);
+    shouldBe(epochDateInstant.toString(), epochInstant.toString());
+    shouldBe(epochDateInstant.epochNanoseconds, epochInstant.epochNanoseconds);
+    
+    shouldBe(epochDateInstant.epochSeconds, 0);
+    shouldBe(epochDateInstant.epochMilliseconds, 0);
+    shouldBe(epochDateInstant.epochMicroseconds, 0n);
+    shouldBe(epochDateInstant.epochNanoseconds, 0n);
+
+}
+
+// Positive value: 10^18
+{
+    const dateToInstant = new Date(1_000_000_000_000).toTemporalInstant();
+    const temporalInstant = new Temporal.Instant(1_000_000_000_000_000_000n);
+    shouldBe(dateToInstant.toString(), temporalInstant.toString());
+    shouldBe(dateToInstant.epochNanoseconds, temporalInstant.epochNanoseconds);
+    
+    shouldBe(dateToInstant.epochSeconds, 1_000_000_000);
+    shouldBe(dateToInstant.epochMilliseconds, 1_000_000_000_000);
+    shouldBe(dateToInstant.epochMicroseconds, 1_000_000_000_000_000n);
+    shouldBe(dateToInstant.epochNanoseconds, 1_000_000_000_000_000_000n);
+}
+
+// Negative value: -10^18
+{
+    const dateToInstant = new Date(-1_000_000_000_000).toTemporalInstant();
+    const temporalInstant = new Temporal.Instant(-1_000_000_000_000_000_000n);
+    shouldBe(dateToInstant.toString(), temporalInstant.toString());
+    shouldBe(dateToInstant.epochNanoseconds, temporalInstant.epochNanoseconds);
+
+    shouldBe(dateToInstant.epochSeconds, -1_000_000_000);
+    shouldBe(dateToInstant.epochMilliseconds, -1_000_000_000_000);
+    shouldBe(dateToInstant.epochMicroseconds, -1_000_000_000_000_000n);
+    shouldBe(dateToInstant.epochNanoseconds, -1_000_000_000_000_000_000n);
+}
+
+// Max instant
+{
+    const dateToInstant = new Date(86400_0000_0000_000).toTemporalInstant();
+    const temporalInstant = new Temporal.Instant(86400_0000_0000_000_000_000n);
+    shouldBe(dateToInstant.toString(), temporalInstant.toString());
+    shouldBe(dateToInstant.epochNanoseconds, temporalInstant.epochNanoseconds);
+    
+    shouldBe(dateToInstant.epochSeconds, 86400_0000_0000);
+    shouldBe(dateToInstant.epochMilliseconds, 86400_0000_0000_000);
+    shouldBe(dateToInstant.epochMicroseconds, 86400_0000_0000_000_000n);
+    shouldBe(dateToInstant.epochNanoseconds, 86400_0000_0000_000_000_000n);
+}
+
+//Min instant
+{
+    const dateToInstant = new Date(-86400_0000_0000_000).toTemporalInstant();
+    const temporalInstant = new Temporal.Instant(-86400_0000_0000_000_000_000n);
+    shouldBe(dateToInstant.toString(), temporalInstant.toString());
+    shouldBe(dateToInstant.epochNanoseconds, temporalInstant.epochNanoseconds);
+    
+    shouldBe(dateToInstant.epochSeconds, -86400_0000_0000);
+    shouldBe(dateToInstant.epochMilliseconds, -86400_0000_0000_000);
+    shouldBe(dateToInstant.epochMicroseconds, -86400_0000_0000_000_000n);
+    shouldBe(dateToInstant.epochNanoseconds, -86400_0000_0000_000_000_000n);
+}
\ No newline at end of file

Modified: trunk/Source/_javascript_Core/ChangeLog (286148 => 286149)


--- trunk/Source/_javascript_Core/ChangeLog	2021-11-24 15:04:31 UTC (rev 286148)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-11-24 15:45:40 UTC (rev 286149)
@@ -1,3 +1,15 @@
+2021-11-24  Aditi Singh  <[email protected]>
+
+        [JSC] Implement Date.prototype.toTemporalInstant()
+        https://bugs.webkit.org/show_bug.cgi?id=232075
+
+        Reviewed by Yusuke Suzuki.
+
+        * runtime/CommonIdentifiers.h:
+        * runtime/DatePrototype.cpp:
+        (JSC::DatePrototype::finishCreation):
+        (JSC::JSC_DEFINE_HOST_FUNCTION):
+
 2021-11-23  Don Olmstead  <[email protected]>
 
         Non-unified build fixes, mid November 2021 edition

Modified: trunk/Source/_javascript_Core/runtime/CommonIdentifiers.h (286148 => 286149)


--- trunk/Source/_javascript_Core/runtime/CommonIdentifiers.h	2021-11-24 15:04:31 UTC (rev 286148)
+++ trunk/Source/_javascript_Core/runtime/CommonIdentifiers.h	2021-11-24 15:45:40 UTC (rev 286149)
@@ -251,6 +251,7 @@
     macro(toLocaleString) \
     macro(toPrecision) \
     macro(toString) \
+    macro(toTemporalInstant) \
     macro(trailingZeroDisplay) \
     macro(type) \
     macro(uid) \

Modified: trunk/Source/_javascript_Core/runtime/DatePrototype.cpp (286148 => 286149)


--- trunk/Source/_javascript_Core/runtime/DatePrototype.cpp	2021-11-24 15:04:31 UTC (rev 286148)
+++ trunk/Source/_javascript_Core/runtime/DatePrototype.cpp	2021-11-24 15:45:40 UTC (rev 286149)
@@ -34,6 +34,7 @@
 #include "JSGlobalObject.h"
 #include "JSObject.h"
 #include "JSString.h"
+#include "TemporalInstant.h"
 #include <wtf/Assertions.h>
 
 namespace JSC {
@@ -79,6 +80,7 @@
 static JSC_DECLARE_HOST_FUNCTION(dateProtoFuncToUTCString);
 static JSC_DECLARE_HOST_FUNCTION(dateProtoFuncToISOString);
 static JSC_DECLARE_HOST_FUNCTION(dateProtoFuncToJSON);
+static JSC_DECLARE_HOST_FUNCTION(dateProtoFuncToTemporalInstant);
 
 }
 
@@ -282,6 +284,11 @@
     JSFunction* toPrimitiveFunction = JSFunction::create(vm, globalObject, 1, "[Symbol.toPrimitive]"_s, dateProtoFuncToPrimitiveSymbol, NoIntrinsic);
     putDirectWithoutTransition(vm, vm.propertyNames->toPrimitiveSymbol, toPrimitiveFunction, PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
 
+    if (Options::useTemporal()) {
+        Identifier toTemporalInstantName = Identifier::fromString(vm, "toTemporalInstant"_s);
+        JSFunction* toTemporalInstantFunction = JSFunction::create(vm, globalObject, 0, toTemporalInstantName.string(), dateProtoFuncToTemporalInstant);
+        putDirectWithoutTransition(vm, toTemporalInstantName, toTemporalInstantFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
+    }
     // The constructor will be added later, after DateConstructor has been built.
 }
 
@@ -904,4 +911,22 @@
     return JSValue::encode(result);
 }
 
+JSC_DEFINE_HOST_FUNCTION(dateProtoFuncToTemporalInstant, (JSGlobalObject* globalObject, CallFrame* callFrame))
+{
+    VM& vm = globalObject->vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+    JSValue thisValue = callFrame->thisValue();
+    auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
+    if (UNLIKELY(!thisDateObj))
+        return throwVMTypeError(globalObject, scope);
+
+    double epochMilliseconds = thisDateObj->internalNumber();
+    if (!isInteger(epochMilliseconds)) 
+        return throwVMError(globalObject, scope, createRangeError(globalObject, "Invalid integer number of Epoch Millseconds"_s));
+
+    ASSERT(epochMilliseconds >= std::numeric_limits<int64_t>::min() && epochMilliseconds <= std::numeric_limits<int64_t>::max());
+    ISO8601::ExactTime exactTime = ISO8601::ExactTime::fromEpochMilliseconds(epochMilliseconds);
+    return JSValue::encode(TemporalInstant::create(vm, globalObject->instantStructure(), exactTime));
+}
+
 } // namespace JSC
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to