Skip to content

Commit

Permalink
support exponentiation operator (#4593)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jan 24, 2021
1 parent a08d425 commit 9d23ba0
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1254,3 +1254,9 @@ To allow for better optimizations, the compiler makes various assumptions:
}()) => b)());
```
UglifyJS may modify the input which in turn may suppress those errors.
- Some arithmetic operations with `BigInt` may throw `TypeError`:
```javascript
1n + 1;
// TypeError: can't convert BigInt to number
```
UglifyJS may modify the input which in turn may suppress those errors.
3 changes: 3 additions & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4168,6 +4168,9 @@ merge(Compressor.prototype, {
case "<=" : result = left <= right; break;
case ">" : result = left > right; break;
case ">=" : result = left >= right; break;
case "**":
result = Math.pow(left, right);
break;
case "in":
if (right && typeof right == "object" && HOP(right, left)) {
result = true;
Expand Down
13 changes: 11 additions & 2 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ function OutputStream(options) {

PARENS(AST_Unary, function(output) {
var p = output.parent();
// (-x) ** y
if (p instanceof AST_Binary) return p.operator == "**" && p.left === this;
// (x++).toString(3)
// (typeof x).length
return (p instanceof AST_Call || p instanceof AST_PropAccess) && p.expression === this;
});

Expand Down Expand Up @@ -722,11 +726,14 @@ function OutputStream(options) {
var p = output.parent();
// await (foo && bar)
if (p instanceof AST_Await) return true;
// this deals with precedence: 3 * (2 + 1)
// this deals with precedence:
// 3 * (2 + 1)
// 3 - (2 - 1)
// (1 ** 2) ** 3
if (p instanceof AST_Binary) {
var po = p.operator, pp = PRECEDENCE[po];
var so = this.operator, sp = PRECEDENCE[so];
return pp > sp || (pp == sp && this === p.right);
return pp > sp || (pp == sp && this === p[po == "**" ? "left" : "right"]);
}
// (foo && bar)()
if (p instanceof AST_Call) return p.expression === this;
Expand Down Expand Up @@ -818,6 +825,8 @@ function OutputStream(options) {

PARENS(AST_Await, function(output) {
var p = output.parent();
// (await x) ** y
if (p instanceof AST_Binary) return p.operator == "**" && p.left === this;
// new (await foo)
// (await foo)(bar)
if (p instanceof AST_Call) return p.expression === this;
Expand Down
6 changes: 4 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var OPERATORS = makePredicate([
"*",
"/",
"%",
"**",
">>",
"<<",
">>>",
Expand Down Expand Up @@ -630,7 +631,8 @@ var PRECEDENCE = function(a, ret) {
["<", ">", "<=", ">=", "in", "instanceof"],
[">>", "<<", ">>>"],
["+", "-"],
["*", "/", "%"]
["*", "/", "%"],
["**"],
], {});

var ATOMIC_START_TOKEN = makePredicate("atom bigint num regexp string");
Expand Down Expand Up @@ -1860,7 +1862,7 @@ function parse($TEXT, options) {
var prec = op != null ? PRECEDENCE[op] : null;
if (prec != null && prec > min_prec) {
next();
var right = expr_op(maybe_await(), prec, no_in);
var right = expr_op(maybe_await(), op == "**" ? prec - 1 : prec, no_in);
return expr_op(new AST_Binary({
start : left.start,
left : left,
Expand Down
58 changes: 58 additions & 0 deletions test/compress/exponentiation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
precedence_1: {
input: {
console.log(-4 ** 3 ** 2);
}
expect_exact: "console.log((-4)**3**2);"
expect_stdout: "-262144"
node_version: ">=8"
}

precedence_2: {
input: {
console.log(-4 ** (3 ** 2));
}
expect_exact: "console.log((-4)**3**2);"
expect_stdout: "-262144"
node_version: ">=8"
}

precedence_3: {
input: {
console.log(-(4 ** 3) ** 2);
}
expect_exact: "console.log((-(4**3))**2);"
expect_stdout: "4096"
node_version: ">=8"
}

precedence_4: {
input: {
console.log((-4 ** 3) ** 2);
}
expect_exact: "console.log(((-4)**3)**2);"
expect_stdout: "4096"
node_version: ">=8"
}

await: {
input: {
(async a => a * await a ** ++a % a)(2).then(console.log);
}
expect_exact: "(async a=>a*(await a)**++a%a)(2).then(console.log);"
expect_stdout: "1"
node_version: ">=8"
}

evaluate: {
options = {
evaluate: true,
}
input: {
console.log(1 + 2 ** 3 - 4);
}
expect: {
console.log(5);
}
expect_stdout: "5"
node_version: ">=8"
}
20 changes: 12 additions & 8 deletions test/ufuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ var SUPPORT = function(matrix) {
const_block: "var a; { const a = 0; }",
default_value: "[ a = 0 ] = [];",
destructuring: "[] = [];",
exponentiation: "0 ** 0",
let: "let a;",
rest: "var [...a] = [];",
rest_object: "var {...a} = {};",
Expand Down Expand Up @@ -168,7 +169,7 @@ var VALUES = [
"4",
"5",
"22",
"-0", // 0/-0 !== 0
"(-0)", // 0/-0 !== 0
"23..toString()",
"24 .toString()",
"25. ",
Expand All @@ -190,7 +191,7 @@ var VALUES = [
"this",
];
if (SUPPORT.bigint) VALUES = VALUES.concat([
"!0o644n",
"(!0o644n)",
"([3n][0] > 2)",
"(-42n).toString()",
"Number(0XDEADn << 16n | 0xbeefn)",
Expand Down Expand Up @@ -224,6 +225,7 @@ var BINARY_OPS = [
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
if (SUPPORT.exponentiation) BINARY_OPS.push("**");
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
BINARY_OPS = BINARY_OPS.concat(BINARY_OPS);
BINARY_OPS.push(" in ");
Expand Down Expand Up @@ -439,17 +441,19 @@ function createAssignmentPairs(recurmax, stmtDepth, canThrow, nameLenBefore, was
return pairs;

function mapShuffled(values, fn) {
var ordered = [];
var shuffled = [];
var declare_only = [];
var side_effects = [];
values.forEach(function(value, index) {
value = fn(value, index);
if (/]:/.test(value) ? canThrow && rng(10) == 0 : rng(5)) {
shuffled.splice(rng(shuffled.length + 1), 0, value);
if (/]:|=/.test(value) ? canThrow && rng(10) == 0 : rng(5)) {
declare_only.splice(rng(declare_only.length + 1), 0, value);
} else if (canThrow && rng(5) == 0) {
side_effects.splice(rng(side_effects.length + 1), 0, value);
} else {
ordered.push(value);
side_effects.push(value);
}
});
return shuffled.concat(ordered);
return declare_only.concat(side_effects);
}

function convertToRest(names) {
Expand Down

0 comments on commit 9d23ba0

Please sign in to comment.