From 3761d054d0d6f10f9ead6105f3eb633572a38673 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+00ff0000red@users.noreply.github.com> Date: Wed, 6 Jan 2021 05:53:30 -0800 Subject: fix: stronger input checking for setTimeout; add function overload (#8957) --- cli/tests/unit/timers_test.ts | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'cli/tests/unit') diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts index e315d018a..55bc11f76 100644 --- a/cli/tests/unit/timers_test.ts +++ b/cli/tests/unit/timers_test.ts @@ -7,10 +7,51 @@ import { unitTest, } from "./test_util.ts"; -function waitForMs(ms: number): Promise { +function waitForMs(ms: number): Promise { return new Promise((resolve): number => setTimeout(resolve, ms)); } +unitTest(async function functionParameterBindingSuccess(): Promise { + const promise = deferred(); + let count = 0; + + const nullProto = (newCount: number): void => { + count = newCount; + promise.resolve(); + }; + + Reflect.setPrototypeOf(nullProto, null); + + setTimeout(nullProto, 500, 1); + await promise; + // count should be reassigned + assertEquals(count, 1); +}); + +unitTest(async function stringifyAndEvalNonFunctions(): Promise { + // eval can only access global scope + const global = globalThis as unknown as { + globalPromise: ReturnType; + globalCount: number; + }; + global.globalPromise = deferred(); + global.globalCount = 0; + + const notAFunction = + "globalThis.globalCount++; globalThis.globalPromise.resolve();" as unknown as () => + void; + + setTimeout(notAFunction, 500); + + await global.globalPromise; + + // count should be incremented + assertEquals(global.globalCount, 1); + + Reflect.deleteProperty(global, "globalPromise"); + Reflect.deleteProperty(global, "globalCount"); +}); + unitTest(async function timeoutSuccess(): Promise { const promise = deferred(); let count = 0; -- cgit v1.2.3