Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5873)
Browse files Browse the repository at this point in the history
fixes #5872
  • Loading branch information
alexlamsl committed Jul 8, 2024
1 parent 787dfbe commit 124c4d3
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,20 @@ Compressor.prototype.compress = function(node) {
}

function access(tw, def) {
tw.defined_ids[def.id] = [ tw.defined_ids.seq ];
tw.defined_ids[def.id] = tw.defined_ids.seq;
}

function assign(tw, def) {
tw.assigned_ids[def.id] = tw.defined_ids.seq;
tw.defined_ids[def.id] = false;
}

function safe_to_access(tw, def) {
var seq = tw.defined_ids.seq;
var defined = tw.defined_ids[def.id];
if (defined) defined[0] = false;
if (defined !== seq) return false;
var assigned = tw.assigned_ids[def.id];
return !assigned || assigned === seq;
}

function mark(tw, def) {
Expand Down Expand Up @@ -1424,8 +1432,7 @@ Compressor.prototype.compress = function(node) {
var d = ref.definition();
var fixed = d.fixed || d.last_ref && d.last_ref.fixed;
push_ref(d, ref);
var defined = tw.defined_ids[d.id];
if (defined && defined[0] === tw.defined_ids.seq) ref.defined = true;
if (safe_to_access(tw, d)) ref.defined = true;
if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) {
tw.loop_ids[d.id] = tw.in_loop;
}
Expand Down Expand Up @@ -1656,6 +1663,7 @@ Compressor.prototype.compress = function(node) {
return node.reduce_vars(tw, descend, compressor);
} : reset_flags);
// Side-effect tracking on sequential property access
tw.assigned_ids = Object.create(null);
tw.defined_ids = Object.create(null);
tw.defined_ids.seq = {};
// Flow control for visiting lambda definitions
Expand Down
116 changes: 116 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8346,3 +8346,119 @@ issue_1666_undefined_strict: {
}
expect_stdout: true
}

issue_5872_1: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = 42;
try {
while (!function f() {
a.p;
a = null;
}(a.q)) {
a.r;
console.log("FAIL");
}
} catch (e) {
console.log("PASS");
}
}
expect: {
var a = 42;
try {
while (!function f() {
a.p;
a = null;
}(a.q)) {
a.r;
console.log("FAIL");
}
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

issue_5872_2: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
toplevel: true,
}
input: {
function f() {
a.p;
a = null;
}

var a = 42;
try {
while (!f(a.q)) {
a.r;
console.log("FAIL");
}
} catch (e) {
console.log("PASS");
}
}
expect: {
function f() {
a.p;
a = null;
}

var a = 42;
try {
while (!f(a.q)) {
a.r;
console.log("FAIL");
}
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

issue_5872_3: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = 42;
try {
while (new function() {
a.p;
a = null;
}(a.q)) {
a.r;
console.log("FAIL");
}
} catch (e) {
console.log("PASS");
}
}
expect: {
var a = 42;
try {
while (new function() {
a.p;
a = null;
}(a.q)) {
a.r;
console.log("FAIL");
}
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
}

0 comments on commit 124c4d3

Please sign in to comment.