Skip to content

Commit

Permalink
support BigInt literals (#4583)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jan 24, 2021
1 parent ef9f7ca commit 8bfd891
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 171 deletions.
17 changes: 15 additions & 2 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ var AST_BlockScope = DEFNODE("BlockScope", "enclosed functions make_def parent_s
enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
functions: "[Object/S] like `variables`, but only lists function declarations",
parent_scope: "[AST_Scope?/S] link to the parent scope",
variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
variables: "[Object/S] a map of name ---> SymbolDef for all variables/functions defined in this scope",
},
clone: function(deep) {
var node = this._clone(deep);
Expand Down Expand Up @@ -472,7 +472,7 @@ var AST_Scope = DEFNODE("Scope", "uses_eval uses_with", {
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
$documentation: "The toplevel scope",
$propdoc: {
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
globals: "[Object/S] a map of name ---> SymbolDef for all undeclared names",
},
wrap: function(name) {
var body = this.body;
Expand Down Expand Up @@ -1440,6 +1440,19 @@ var AST_Number = DEFNODE("Number", "value", {
},
_validate: function() {
if (typeof this.value != "number") throw new Error("value must be number");
if (!isFinite(this.value)) throw new Error("value must be finite");
if (this.value < 0) throw new Error("value cannot be negative");
},
}, AST_Constant);

var AST_BigInt = DEFNODE("BigInt", "value", {
$documentation: "A BigInt literal",
$propdoc: {
value: "[string] the numeric representation",
},
_validate: function() {
if (typeof this.value != "string") throw new Error("value must be string");
if (this.value[0] == "-") throw new Error("value cannot be negative");
},
}, AST_Constant);

Expand Down
282 changes: 146 additions & 136 deletions lib/compress.js

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,32 +689,32 @@ function OutputStream(options) {

PARENS(AST_Sequence, function(output) {
var p = output.parent();
// [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
// [ 1, (2, 3), 4 ] ---> [ 1, 3, 4 ]
return p instanceof AST_Array
// () => (foo, bar)
// () ---> (foo, bar)
|| is_arrow(p) && p.value === this
// await (foo, bar)
|| p instanceof AST_Await
// 1 + (2, 3) + 4 ==> 8
// 1 + (2, 3) + 4 ---> 8
|| p instanceof AST_Binary
// new (foo, bar) or foo(1, (2, 3), 4)
|| p instanceof AST_Call
// (false, true) ? (a = 10, b = 20) : (c = 30)
// ==> 20 (side effect, set a := 10 and b := 20)
// ---> 20 (side effect, set a := 10 and b := 20)
|| p instanceof AST_Conditional
// [ a = (1, 2) ] = [] ==> a == 2
// [ a = (1, 2) ] = [] ---> a == 2
|| p instanceof AST_DefaultValue
// { [(1, 2)]: 3 }[2] ==> 3
// { foo: (1, 2) }.foo ==> 2
// { [(1, 2)]: 3 }[2] ---> 3
// { foo: (1, 2) }.foo ---> 2
|| p instanceof AST_DestructuredKeyVal
|| p instanceof AST_ObjectProperty
// (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
// (1, {foo:2}).foo or (1, {foo:2})["foo"] ---> 2
|| p instanceof AST_PropAccess && p.expression === this
// ...(foo, bar, baz)
|| p instanceof AST_Spread
// !(foo, bar, baz)
|| p instanceof AST_Unary
// var a = (1, 2), b = a + a; ==> b == 4
// var a = (1, 2), b = a + a; ---> b == 4
|| p instanceof AST_VarDef;
});

Expand Down Expand Up @@ -777,14 +777,10 @@ function OutputStream(options) {
});

PARENS(AST_Number, function(output) {
if (!output.option("galio")) return false;
// https://github.com/mishoo/UglifyJS/pull/1009
var p = output.parent();
if (p instanceof AST_PropAccess && p.expression === this) {
var value = this.value;
// https://github.com/mishoo/UglifyJS/issues/115
return value < 0
// https://github.com/mishoo/UglifyJS/pull/1009
|| output.option("galio") && /^0/.test(make_num(value));
}
return p instanceof AST_PropAccess && p.expression === this && /^0/.test(make_num(this.value));
});

function needs_parens_assign_cond(self, output) {
Expand All @@ -807,8 +803,8 @@ function OutputStream(options) {
});
PARENS(AST_Assign, function(output) {
if (needs_parens_assign_cond(this, output)) return true;
// v8 parser bug => workaround
// f([1], [a] = []) => f([1], ([a] = []))
// v8 parser bug ---> workaround
// f([1], [a] = []) ---> f([1], ([a] = []))
if (output.option("v8")) return this.left instanceof AST_Destructured;
// ({ p: a } = o);
if (this.left instanceof AST_DestructuredObject) return needs_parens_obj(output);
Expand Down
22 changes: 12 additions & 10 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
}

function read_while(pred) {
var ret = "", ch, i = 0;
while ((ch = peek()) && pred(ch, i++))
ret += next();
var ret = "", ch;
while ((ch = peek()) && pred(ch)) ret += next();
return ret;
}

Expand All @@ -292,16 +291,14 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {

function read_num(prefix) {
var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".";
var num = read_while(function(ch, i) {
var num = read_while(function(ch) {
var code = ch.charCodeAt(0);
switch (code) {
case 120: case 88: // xX
return has_x ? false : (has_x = true);
case 101: case 69: // eE
return has_x ? true : has_e ? false : (has_e = after_e = true);
case 45: // -
return after_e || (i == 0 && !prefix);
case 43: // +
case 43: case 45: // +-
return after_e;
case (after_e = false, 46): // .
return (!has_dot && !has_x && !has_e) ? (has_dot = true) : false;
Expand All @@ -315,8 +312,9 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
num = num.replace(has_x ? /([1-9a-f]|.0)_(?=[0-9a-f])/gi : /([1-9]|.0)_(?=[0-9])/gi, "$1");
}
var valid = parse_js_number(num);
if (!isNaN(valid)) return token("num", valid);
parse_error("Invalid syntax: " + num);
if (isNaN(valid)) parse_error("Invalid syntax: " + num);
if (has_dot || has_e || peek() != "n") return token("num", valid);
return token("bigint", num.toLowerCase() + next());
}

function read_escaped_char(in_string) {
Expand Down Expand Up @@ -635,7 +633,7 @@ var PRECEDENCE = function(a, ret) {
["*", "/", "%"]
], {});

var ATOMIC_START_TOKEN = makePredicate("atom num regexp string");
var ATOMIC_START_TOKEN = makePredicate("atom bigint num regexp string");

/* -----[ Parser ]----- */

Expand Down Expand Up @@ -783,6 +781,7 @@ function parse($TEXT, options) {
semicolon();
return dir ? new AST_Directive(body) : new AST_SimpleStatement({ body: body });
case "num":
case "bigint":
case "regexp":
case "operator":
case "atom":
Expand Down Expand Up @@ -1361,6 +1360,9 @@ function parse($TEXT, options) {
case "num":
ret = new AST_Number({ start: tok, end: tok, value: tok.value });
break;
case "bigint":
ret = new AST_BigInt({ start: tok, end: tok, value: tok.value });
break;
case "string":
ret = new AST_String({
start : tok,
Expand Down
2 changes: 1 addition & 1 deletion lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function mangle_properties(ast, options) {
var mangled = cache.get(name);
if (!mangled) {
if (debug) {
// debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_.
// debug mode: use a prefix and suffix to preserve readability, e.g. o.foo ---> o._$foo$NNN_.
var debug_mangled = "_$" + name + "$" + debug_suffix + "_";
if (can_mangle(debug_mangled)) mangled = debug_mangled;
}
Expand Down
5 changes: 3 additions & 2 deletions test/compress/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ holes_and_undefined: {
}
}

constant_join: {
constant_join_1: {
options = {
evaluate: true,
side_effects: true,
strings: true,
unsafe: true,
}
Expand Down Expand Up @@ -57,7 +58,7 @@ constant_join: {
var c5 = [ boo() + bar() + "foo", 1, 2, 3, "bar", bar() + "foo" ].join();
var c6 = [ "1,2,,,foo,bar", baz() ].join();
var d = "foo-3bar-baz";
var e = [].join(foo + bar);
var e = (foo, bar, "");
var f = "";
var g = "";
}
Expand Down
46 changes: 46 additions & 0 deletions test/compress/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
arithmetic: {
input: {
console.log(((1n + 0x2n) * (0o3n - -4n)) >> (5n - 6n));
}
expect_exact: "console.log((1n+0x2n)*(0o3n- -4n)>>5n-6n);"
expect_stdout: "42n"
node_version: ">=10"
}

minus_dot: {
input: {
console.log(typeof -42n.toString(), typeof (-42n).toString());
}
expect_exact: "console.log(typeof-42n.toString(),typeof(-42n).toString());"
expect_stdout: "number string"
node_version: ">=10"
}

evaluate: {
options = {
evaluate: true,
unsafe: true,
}
input: {
console.log((0xDEAD_BEEFn).toString(16));
}
expect: {
console.log(0xdeadbeefn.toString(16));
}
expect_stdout: "deadbeef"
node_version: ">=10"
}

Number: {
options = {
unsafe: true,
}
input: {
console.log(Number(-0xfeed_dead_beef_badn));
}
expect: {
console.log(+("" + -0xfeed_dead_beef_badn));
}
expect_stdout: "-1148098955808013200"
node_version: ">=10"
}
2 changes: 1 addition & 1 deletion test/compress/issue-269.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ issue_269_1: {
expect: {
var x = {};
console.log(
x + "", +x, !!x,
"" + x, +("" + x), !!x,
"", 0, false
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/compress/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ evaluate_3: {
console.log(1 + Number(x) + 2);
}
expect: {
console.log(+x + 3);
console.log(+("" + x) + 3);
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/ufuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var SUPPORT = function(matrix) {
}({
arrow: "a => 0;",
async: "async function f(){}",
bigint: "42n",
catch_omit_var: "try {} catch {}",
computed_key: "({[0]: 0});",
const_block: "var a; { const a = 0; }",
Expand Down Expand Up @@ -188,6 +189,12 @@ var VALUES = [
'"function"',
"this",
];
if (SUPPORT.bigint) VALUES = VALUES.concat([
"!0o644n",
"([3n][0] > 2)",
"(-42n).toString()",
"Number(0XDEADn << 16n | 0xbeefn)",
]);

var BINARY_OPS = [
" + ", // spaces needed to disambiguate with ++ cases (could otherwise cause syntax errors)
Expand Down

0 comments on commit 8bfd891

Please sign in to comment.