feat: Deno namespace configurable and unfrozen (#11062)

Closes #11033
This commit is contained in:
Kitson Kelly 2021-06-22 07:17:35 +10:00 committed by GitHub
parent 2d2b5625e0
commit cda15f2a98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 45 deletions

View file

@ -62,8 +62,8 @@ unitTest(function DenoNamespaceEqualsWindowDeno(): void {
assert(Deno === window.Deno); assert(Deno === window.Deno);
}); });
unitTest(function DenoNamespaceIsFrozen(): void { unitTest(function DenoNamespaceIsNotFrozen(): void {
assert(Object.isFrozen(Deno)); assert(!Object.isFrozen(Deno));
}); });
unitTest(function webAssemblyExists(): void { unitTest(function webAssemblyExists(): void {
@ -77,46 +77,14 @@ declare global {
} }
} }
unitTest(function DenoNamespaceImmutable(): void { unitTest(function DenoNamespaceConfigurable() {
const denoCopy = window.Deno; const desc = Object.getOwnPropertyDescriptor(globalThis, "Deno");
try { assert(desc);
// deno-lint-ignore no-explicit-any assert(desc.configurable);
(Deno as any) = 1; assert(!desc.writable);
} catch { });
// pass
}
assert(denoCopy === Deno);
try {
// deno-lint-ignore no-explicit-any
(window as any).Deno = 1;
} catch {
// pass
}
assert(denoCopy === Deno);
try {
// deno-lint-ignore no-explicit-any
delete (window as any).Deno;
} catch {
// pass
}
assert(denoCopy === Deno);
const { readFile } = Deno;
try {
// deno-lint-ignore no-explicit-any
(Deno as any).readFile = 1;
} catch {
// pass
}
assert(readFile === Deno.readFile);
try {
// deno-lint-ignore no-explicit-any
delete (window as any).Deno.readFile;
} catch {
// pass
}
assert(readFile === Deno.readFile);
unitTest(function DenoCoreNamespaceIsImmutable(): void {
const { print } = Deno.core; const { print } = Deno.core;
try { try {
Deno.core.print = 1; Deno.core.print = 1;

View file

@ -117,6 +117,7 @@
return { return {
value, value,
writable: true, writable: true,
enumerable: false,
configurable: true, configurable: true,
}; };
} }
@ -125,6 +126,8 @@
return { return {
value, value,
enumerable: true, enumerable: true,
writable: false,
configurable: true,
}; };
} }
@ -132,6 +135,7 @@
return { return {
get: getter, get: getter,
enumerable: true, enumerable: true,
configurable: true,
}; };
} }

View file

@ -492,10 +492,9 @@ delete Object.prototype.__proto__;
Object.assign(finalDenoNs, denoNsUnstable); Object.assign(finalDenoNs, denoNsUnstable);
} }
// Setup `Deno` global - we're actually overriding already // Setup `Deno` global - we're actually overriding already existing global
// existing global `Deno` with `Deno` namespace from "./deno.ts". // `Deno` with `Deno` namespace from "./deno.ts".
util.immutableDefine(globalThis, "Deno", finalDenoNs); Object.defineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
Object.freeze(globalThis.Deno);
Object.freeze(globalThis.Deno.core); Object.freeze(globalThis.Deno.core);
Object.freeze(globalThis.Deno.core.sharedQueue); Object.freeze(globalThis.Deno.core.sharedQueue);
signals.setSignals(); signals.setSignals();