summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-01-17 23:23:49 +0100
committerGitHub <noreply@github.com>2022-01-17 23:23:49 +0100
commitbc666e42a89f2f6ab104e5f1c1298ec590a99ce9 (patch)
tree3c2873e350e73b229182fffe1102d7779cac22ac
parent1ab5dea367800defa264b633705f8b535bb167d9 (diff)
fix(ext/console): don't depend on globalThis present (#13387)
-rw-r--r--cli/tests/integration/run_tests.rs5
-rw-r--r--cli/tests/testdata/colors_without_globalThis.js1
-rw-r--r--ext/console/01_colors.js14
-rw-r--r--ext/console/02_console.js4
-rw-r--r--runtime/js/99_main.js3
5 files changed, 24 insertions, 3 deletions
diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs
index bc137a7fd..2f32fd67f 100644
--- a/cli/tests/integration/run_tests.rs
+++ b/cli/tests/integration/run_tests.rs
@@ -2479,3 +2479,8 @@ itest!(import_assertions_type_check {
output: "import_assertions/type_check.out",
exit_code: 1,
});
+
+itest!(colors_without_global_this {
+ args: "run colors_without_globalThis.js",
+ output_str: Some("true\n"),
+});
diff --git a/cli/tests/testdata/colors_without_globalThis.js b/cli/tests/testdata/colors_without_globalThis.js
new file mode 100644
index 000000000..f9d4b68fc
--- /dev/null
+++ b/cli/tests/testdata/colors_without_globalThis.js
@@ -0,0 +1 @@
+console.log(delete globalThis.globalThis);
diff --git a/ext/console/01_colors.js b/ext/console/01_colors.js
index 138eaa6d9..42551963c 100644
--- a/ext/console/01_colors.js
+++ b/ext/console/01_colors.js
@@ -11,6 +11,16 @@
ArrayPrototypeJoin,
} = window.__bootstrap.primordials;
+ let noColor = false;
+
+ function setNoColor(value) {
+ noColor = value;
+ }
+
+ function getNoColor() {
+ return noColor;
+ }
+
function code(open, close) {
return {
open: `\x1b[${open}m`,
@@ -79,7 +89,7 @@
}
function maybeColor(fn) {
- return !(globalThis.Deno?.noColor ?? false) ? fn : (s) => s;
+ return !noColor ? fn : (s) => s;
}
window.__bootstrap.colors = {
@@ -95,5 +105,7 @@
magenta,
stripColor,
maybeColor,
+ setNoColor,
+ getNoColor,
};
})(this);
diff --git a/ext/console/02_console.js b/ext/console/02_console.js
index 435c95187..0ebf5a4d9 100644
--- a/ext/console/02_console.js
+++ b/ext/console/02_console.js
@@ -1650,7 +1650,7 @@
}
function inspectArgs(args, inspectOptions = {}) {
- const noColor = globalThis.Deno?.noColor ?? true;
+ const noColor = colors.getNoColor();
const rInspectOptions = { ...DEFAULT_INSPECT_OPTIONS, ...inspectOptions };
const first = args[0];
let a = 0;
@@ -1759,7 +1759,7 @@
function getConsoleInspectOptions() {
return {
...DEFAULT_INSPECT_OPTIONS,
- colors: !(globalThis.Deno?.noColor ?? false),
+ colors: !colors.getNoColor(),
};
}
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 3a56d93dc..534a28733 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -35,6 +35,7 @@ delete Object.prototype.__proto__;
const timers = window.__bootstrap.timers;
const base64 = window.__bootstrap.base64;
const encoding = window.__bootstrap.encoding;
+ const colors = window.__bootstrap.colors;
const Console = window.__bootstrap.console.Console;
const worker = window.__bootstrap.worker;
const internals = window.__bootstrap.internals;
@@ -574,6 +575,7 @@ delete Object.prototype.__proto__;
cpuCount,
} = runtimeOptions;
+ colors.setNoColor(noColor);
if (locationHref != null) {
location.setLocationHref(locationHref);
}
@@ -664,6 +666,7 @@ delete Object.prototype.__proto__;
cpuCount,
} = runtimeOptions;
+ colors.setNoColor(noColor);
location.setLocationHref(locationHref);
numCpus = cpuCount;
registerErrors();