fix corner cases with new.target (#4784)

This commit is contained in:
Alex Lam S.L
2021-03-16 06:34:36 +00:00
committed by GitHub
parent 77c9116c91
commit 352a944868
5 changed files with 126 additions and 30 deletions

View File

@@ -5753,22 +5753,102 @@ issue_4725_2: {
node_version: ">=4"
}
new_target: {
new_target_1: {
input: {
console.log(typeof new function() {
return new.target;
}, function() {
new function f() {
console.log(new.target === f);
}();
console.log(function() {
return new.target;
}());
}
expect: {
console.log(typeof new function() {
return new.target;
}(), function() {
new function f() {
console.log(new.target === f);
}();
console.log(function() {
return new.target;
}());
}
expect_stdout: "function undefined"
expect_stdout: [
"true",
"undefined",
]
node_version: ">=6"
}
new_target_2: {
input: {
new function(a) {
if (!new.target)
console.log("FAIL");
else if (a)
console.log("PASS");
else
new new.target(new.target.length);
}();
}
expect: {
new function(a) {
if (!new.target)
console.log("FAIL");
else if (a)
console.log("PASS");
else
new new.target(new.target.length);
}();
}
expect_stdout: "PASS"
node_version: ">=6"
}
new_target_collapse_vars: {
options = {
collapse_vars: true,
unused: true,
}
input: {
new function(a) {
if (a)
console.log("PASS");
else
new new.target(new.target.length);
}(0);
}
expect: {
new function(a) {
if (a)
console.log("PASS");
else
new new.target(new.target.length);
}(0);
}
expect_stdout: "PASS"
node_version: ">=6"
}
new_target_reduce_vars: {
options = {
evaluate: true,
reduce_vars: true,
}
input: {
new function(a) {
if (a)
console.log("PASS");
else
new new.target(new.target.length);
}(0);
}
expect: {
new function(a) {
if (a)
console.log("PASS");
else
new new.target(new.target.length);
}(0);
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@@ -149,6 +149,7 @@ var SUPPORT = function(matrix) {
for_of: "for (var a of []);",
generator: "function* f(){}",
let: "let a;",
new_target: "function f() { new.target; }",
nullish: "0 ?? 0",
rest: "var [...a] = [];",
rest_object: "var {...a} = {};",
@@ -1401,13 +1402,16 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
createBlockVariables(recurmax, stmtDepth, canThrow, function(defns) {
s.push(
instantiate + makeFunction(name) + "(" + createParams(save_async, save_generator) + "){",
strictMode(),
defns()
strictMode()
);
var add_new_target = SUPPORT.new_target && VALUES.indexOf("new.target") < 0;
if (add_new_target) VALUES.push("new.target");
s.push(defns());
if (instantiate) for (var i = rng(4); --i >= 0;) {
s.push((in_class ? "if (this) " : "") + createThisAssignment(recurmax, stmtDepth, canThrow));
}
s.push(_createStatements(rng(5) + 1, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth));
if (add_new_target) VALUES.splice(VALUES.indexOf("new.target"), 1);
});
generator = save_generator;
async = save_async;