Skip to content

Commit

Permalink
fix crash in optimizing bitwise ops. Closes #1496
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Feb 23, 2024
1 parent 314152d commit 0ef0584
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/compress/index.js
Expand Up @@ -2711,24 +2711,26 @@ def_optimize(AST_Binary, function(self, compressor) {
y_node = self.right.left;
}

if ((y & z) === 0) {
self = make_node(AST_Binary, self, {
operator: self.operator,
left: z_node,
right: x_node
});
} else {
const reordered_ops = make_node(AST_Binary, self, {
operator: "|",
left: make_node(AST_Binary, self, {
operator: "&",
left: x_node,
right: z_node
}),
right: make_node_from_constant(y & z, y_node),
});
if (x_node && y_node) {
if ((y & z) === 0) {
self = make_node(AST_Binary, self, {
operator: self.operator,
left: z_node,
right: x_node
});
} else {
const reordered_ops = make_node(AST_Binary, self, {
operator: "|",
left: make_node(AST_Binary, self, {
operator: "&",
left: x_node,
right: z_node
}),
right: make_node_from_constant(y & z, y_node),
});

self = best_of(compressor, self, reordered_ops);
self = best_of(compressor, self, reordered_ops);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -278,6 +278,16 @@ bitwise_2: {
}
}

bitwise_regression: {
options = {
evaluate: true,
}
input: {
console.log((id(0xff00ff) | id(0x00ff00)) & 0x3fff)
}
expect_stdout: "16383"
}

unary_prefix: {
options = {
evaluate: true,
Expand Down

0 comments on commit 0ef0584

Please sign in to comment.