From fbcbbd7ae3fa2d57e8ad026a1f9f01c6d07fd80b Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Mon, 11 Oct 2021 09:50:18 -0700 Subject: fix(runtime): Declare `Window.self` and `DedicatedWorkerGlobalScope.name` with `util.writable()` (#12378) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Window`'s `self` property and `DedicatedWorkerGlobalScope`'s `name` property are defined as Web IDL read-only attributes with the `[Replaceable]` extended attribute, meaning that their setter will redefine the property as a data property with the set value, rather than changing some internal state. Deno currently defines them as read-only data properties instead. Given that Web IDL requires all attributes to be accessor properties rather than data properties, but Deno exposes almost all of those properties as either read-only or writable data properties, it makes sense to expose `[Replaceable]` properties as writable as well – as is already the case with `WindowOrWorkerGlobalScope`'s `performance` property. --- cli/tests/integration/run_tests.rs | 7 +++++++ cli/tests/testdata/replace_self.js | 21 +++++++++++++++++++++ cli/tests/testdata/replace_self.js.out | 4 ++++ 3 files changed, 32 insertions(+) create mode 100644 cli/tests/testdata/replace_self.js create mode 100644 cli/tests/testdata/replace_self.js.out (limited to 'cli/tests') diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index f247aa93f..f469bb364 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -1185,6 +1185,13 @@ itest!(error_import_map_unable_to_load { exit_code: 1, }); +// Test that setting `self` in the main thread to some other value doesn't break +// the world. +itest!(replace_self { + args: "run replace_self.js", + output: "replace_self.js.out", +}); + itest!(worker_event_handler_test { args: "run --quiet --reload --allow-read worker_event_handler_test.js", output: "worker_event_handler_test.js.out", diff --git a/cli/tests/testdata/replace_self.js b/cli/tests/testdata/replace_self.js new file mode 100644 index 000000000..cfd473cd3 --- /dev/null +++ b/cli/tests/testdata/replace_self.js @@ -0,0 +1,21 @@ +// Test that setting `self` in the main thread to some other value doesn't break +// the world, in particular for events fired on the global scope. + +// deno-lint-ignore no-global-assign +self = null; + +addEventListener("load", () => { + console.log("load event (event listener)"); +}); + +addEventListener("unload", () => { + console.log("unload event (event listener)"); +}); + +globalThis.onload = () => { + console.log("load event (event handler)"); +}; + +globalThis.onunload = () => { + console.log("unload event (event handler)"); +}; diff --git a/cli/tests/testdata/replace_self.js.out b/cli/tests/testdata/replace_self.js.out new file mode 100644 index 000000000..aaffb5a62 --- /dev/null +++ b/cli/tests/testdata/replace_self.js.out @@ -0,0 +1,4 @@ +load event (event listener) +load event (event handler) +unload event (event listener) +unload event (event handler) -- cgit v1.2.3