Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile conditional assigns to assign with ternary operand #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ function ast_mangle(ast, options) {
- join consecutive var declarations
- various optimizations for IFs:
- if (cond) foo(); else bar(); ==> cond?foo():bar();
- if (cond) a=foo() ==> a=cond?foo():a
- if (cond) foo(); ==> cond&&foo();
- if (foo) return bar(); else return baz(); ==> return foo?bar():baz(); // also for throw
- if (foo) return bar(); else something(); ==> {if(foo)return bar();something()}
Expand Down Expand Up @@ -615,7 +616,32 @@ function boolean_expr(expr) {

function make_conditional(c, t, e) {
var make_real_conditional = function() {
if (c[0] == "unary-prefix" && c[1] == "!") {
function justAssigns(a) { return a[0] == "assign"}
if (
justAssigns(t)
// if there's an else block, it must have the same target for assignment
&& (!e || (justAssigns(e) && deepEquals(t[2], e[2]) && t[1] === e[1]))
// if there's no else block, the assignment target appears twice in the result
// and therefore it must not be longer than one byte
&& (e || (t[2][0] == "name" && t[2][1].length == 1))) {
var target = t[2];
var ZERO_NODE = [ "num", 0 ];
var ONE_NODE = [ "num", 1 ];
var NO_OPS =
{ "true": target
, "+": ZERO_NODE
, "-": ZERO_NODE
, "*": ONE_NODE
, "/": ONE_NODE
}
var t_val = t[3];
// if there's no else block, just do a no-op in case the condition isn't fullfilled
var e_val = e?e[3]:NO_OPS[t[1]];
// don't return the result if we need a no-op but can't find one
if (e_val)
return [ "assign", t[1], target, [ "conditional", c, t_val, e_val ] ];
// yes, not "else". fallthrough in case the operator has no neutral element.
} if (c[0] == "unary-prefix" && c[1] == "!") {
return e ? [ "conditional", c[2], e, t ] : [ "binary", "||", c[2], t ];
} else {
return e ? [ "conditional", c, t, e ] : [ "binary", "&&", c, t ];
Expand Down Expand Up @@ -1758,6 +1784,28 @@ var MAP;
function AtTop(val) { this.v = val };
})();

function deepEquals(a, b) {
if (typeof a !== typeof b) return false;
switch (typeof a) {
case "object":
var keys = Object.keys(a);
var keysB = Object.keys(b);
if (keys.length !== keysB.length) return false;
for (var i=0; i<keys.length; i++) {
var key = keys[i];
if (keysB.indexOf(key) === -1) return false;
if (!deepEquals(a[key], b[key])) return false;
}
return true;
case "array":
if (a.length !== b.length) return false;
for (var i=0; i<a.length; i++)
if (!deepEquals(a[i], b[i])) return false;
return true;
}
return a === b;
}

/* -----[ Exports ]----- */

exports.ast_walker = ast_walker;
Expand Down