workaround toString() quirks on global context (#4814)

This commit is contained in:
Alex Lam S.L
2021-03-23 03:15:41 +00:00
committed by GitHub
parent f9055df44d
commit 44394e61c9
4 changed files with 16 additions and 19 deletions

View File

@@ -176,13 +176,13 @@ issue_4054: {
issue_4811_1: { issue_4811_1: {
input: { input: {
for (var PASS in this); for (var PASS in this);
console.log(PASS, this); console.log(PASS, this, {} < this);
} }
expect: { expect: {
for (var PASS in this); 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: { issue_4811_2: {
@@ -192,12 +192,12 @@ issue_4811_2: {
input: { input: {
(async function() {}); (async function() {});
for (var PASS in this); for (var PASS in this);
console.log(PASS, this); console.log(PASS, this, {} < this);
} }
expect: { expect: {
for (var PASS in this); 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" node_version: ">=8"
} }

View File

@@ -366,7 +366,7 @@ describe("test/reduce.js", function() {
var code = [ var code = [
"(async function() {});", "(async function() {});",
"for (var k in this);", "for (var k in this);",
"console.log(k);", "console.log(k, 42 + this);",
].join("\n"); ].join("\n");
var result = reduce_test(code, { var result = reduce_test(code, {
mangle: false, mangle: false,

View File

@@ -170,12 +170,6 @@ function setup(global, builtins, setup_log, setup_tty) {
}, },
global: { get: self }, global: { get: self },
self: { get: self }, self: { get: self },
// for Node.js v8+
toString: {
get: function() {
return global_toString;
},
},
window: { get: self }, window: { get: self },
}; };
[ [
@@ -205,15 +199,19 @@ function setup(global, builtins, setup_log, setup_tty) {
} catch (e) {} } catch (e) {}
}); });
Object.defineProperties(global, props); 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() { function self() {
return this; return this;
} }
function global_toString() {
return "[object global]";
}
function safe_log(arg, cache) { function safe_log(arg, cache) {
if (arg) switch (typeof arg) { if (arg) switch (typeof arg) {
case "function": case "function":

View File

@@ -2206,8 +2206,7 @@ function log(options) {
function sort_globals(code) { function sort_globals(code) {
var globals = run_code("throw Object.keys(this).sort(" + function(global) { var globals = run_code("throw Object.keys(this).sort(" + function(global) {
return function(m, n) { return function(m, n) {
return (n == "toString") - (m == "toString") return (typeof global[n] == "function") - (typeof global[m] == "function")
|| (typeof global[n] == "function") - (typeof global[m] == "function")
|| (m < n ? -1 : m > n ? 1 : 0); || (m < n ? -1 : m > n ? 1 : 0);
}; };
} + "(this));" + code); } + "(this));" + code);