From b10563cb2083f7af9d4320662d6aa1897b6db23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 18 Jan 2022 00:13:14 +0100 Subject: fix(runtime): don't crash when window is deleted (#13392) This commit fixes an error when user deletes "window" global JS variable. Instead of relying on "window" or "globalThis" to dispatch "load" and "unload" events, we are default to global scope of the worker. --- runtime/worker.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'runtime') diff --git a/runtime/worker.rs b/runtime/worker.rs index ad31cacc6..7a3a6c1c3 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -313,7 +313,13 @@ impl MainWorker { &mut self, script_name: &str, ) -> Result<(), AnyError> { - self.execute_script(script_name, "window.dispatchEvent(new Event('load'))") + self.execute_script( + script_name, + // NOTE(@bartlomieju): not using `globalThis` here, because user might delete + // it. Instead we're using global `dispatchEvent` function which will + // used a saved reference to global scope. + "dispatchEvent(new Event('load'))", + ) } /// Dispatches "unload" event to the JavaScript runtime. @@ -323,8 +329,13 @@ impl MainWorker { &mut self, script_name: &str, ) -> Result<(), AnyError> { - self - .execute_script(script_name, "window.dispatchEvent(new Event('unload'))") + self.execute_script( + script_name, + // NOTE(@bartlomieju): not using `globalThis` here, because user might delete + // it. Instead we're using global `dispatchEvent` function which will + // used a saved reference to global scope. + "dispatchEvent(new Event('unload'))", + ) } } -- cgit v1.2.3