Title: [295428] trunk
Revision
295428
Author
keith_mil...@apple.com
Date
2022-06-09 12:05:16 -0700 (Thu, 09 Jun 2022)

Log Message

jsc's settimeout should properly handle a delay
https://bugs.webkit.org/show_bug.cgi?id=240467

Reviewed by Yusuke Suzuki.

This patch makes it so that we properly handle a timeout passed to the JSC CLI setTimeout API. Previously we would just run the callback on the next runloop tick regardless of the passed value.

* Source/_javascript_Core/jsc.cpp:
(JSC_DEFINE_HOST_FUNCTION):

Canonical link: https://commits.webkit.org/251434@main

Modified Paths

Added Paths

Diff

Added: trunk/JSTests/stress/setTimeout-with-delay.js (0 => 295428)


--- trunk/JSTests/stress/setTimeout-with-delay.js	                        (rev 0)
+++ trunk/JSTests/stress/setTimeout-with-delay.js	2022-06-09 19:05:16 UTC (rev 295428)
@@ -0,0 +1,7 @@
+let startTime = Date.now();
+let waitTime = 1000;
+
+setTimeout(() => {
+    if (startTime + waitTime > Date.now())
+        throw new Error();
+}, waitTime);
\ No newline at end of file

Modified: trunk/Source/_javascript_Core/jsc.cpp (295427 => 295428)


--- trunk/Source/_javascript_Core/jsc.cpp	2022-06-09 18:57:46 UTC (rev 295427)
+++ trunk/Source/_javascript_Core/jsc.cpp	2022-06-09 19:05:16 UTC (rev 295428)
@@ -2525,13 +2525,21 @@
     if (!callback)
         return throwVMTypeError(globalObject, scope, "First argument is not a JS function"_s);
 
-    // FIXME: We don't look at the timeout parameter because we don't have a schedule work later API.
     auto ticket = vm.deferredWorkTimer->addPendingWork(vm, callback, { });
-    vm.deferredWorkTimer->scheduleWorkSoon(ticket, [callback](DeferredWorkTimer::Ticket) {
-        JSGlobalObject* globalObject = callback->globalObject();
-        MarkedArgumentBuffer args;
-        call(globalObject, callback, jsUndefined(), args, "You shouldn't see this..."_s);
-    });
+    auto dispatch = [callback, ticket] {
+        callback->vm().deferredWorkTimer->scheduleWorkSoon(ticket, [callback](DeferredWorkTimer::Ticket) {
+            JSGlobalObject* globalObject = callback->globalObject();
+            MarkedArgumentBuffer args;
+            call(globalObject, callback, jsUndefined(), args, "You shouldn't see this..."_s);
+        });
+    };
+
+    JSValue timeout = callFrame->argument(1);
+    if (timeout.isNumber() && timeout.asNumber())
+        RunLoop::current().dispatchAfter(Seconds::fromMilliseconds(timeout.asNumber()), WTFMove(dispatch));
+    else
+        dispatch();
+
     return JSValue::encode(jsUndefined());
 }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to