1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix(runtime): Declare Window.self and DedicatedWorkerGlobalScope.name with util.writable() (#12378)

`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.
This commit is contained in:
Andreu Botella 2021-10-11 09:50:18 -07:00 committed by GitHub
parent c40d5040cd
commit fbcbbd7ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 3 deletions

View file

@ -1185,6 +1185,13 @@ itest!(error_import_map_unable_to_load {
exit_code: 1, 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 { itest!(worker_event_handler_test {
args: "run --quiet --reload --allow-read worker_event_handler_test.js", args: "run --quiet --reload --allow-read worker_event_handler_test.js",
output: "worker_event_handler_test.js.out", output: "worker_event_handler_test.js.out",

21
cli/tests/testdata/replace_self.js vendored Normal file
View file

@ -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)");
};

View file

@ -0,0 +1,4 @@
load event (event listener)
load event (event handler)
unload event (event listener)
unload event (event handler)

View file

@ -475,7 +475,7 @@ delete Object.prototype.__proto__;
location: location.locationDescriptor, location: location.locationDescriptor,
Window: globalInterfaces.windowConstructorDescriptor, Window: globalInterfaces.windowConstructorDescriptor,
window: util.readOnly(globalThis), window: util.readOnly(globalThis),
self: util.readOnly(globalThis), self: util.writable(globalThis),
Navigator: util.nonEnumerable(Navigator), Navigator: util.nonEnumerable(Navigator),
navigator: { navigator: {
configurable: true, configurable: true,
@ -628,7 +628,7 @@ delete Object.prototype.__proto__;
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope); ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
} }
ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties); ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties);
ObjectDefineProperties(globalThis, { name: util.readOnly(name) }); ObjectDefineProperties(globalThis, { name: util.writable(name) });
if (runtimeOptions.enableTestingFeaturesFlag) { if (runtimeOptions.enableTestingFeaturesFlag) {
ObjectDefineProperty( ObjectDefineProperty(
globalThis, globalThis,

View file

@ -27840,7 +27840,7 @@
"headers-normalize.any.html": true, "headers-normalize.any.html": true,
"headers-record.any.html": true, "headers-record.any.html": true,
"headers-structure.any.html": true, "headers-structure.any.html": true,
"headers-basic.any.worker.html": false, "headers-basic.any.worker.html": true,
"headers-casing.any.worker.html": true, "headers-casing.any.worker.html": true,
"headers-combine.any.worker.html": true, "headers-combine.any.worker.html": true,
"headers-errors.any.worker.html": true, "headers-errors.any.worker.html": true,