Skip to content

Commit

Permalink
Evaluate some BigInt operations. Closes #1388
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed May 15, 2023
1 parent 3cb47fb commit 97a79a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/compress/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import {
AST_Array,
AST_Arrow,
AST_BigInt,
AST_BlockStatement,
AST_Call,
AST_Chain,
Expand Down Expand Up @@ -124,6 +125,8 @@ export function make_node_from_constant(val, orig) {
operator: "-",
expression: make_node(AST_Infinity, orig)
}) : make_node(AST_Infinity, orig);
case "bigint":
return make_node(AST_BigInt, orig, { value: val.toString() });
case "boolean":
return make_node(val ? AST_True : AST_False, orig);
case "undefined":
Expand Down
20 changes: 18 additions & 2 deletions lib/compress/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ def_eval(AST_Constant, function () {
return this.getValue();
});

def_eval(AST_BigInt, return_this);
const supports_bigint = typeof BigInt === "function";
def_eval(AST_BigInt, function () {
if (supports_bigint) {
return BigInt(this.value);
} else {
return this;
}
});

def_eval(AST_RegExp, function (compressor) {
let evaluated = compressor.evaluated_regexps.get(this.value);
Expand Down Expand Up @@ -246,6 +253,7 @@ const identity_comparison = makePredicate("== != === !==");
const has_identity = value => typeof value === "object"
|| typeof value === "function"
|| typeof value === "symbol";
const no_bigint_ops = new Set(['**', ">>>"]);

def_eval(AST_Binary, function (compressor, depth) {
if (!non_converting_binary.has(this.operator))
Expand All @@ -269,6 +277,14 @@ def_eval(AST_Binary, function (compressor, depth) {
return this;
}

// Do not mix BigInt and Number; Don't use Math.pow() on BigInt
if (
(typeof left === "bigint") !== (typeof right === "bigint")
|| typeof left === "bigint" && no_bigint_ops.has(this.operator)
) {
return this;
}

switch (this.operator) {
case "&&": result = left && right; break;
case "||": result = left || right; break;
Expand Down Expand Up @@ -296,7 +312,7 @@ def_eval(AST_Binary, function (compressor, depth) {
default:
return this;
}
if (isNaN(result) && compressor.find_parent(AST_With)) {
if (typeof result === "number" && isNaN(result) && compressor.find_parent(AST_With)) {
// leave original expression as is
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion test/compress/big_int.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ big_int_math: {
const div = 15n / 5n;
const regular_number = 1 * 10;
}
expect_exact: "const sum=10n+15n,exp=5n**10n,sub=1n-3n,mul=5n*5n,div=15n/5n,regular_number=10;"
expect_exact: "const sum=25n,exp=5n**10n,sub=-2n,mul=25n,div=3n,regular_number=10;"
}

0 comments on commit 97a79a6

Please sign in to comment.