summaryrefslogtreecommitdiff
path: root/js/timers.ts
diff options
context:
space:
mode:
author迷渡 <justjavac@gmail.com>2019-06-13 23:08:27 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-06-13 08:08:27 -0700
commit42d1024cd98811a4ce9eaaa73c84970d271628a6 (patch)
tree8df574ac158a29a5d55df9df75451573738bcd7f /js/timers.ts
parentbca5cc5041172e22ad1851c8510d6521bf70ec22 (diff)
forbidden to set `this` for setTimeout (#2511)
Diffstat (limited to 'js/timers.ts')
-rw-r--r--js/timers.ts10
1 files changed, 10 insertions, 0 deletions
diff --git a/js/timers.ts b/js/timers.ts
index 1e8bb428d..ba9a653a8 100644
--- a/js/timers.ts
+++ b/js/timers.ts
@@ -181,6 +181,12 @@ function fireTimers(): void {
export type Args = unknown[];
+function checkThis(thisArg: unknown): void {
+ if (thisArg !== null && thisArg !== undefined && thisArg !== window) {
+ throw new TypeError("Illegal invocation");
+ }
+}
+
function setTimer(
cb: (...args: Args) => void,
delay: number,
@@ -226,6 +232,8 @@ export function setTimeout(
delay: number,
...args: Args
): number {
+ // @ts-ignore
+ checkThis(this);
return setTimer(cb, delay, args, false);
}
@@ -235,6 +243,8 @@ export function setInterval(
delay: number,
...args: Args
): number {
+ // @ts-ignore
+ checkThis(this);
return setTimer(cb, delay, args, true);
}