Skip to content

Commit fa94dae

Browse files
committed
refactor: move clearTimeout argument checks to the JavaScript side
1 parent f14ae78 commit fa94dae

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

python/pythonmonkey/builtin_modules/timers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ function setTimeout(handler, delayMs = 0, ...args)
5353
*/
5454
function clearTimeout(timeoutId)
5555
{
56+
// silently does nothing when an invalid timeoutId (should be an int32 value) is passed in
57+
if (!Number.isInteger(timeoutId))
58+
return;
59+
5660
return cancelByTimeoutId(timeoutId);
5761
}
5862

src/internalBinding/timers.cc

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,14 @@ static bool enqueueWithDelay(JSContext *cx, unsigned argc, JS::Value *vp) {
3636
return true;
3737
}
3838

39-
// TODO (Tom Tang): move argument checks to the JavaScript side
4039
static bool cancelByTimeoutId(JSContext *cx, unsigned argc, JS::Value *vp) {
4140
using AsyncHandle = PyEventLoop::AsyncHandle;
4241
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
43-
JS::HandleValue timeoutIdArg = args.get(0);
42+
double timeoutID = args.get(0).toNumber();
4443

4544
args.rval().setUndefined();
4645

47-
// silently does nothing when an invalid timeoutID (should be an int32 value) is passed in
48-
if (!timeoutIdArg.isNumber()) {
49-
return true;
50-
}
51-
5246
// Retrieve the AsyncHandle by `timeoutID`
53-
double timeoutID = timeoutIdArg.toNumber();
5447
AsyncHandle *handle = AsyncHandle::fromId((uint32_t)timeoutID);
5548
if (!handle) return true; // does nothing on invalid timeoutID
5649

0 commit comments

Comments
 (0)