Skip to content

Commit

Permalink
workaround toString() quirks on global context (#4814)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Mar 23, 2021
1 parent f9055df commit 44394e6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
12 changes: 6 additions & 6 deletions test/compress/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ issue_4054: {
issue_4811_1: {
input: {
for (var PASS in this);
console.log(PASS, this);
console.log(PASS, this, {} < this);
}
expect: {
for (var PASS in this);
console.log(PASS, this);
console.log(PASS, this, {} < this);
}
expect_stdout: "PASS [object global]"
expect_stdout: "PASS [object global] true"
}

issue_4811_2: {
Expand All @@ -192,12 +192,12 @@ issue_4811_2: {
input: {
(async function() {});
for (var PASS in this);
console.log(PASS, this);
console.log(PASS, this, {} < this);
}
expect: {
for (var PASS in this);
console.log(PASS, this);
console.log(PASS, this, {} < this);
}
expect_stdout: "PASS [object global]"
expect_stdout: "PASS [object global] true"
node_version: ">=8"
}
2 changes: 1 addition & 1 deletion test/mocha/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ describe("test/reduce.js", function() {
var code = [
"(async function() {});",
"for (var k in this);",
"console.log(k);",
"console.log(k, 42 + this);",
].join("\n");
var result = reduce_test(code, {
mangle: false,
Expand Down
18 changes: 8 additions & 10 deletions test/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@ function setup(global, builtins, setup_log, setup_tty) {
},
global: { get: self },
self: { get: self },
// for Node.js v8+
toString: {
get: function() {
return global_toString;
},
},
window: { get: self },
};
[
Expand Down Expand Up @@ -205,15 +199,19 @@ function setup(global, builtins, setup_log, setup_tty) {
} catch (e) {}
});
Object.defineProperties(global, props);
// for Node.js v8+
if (global.toString !== Object.prototype.toString) {
global.__proto__ = Object.defineProperty(Object.create(global.__proto__), "toString", {
value: function() {
return "[object global]";
},
});
}

function self() {
return this;
}

function global_toString() {
return "[object global]";
}

function safe_log(arg, cache) {
if (arg) switch (typeof arg) {
case "function":
Expand Down
3 changes: 1 addition & 2 deletions test/ufuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2206,8 +2206,7 @@ function log(options) {
function sort_globals(code) {
var globals = run_code("throw Object.keys(this).sort(" + function(global) {
return function(m, n) {
return (n == "toString") - (m == "toString")
|| (typeof global[n] == "function") - (typeof global[m] == "function")
return (typeof global[n] == "function") - (typeof global[m] == "function")
|| (m < n ? -1 : m > n ? 1 : 0);
};
} + "(this));" + code);
Expand Down

0 comments on commit 44394e6

Please sign in to comment.