Skip to content
Permalink
Browse files
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/JavaScriptCore/jsc.cpp:
(JSC_DEFINE_HOST_FUNCTION):

Canonical link: https://commits.webkit.org/251434@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295428 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
kmiller68 committed Jun 9, 2022
1 parent 4e536a0 commit ff6381fe952bbea67403e65ea3322b4803d0ea3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
@@ -0,0 +1,7 @@
let startTime = Date.now();
let waitTime = 1000;

setTimeout(() => {
if (startTime + waitTime > Date.now())
throw new Error();
}, waitTime);
@@ -2525,13 +2525,21 @@ JSC_DEFINE_HOST_FUNCTION(functionSetTimeout, (JSGlobalObject* globalObject, Call
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());
}

0 comments on commit ff6381f

Please sign in to comment.