Skip to content

Commit

Permalink
fix: allow setting globalThis.location when no --location is prov…
Browse files Browse the repository at this point in the history
…ided (denoland#15448)
  • Loading branch information
aslilac committed Aug 10, 2022
1 parent f16fe44 commit 08061b6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
8 changes: 8 additions & 0 deletions cli/tests/testdata/070_location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// deno-lint-ignore-file no-global-assign
console.log(Location);
console.log(Location.prototype);
console.log(location);
try {
location = {};
} catch (error) {
if (error instanceof Error) {
console.log(error.toString());
}
}
try {
location.hostname = "bar";
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions cli/tests/testdata/070_location.ts.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Location {
protocol: "https:",
search: "?baz"
}
NotSupportedError: Cannot set "location".
NotSupportedError: Cannot set "location.hostname".
[WILDCARD]
13 changes: 13 additions & 0 deletions cli/tests/testdata/071_location_unset.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
console.log(Location);
console.log(Location.prototype);
console.log(location);

globalThis.location = {
hash: "#bat",
host: "foo",
hostname: "foo",
href: "https://foo/bar?baz#bat",
origin: "https://foo",
pathname: "/bar",
port: "",
protocol: "https:",
search: "?baz",
};
console.log(location.pathname);
1 change: 1 addition & 0 deletions cli/tests/testdata/071_location_unset.ts.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[WILDCARD][Function: Location]
Location {}
undefined
/bar
[WILDCARD]
38 changes: 24 additions & 14 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ delete Intl.v8BreakIterator;
throw new Error("Worker runtime already bootstrapped");
}

const {
args,
location: locationHref,
noColor,
isTty,
pid,
ppid,
unstableFlag,
cpuCount,
userAgent: userAgentInfo,
} = runtimeOptions;

performance.setTimeOrigin(DateNow());
const consoleFromV8 = window.console;
const wrapConsole = window.__bootstrap.console.wrapConsole;
Expand All @@ -648,6 +660,18 @@ delete Intl.v8BreakIterator;
delete globalThis.bootstrap;
util.log("bootstrapMainRuntime");
hasBootstrapped = true;

// If the `--location` flag isn't set, make `globalThis.location` `undefined` and
// writable, so that they can mock it themselves if they like. If the flag was
// set, define `globalThis.location`, using the provided value.
if (locationHref == null) {
mainRuntimeGlobalProperties.location = {
writable: true,
};
} else {
location.setLocationHref(locationHref);
}

ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
if (runtimeOptions.unstableFlag) {
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
Expand Down Expand Up @@ -678,22 +702,8 @@ delete Intl.v8BreakIterator;
});

runtimeStart(runtimeOptions);
const {
args,
location: locationHref,
noColor,
isTty,
pid,
ppid,
unstableFlag,
cpuCount,
userAgent: userAgentInfo,
} = runtimeOptions;

colors.setNoColor(noColor || !isTty);
if (locationHref != null) {
location.setLocationHref(locationHref);
}
numCpus = cpuCount;
userAgent = userAgentInfo;
registerErrors();
Expand Down

0 comments on commit 08061b6

Please sign in to comment.