summaryrefslogtreecommitdiff
path: root/js/timers_test.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_test.ts
parentbca5cc5041172e22ad1851c8510d6521bf70ec22 (diff)
forbidden to set `this` for setTimeout (#2511)
Diffstat (limited to 'js/timers_test.ts')
-rw-r--r--js/timers_test.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/js/timers_test.ts b/js/timers_test.ts
index cbdc6eaba..88c5ea5ca 100644
--- a/js/timers_test.ts
+++ b/js/timers_test.ts
@@ -177,3 +177,57 @@ test(async function timeoutCallbackThis(): Promise<void> {
setTimeout(obj.foo, 1);
await promise;
});
+
+test(async function timeoutBindThis(): Promise<void> {
+ function noop(): void {}
+
+ const thisCheckPassed = [null, undefined, window, globalThis];
+
+ const thisCheckFailed = [
+ 0,
+ "",
+ true,
+ false,
+ {},
+ [],
+ "foo",
+ (): void => {},
+ Object.prototype
+ ];
+
+ thisCheckPassed.forEach(
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ (thisArg: any): void => {
+ let hasThrown = 0;
+ try {
+ setTimeout.call(thisArg, noop, 1);
+ hasThrown = 1;
+ } catch (err) {
+ if (err instanceof TypeError) {
+ hasThrown = 2;
+ } else {
+ hasThrown = 3;
+ }
+ }
+ assertEquals(hasThrown, 1);
+ }
+ );
+
+ thisCheckFailed.forEach(
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ (thisArg: any): void => {
+ let hasThrown = 0;
+ try {
+ setTimeout.call(thisArg, noop, 1);
+ hasThrown = 1;
+ } catch (err) {
+ if (err instanceof TypeError) {
+ hasThrown = 2;
+ } else {
+ hasThrown = 3;
+ }
+ }
+ assertEquals(hasThrown, 2);
+ }
+ );
+});