Compare commits
56 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1786c69070 | ||
|
|
95ef4d5377 | ||
|
|
04017215cc | ||
|
|
142bd1bd1a | ||
|
|
8cb509d50e | ||
|
|
baf4903aa7 | ||
|
|
35465d590e | ||
|
|
ccd91b9952 | ||
|
|
47a5e6e17a | ||
|
|
090ee895e1 | ||
|
|
1cd1a1e5ee | ||
|
|
1d835ac17d | ||
|
|
9e07ac4102 | ||
|
|
92d1391e5e | ||
|
|
b4ff6d0f2d | ||
|
|
9882a9f4af | ||
|
|
40f36b9e01 | ||
|
|
6e105c5ca6 | ||
|
|
af35cd32f2 | ||
|
|
7de8daa4b1 | ||
|
|
305a4bdcee | ||
|
|
3472cf1a90 | ||
|
|
6d4c0fa6fa | ||
|
|
3cca0d6249 | ||
|
|
12ac49b970 | ||
|
|
8c670cae93 | ||
|
|
0e3da27727 | ||
|
|
13cdc167a2 | ||
|
|
51803cdcb2 | ||
|
|
8fa470c17c | ||
|
|
90410f9fc3 | ||
|
|
ef3831437d | ||
|
|
171c544705 | ||
|
|
3c609e2f4a | ||
|
|
f0ae03ed39 | ||
|
|
31c6b45036 | ||
|
|
3ac533e644 | ||
|
|
38a46c86d7 | ||
|
|
0f0759ec15 | ||
|
|
7f501f9fed | ||
|
|
72844eb5a4 | ||
|
|
09d93cc6c8 | ||
|
|
dd1374aa8a | ||
|
|
fdf2e8c5b0 | ||
|
|
a9d934ab4e | ||
|
|
2a053710bd | ||
|
|
219aac6a84 | ||
|
|
2039185051 | ||
|
|
ad27c14202 | ||
|
|
a62b086184 | ||
|
|
335456cf77 | ||
|
|
d64d0b0bec | ||
|
|
3ac575f2e8 | ||
|
|
d33a3a3253 | ||
|
|
d7456a2dc2 | ||
|
|
d97672613d |
9
.github/workflows/ufuzz.yml
vendored
9
.github/workflows/ufuzz.yml
vendored
@@ -6,6 +6,7 @@ on:
|
||||
env:
|
||||
BASE_URL: https://api.github.com/repos/${{ github.repository }}
|
||||
CAUSE: ${{ github.event_name }}
|
||||
RUN_NUM: ${{ github.run_number }}
|
||||
TOKEN: ${{ github.token }}
|
||||
jobs:
|
||||
ufuzz:
|
||||
@@ -36,12 +37,8 @@ jobs:
|
||||
npm config set update-notifier false
|
||||
npm --version
|
||||
while !(npm install); do echo "'npm install' failed - retrying..."; done
|
||||
PERIOD=1800000
|
||||
if [[ $CAUSE == "schedule" ]]; then
|
||||
PERIOD=`node test/ufuzz/actions $BASE_URL $TOKEN`
|
||||
fi
|
||||
if (( $PERIOD == 0 )); then
|
||||
echo "too many jobs in queue - skipping..."
|
||||
node test/ufuzz/job $BASE_URL $TOKEN $RUN_NUM
|
||||
else
|
||||
node test/ufuzz/job $PERIOD
|
||||
node test/ufuzz/job 5000
|
||||
fi
|
||||
|
||||
@@ -688,6 +688,8 @@ to be `false` and all symbol names will be omitted.
|
||||
- `loops` (default: `true`) -- optimizations for `do`, `while` and `for` loops
|
||||
when we can statically determine the condition.
|
||||
|
||||
- `merge_vars` (default: `true`) -- combine and reuse variables.
|
||||
|
||||
- `negate_iife` (default: `true`) -- negate "Immediately-Called Function Expressions"
|
||||
where the return value is discarded, to avoid the parens that the
|
||||
code generator would insert.
|
||||
|
||||
13
bin/uglifyjs
13
bin/uglifyjs
@@ -342,7 +342,18 @@ function run() {
|
||||
}
|
||||
fatal(ex);
|
||||
} else if (output == "ast") {
|
||||
if (!options.compress && !options.mangle) result.ast.figure_out_scope({});
|
||||
if (!options.compress && !options.mangle) {
|
||||
var toplevel = result.ast;
|
||||
if (!(toplevel instanceof UglifyJS.AST_Toplevel)) {
|
||||
if (!(toplevel instanceof UglifyJS.AST_Statement)) toplevel = new UglifyJS.AST_SimpleStatement({
|
||||
body: toplevel,
|
||||
});
|
||||
toplevel = new UglifyJS.AST_Toplevel({
|
||||
body: [ toplevel ],
|
||||
});
|
||||
}
|
||||
toplevel.figure_out_scope({});
|
||||
}
|
||||
print(JSON.stringify(result.ast, function(key, value) {
|
||||
if (value) switch (key) {
|
||||
case "thedef":
|
||||
|
||||
44
lib/ast.js
44
lib/ast.js
@@ -412,33 +412,46 @@ var AST_With = DEFNODE("With", "expression", {
|
||||
|
||||
/* -----[ scope and functions ]----- */
|
||||
|
||||
var AST_Scope = DEFNODE("Scope", "cname enclosed uses_eval uses_with parent_scope functions variables make_def", {
|
||||
var AST_BlockScope = DEFNODE("BlockScope", "enclosed functions make_def parent_scope variables", {
|
||||
$documentation: "Base class for all statements introducing a lexical scope",
|
||||
$propdoc: {
|
||||
cname: "[integer/S] current index for mangling variables (used internally by the mangler)",
|
||||
enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
|
||||
uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
|
||||
uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
|
||||
parent_scope: "[AST_Scope?/S] link to the parent scope",
|
||||
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",
|
||||
},
|
||||
clone: function(deep) {
|
||||
var node = this._clone(deep);
|
||||
if (this.variables) node.variables = this.variables.clone();
|
||||
if (this.functions) node.functions = this.functions.clone();
|
||||
if (this.enclosed) node.enclosed = this.enclosed.slice();
|
||||
if (this.functions) node.functions = this.functions.clone();
|
||||
if (this.variables) node.variables = this.variables.clone();
|
||||
return node;
|
||||
},
|
||||
pinned: function() {
|
||||
return this.resolve().pinned();
|
||||
},
|
||||
resolve: function() {
|
||||
return this.parent_scope.resolve();
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.parent_scope == null) return;
|
||||
if (!(this.parent_scope instanceof AST_BlockScope)) throw new Error("parent_scope must be AST_BlockScope");
|
||||
if (!(this.resolve() instanceof AST_Scope)) throw new Error("must be contained within AST_Scope");
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
var AST_Scope = DEFNODE("Scope", "cname uses_eval uses_with", {
|
||||
$documentation: "Base class for all statements introducing a lexical scope",
|
||||
$propdoc: {
|
||||
cname: "[integer/S] current index for mangling variables (used internally by the mangler)",
|
||||
uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
|
||||
uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
|
||||
},
|
||||
pinned: function() {
|
||||
return this.uses_eval || this.uses_with;
|
||||
},
|
||||
_validate: function() {
|
||||
if (this.parent_scope != null) {
|
||||
if (!(this.parent_scope instanceof AST_Scope)) throw new Error("parent_scope must be AST_Scope");
|
||||
}
|
||||
},
|
||||
}, AST_Block);
|
||||
resolve: return_this,
|
||||
}, AST_BlockScope);
|
||||
|
||||
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
|
||||
$documentation: "The toplevel scope",
|
||||
@@ -631,6 +644,9 @@ var AST_Switch = DEFNODE("Switch", "expression", {
|
||||
},
|
||||
_validate: function() {
|
||||
must_be_expression(this, "expression");
|
||||
this.body.forEach(function(node) {
|
||||
if (!(node instanceof AST_SwitchBranch)) throw new Error("body must be AST_SwitchBranch[]");
|
||||
});
|
||||
},
|
||||
}, AST_Block);
|
||||
|
||||
@@ -700,7 +716,7 @@ var AST_Catch = DEFNODE("Catch", "argname", {
|
||||
_validate: function() {
|
||||
if (!(this.argname instanceof AST_SymbolCatch)) throw new Error("argname must be AST_SymbolCatch");
|
||||
},
|
||||
}, AST_Block);
|
||||
}, AST_BlockScope);
|
||||
|
||||
var AST_Finally = DEFNODE("Finally", null, {
|
||||
$documentation: "A `finally` node; only makes sense as part of a `try` statement"
|
||||
|
||||
649
lib/compress.js
649
lib/compress.js
@@ -73,6 +73,7 @@ function Compressor(options, false_by_default) {
|
||||
keep_fnames : false,
|
||||
keep_infinity : false,
|
||||
loops : !false_by_default,
|
||||
merge_vars : !false_by_default,
|
||||
negate_iife : !false_by_default,
|
||||
objects : !false_by_default,
|
||||
passes : 1,
|
||||
@@ -225,7 +226,8 @@ merge(Compressor.prototype, {
|
||||
// output and performance.
|
||||
descend(node, this);
|
||||
var opt = node.optimize(this);
|
||||
if (is_scope && opt === node) {
|
||||
if (is_scope && opt === node && !this.has_directive("use asm") && !opt.pinned()) {
|
||||
opt.merge_variables(this);
|
||||
opt.drop_unused(this);
|
||||
descend(opt, this);
|
||||
}
|
||||
@@ -670,15 +672,6 @@ merge(Compressor.prototype, {
|
||||
exp.left.definition().bool_fn++;
|
||||
}
|
||||
});
|
||||
def(AST_Case, function(tw) {
|
||||
push(tw);
|
||||
this.expression.walk(tw);
|
||||
pop(tw);
|
||||
push(tw);
|
||||
walk_body(this, tw);
|
||||
pop(tw);
|
||||
return true;
|
||||
});
|
||||
def(AST_Conditional, function(tw) {
|
||||
this.condition.walk(tw);
|
||||
push(tw);
|
||||
@@ -689,12 +682,6 @@ merge(Compressor.prototype, {
|
||||
pop(tw);
|
||||
return true;
|
||||
});
|
||||
def(AST_Default, function(tw, descend) {
|
||||
push(tw);
|
||||
descend();
|
||||
pop(tw);
|
||||
return true;
|
||||
});
|
||||
def(AST_Defun, function(tw, descend, compressor) {
|
||||
var id = this.name.definition().id;
|
||||
if (tw.defun_visited[id]) return true;
|
||||
@@ -784,6 +771,7 @@ merge(Compressor.prototype, {
|
||||
var j = fn.argnames.indexOf(arg);
|
||||
return (j < 0 ? value : iife.args[j]) || make_node(AST_Undefined, iife);
|
||||
};
|
||||
d.fixed.assigns = [ arg ];
|
||||
} else {
|
||||
d.fixed = false;
|
||||
}
|
||||
@@ -820,6 +808,27 @@ merge(Compressor.prototype, {
|
||||
pop(tw);
|
||||
return true;
|
||||
});
|
||||
def(AST_Switch, function(tw) {
|
||||
this.expression.walk(tw);
|
||||
var first = true;
|
||||
this.body.forEach(function(branch) {
|
||||
if (branch instanceof AST_Default) return;
|
||||
branch.expression.walk(tw);
|
||||
if (first) {
|
||||
first = false;
|
||||
push(tw);
|
||||
}
|
||||
})
|
||||
if (!first) pop(tw);
|
||||
walk_body(this, tw);
|
||||
return true;
|
||||
});
|
||||
def(AST_SwitchBranch, function(tw) {
|
||||
push(tw);
|
||||
walk_body(this, tw);
|
||||
pop(tw);
|
||||
return true;
|
||||
});
|
||||
def(AST_SymbolCatch, function() {
|
||||
this.definition().fixed = false;
|
||||
});
|
||||
@@ -1028,18 +1037,6 @@ merge(Compressor.prototype, {
|
||||
return false;
|
||||
}
|
||||
|
||||
function find_variable(compressor, name) {
|
||||
var scope, i = 0;
|
||||
while (scope = compressor.parent(i++)) {
|
||||
if (scope instanceof AST_Scope) break;
|
||||
if (scope instanceof AST_Catch) {
|
||||
scope = scope.argname.definition().scope;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return scope.find_variable(name);
|
||||
}
|
||||
|
||||
function make_node(ctor, orig, props) {
|
||||
if (!props) props = {};
|
||||
if (orig) {
|
||||
@@ -1450,9 +1447,15 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
|
||||
function handle_custom_scan_order(node, tt) {
|
||||
// Scan object only in a for-in statement
|
||||
if (node instanceof AST_ForIn) {
|
||||
node.object = node.object.transform(tt);
|
||||
abort = true;
|
||||
return node;
|
||||
}
|
||||
// Skip (non-executed) functions
|
||||
if (node instanceof AST_Scope) return node;
|
||||
// Scan case expressions first in a switch statement
|
||||
// Scan first case expression only in a switch statement
|
||||
if (node instanceof AST_Switch) {
|
||||
node.expression = node.expression.transform(tt);
|
||||
for (var i = 0; !abort && i < node.body.length; i++) {
|
||||
@@ -1485,7 +1488,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
if (node instanceof AST_Debugger) return true;
|
||||
if (node instanceof AST_Defun) return funarg && lhs.name === node.name.name;
|
||||
if (node instanceof AST_IterationStatement) return !(node instanceof AST_For);
|
||||
if (node instanceof AST_DWLoop) return true;
|
||||
if (node instanceof AST_LoopControl) return true;
|
||||
if (node instanceof AST_Try) return true;
|
||||
if (node instanceof AST_With) return true;
|
||||
@@ -2113,25 +2116,31 @@ merge(Compressor.prototype, {
|
||||
eliminate_spurious_blocks(stat.body);
|
||||
[].splice.apply(statements, [i, 1].concat(stat.body));
|
||||
i += stat.body.length;
|
||||
} else if (stat instanceof AST_EmptyStatement) {
|
||||
CHANGED = true;
|
||||
statements.splice(i, 1);
|
||||
} else if (stat instanceof AST_Directive) {
|
||||
if (!member(stat.value, seen_dirs)) {
|
||||
i++;
|
||||
seen_dirs.push(stat.value);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if (stat instanceof AST_Directive) {
|
||||
if (member(stat.value, seen_dirs)) {
|
||||
CHANGED = true;
|
||||
statements.splice(i, 1);
|
||||
continue;
|
||||
}
|
||||
} else i++;
|
||||
seen_dirs.push(stat.value);
|
||||
}
|
||||
if (stat instanceof AST_EmptyStatement) {
|
||||
CHANGED = true;
|
||||
statements.splice(i, 1);
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
function handle_if_return(statements, compressor) {
|
||||
var self = compressor.self();
|
||||
var parent = compressor.parent();
|
||||
var in_lambda = self instanceof AST_Lambda;
|
||||
var in_lambda = last_of(function(node) {
|
||||
return node instanceof AST_Lambda;
|
||||
});
|
||||
var in_iife = in_lambda && parent && parent.TYPE == "Call";
|
||||
var multiple_if_returns = has_multiple_if_returns(statements);
|
||||
for (var i = statements.length; --i >= 0;) {
|
||||
@@ -2234,7 +2243,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
//---
|
||||
// if (foo()) return x; return y; => return foo() ? x : y;
|
||||
if ((in_bool || value) && !stat.alternative && next instanceof AST_Return) {
|
||||
if (!stat.alternative && next instanceof AST_Return) {
|
||||
CHANGED = true;
|
||||
stat = stat.clone();
|
||||
stat.alternative = next;
|
||||
@@ -2295,12 +2304,39 @@ merge(Compressor.prototype, {
|
||||
return !value || value instanceof AST_UnaryPrefix && value.operator == "void";
|
||||
}
|
||||
|
||||
function is_last_statement(body, stat) {
|
||||
var index = body.lastIndexOf(stat);
|
||||
if (index < 0) return false;
|
||||
while (++index < body.length) {
|
||||
if (!is_declaration(body[index])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function last_of(predicate) {
|
||||
var block = self, stat, level = 0;
|
||||
do {
|
||||
do {
|
||||
if (predicate(block)) return true;
|
||||
block = compressor.parent(level++);
|
||||
} while (block instanceof AST_If && (stat = block));
|
||||
} while ((block instanceof AST_BlockStatement || block instanceof AST_Scope)
|
||||
&& is_last_statement(block.body, stat));
|
||||
}
|
||||
|
||||
function match_target(target) {
|
||||
return last_of(function(node) {
|
||||
return node === target;
|
||||
});
|
||||
}
|
||||
|
||||
function can_merge_flow(ab) {
|
||||
if (!ab) return false;
|
||||
var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab) : null;
|
||||
return ab instanceof AST_Return && in_lambda && is_return_void(ab.value)
|
||||
|| ab instanceof AST_Continue && self === loop_body(lct)
|
||||
|| ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct;
|
||||
if (ab instanceof AST_Return) return in_lambda && is_return_void(ab.value);
|
||||
if (!(ab instanceof AST_LoopControl)) return false;
|
||||
var lct = compressor.loopcontrol_target(ab);
|
||||
if (ab instanceof AST_Continue) return match_target(loop_body(lct));
|
||||
if (lct instanceof AST_IterationStatement) return false;
|
||||
return match_target(lct);
|
||||
}
|
||||
|
||||
function extract_functions() {
|
||||
@@ -2327,20 +2363,14 @@ merge(Compressor.prototype, {
|
||||
|
||||
function next_index(i) {
|
||||
for (var j = i + 1; j < statements.length; j++) {
|
||||
var stat = statements[j];
|
||||
if (!(stat instanceof AST_Var && declarations_only(stat))) {
|
||||
break;
|
||||
}
|
||||
if (!is_declaration(statements[j])) break;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
function prev_index(i) {
|
||||
for (var j = i; --j >= 0;) {
|
||||
var stat = statements[j];
|
||||
if (!(stat instanceof AST_Var && declarations_only(stat))) {
|
||||
break;
|
||||
}
|
||||
if (!is_declaration(statements[j])) break;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
@@ -2383,6 +2413,10 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
}
|
||||
|
||||
function is_declaration(stat) {
|
||||
return stat instanceof AST_Defun || stat instanceof AST_Var && declarations_only(stat);
|
||||
}
|
||||
|
||||
function sequencesize(statements, compressor) {
|
||||
if (statements.length < 2) return;
|
||||
var seq = [], n = 0;
|
||||
@@ -2399,8 +2433,7 @@ merge(Compressor.prototype, {
|
||||
var body = stat.body;
|
||||
if (seq.length > 0) body = body.drop_side_effect_free(compressor);
|
||||
if (body) merge_sequence(seq, body);
|
||||
} else if (stat instanceof AST_Definitions && declarations_only(stat)
|
||||
|| stat instanceof AST_Defun) {
|
||||
} else if (is_declaration(stat)) {
|
||||
statements[n++] = stat;
|
||||
} else {
|
||||
push_seq();
|
||||
@@ -2871,9 +2904,12 @@ merge(Compressor.prototype, {
|
||||
var fixed = this.fixed_value();
|
||||
if (!fixed) return true;
|
||||
this._dot_throw = return_true;
|
||||
var result = fixed._dot_throw(compressor);
|
||||
delete this._dot_throw;
|
||||
return result;
|
||||
if (fixed._dot_throw(compressor)) {
|
||||
delete this._dot_throw;
|
||||
return true;
|
||||
}
|
||||
this._dot_throw = return_false;
|
||||
return false;
|
||||
});
|
||||
def(AST_UnaryPrefix, function() {
|
||||
return this.operator == "void";
|
||||
@@ -3068,7 +3104,12 @@ merge(Compressor.prototype, {
|
||||
(function(def) {
|
||||
def(AST_Node, return_false);
|
||||
def(AST_Assign, function(compressor) {
|
||||
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
|
||||
switch (this.operator) {
|
||||
case "+=":
|
||||
if (this.left.is_string(compressor)) return true;
|
||||
case "=":
|
||||
return this.right.is_string(compressor);
|
||||
}
|
||||
});
|
||||
def(AST_Binary, function(compressor) {
|
||||
return this.operator == "+" &&
|
||||
@@ -3393,7 +3434,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
}
|
||||
var value = node._eval(compressor, ignore_side_effects, cached, depth);
|
||||
if (value === node) return this;
|
||||
if (typeof value == "object") return this;
|
||||
modified(lhs);
|
||||
return value;
|
||||
});
|
||||
@@ -3857,7 +3898,8 @@ merge(Compressor.prototype, {
|
||||
if (is_undeclared_ref(expr) && global_pure_fns[expr.name]) return true;
|
||||
if (expr instanceof AST_Dot && is_undeclared_ref(expr.expression)) {
|
||||
var static_fn = static_fns[expr.expression.name];
|
||||
return static_fn && static_fn[expr.property];
|
||||
return static_fn && (static_fn[expr.property]
|
||||
|| expr.expression.name == "Math" && expr.property == "random");
|
||||
}
|
||||
}
|
||||
return this.pure || !compressor.pure_funcs(this);
|
||||
@@ -4123,13 +4165,7 @@ merge(Compressor.prototype, {
|
||||
var scopes = [];
|
||||
self.walk(new TreeWalker(function(node, descend) {
|
||||
if (!result) return true;
|
||||
if (node instanceof AST_Catch) {
|
||||
scopes.push(node.argname.scope);
|
||||
descend();
|
||||
scopes.pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
if (node instanceof AST_BlockScope) {
|
||||
if (node === self) return;
|
||||
scopes.push(node);
|
||||
descend();
|
||||
@@ -4137,14 +4173,14 @@ merge(Compressor.prototype, {
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
if (self.inlined) {
|
||||
if (self.inlined || node.redef) {
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
if (self.variables.has(node.name)) return true;
|
||||
var def = node.definition();
|
||||
if (member(def.scope, scopes)) return true;
|
||||
if (scope) {
|
||||
if (scope && !def.redefined()) {
|
||||
var scope_def = scope.find_variable(node);
|
||||
if (def.undeclared ? !scope_def : scope_def === def) {
|
||||
result = "f";
|
||||
@@ -4284,11 +4320,267 @@ merge(Compressor.prototype, {
|
||||
return self;
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("merge_variables", function(compressor) {
|
||||
if (!compressor.option("merge_vars")) return;
|
||||
var self = this, segment = {}, root;
|
||||
var first = [], last = [], index = 0;
|
||||
var declarations = new Dictionary();
|
||||
var references = Object.create(null);
|
||||
var prev = Object.create(null);
|
||||
var tw = new TreeWalker(function(node, descend) {
|
||||
if (node instanceof AST_Assign) {
|
||||
var sym = node.left;
|
||||
if (!(sym instanceof AST_SymbolRef)) return;
|
||||
if (node.operator != "=") mark(sym, true, false);
|
||||
node.right.walk(tw);
|
||||
mark(sym, false, true);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Binary) {
|
||||
if (!lazy_op[node.operator]) return;
|
||||
node.left.walk(tw);
|
||||
push();
|
||||
node.right.walk(tw);
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Conditional) {
|
||||
node.condition.walk(tw);
|
||||
push();
|
||||
node.consequent.walk(tw);
|
||||
pop();
|
||||
push();
|
||||
node.alternative.walk(tw);
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_For) {
|
||||
if (node.init) node.init.walk(tw);
|
||||
push();
|
||||
segment.block = node;
|
||||
segment.loop = true;
|
||||
if (node.condition) node.condition.walk(tw);
|
||||
node.body.walk(tw);
|
||||
if (node.step) node.step.walk(tw);
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_ForIn) {
|
||||
node.object.walk(tw);
|
||||
push();
|
||||
segment.block = node;
|
||||
segment.loop = true;
|
||||
node.init.walk(tw);
|
||||
node.body.walk(tw);
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_If) {
|
||||
node.condition.walk(tw);
|
||||
push();
|
||||
node.body.walk(tw);
|
||||
pop();
|
||||
if (node.alternative) {
|
||||
push();
|
||||
node.alternative.walk(tw);
|
||||
pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_IterationStatement) {
|
||||
push();
|
||||
segment.block = node;
|
||||
segment.loop = true;
|
||||
descend();
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_LabeledStatement) {
|
||||
push();
|
||||
segment.block = node;
|
||||
node.body.walk(tw);
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
push();
|
||||
segment.block = node;
|
||||
if (node === self) root = segment;
|
||||
if (node instanceof AST_Lambda) {
|
||||
if (node.name) references[node.name.definition().id] = false;
|
||||
if (node.uses_arguments && !tw.has_directive("use strict")) node.argnames.forEach(function(node) {
|
||||
references[node.definition().id] = false;
|
||||
});
|
||||
}
|
||||
descend();
|
||||
pop();
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Switch) {
|
||||
node.expression.walk(tw);
|
||||
var save = segment;
|
||||
node.body.forEach(function(branch) {
|
||||
if (branch instanceof AST_Default) return;
|
||||
branch.expression.walk(tw);
|
||||
if (save === segment) push();
|
||||
});
|
||||
segment = save;
|
||||
node.body.forEach(function(branch) {
|
||||
push();
|
||||
walk_body(branch, tw);
|
||||
pop();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolFunarg) {
|
||||
if (!node.__unused) mark(node, false, true);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
mark(node, true, false);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Try) {
|
||||
push();
|
||||
segment.block = node;
|
||||
walk_body(node, tw);
|
||||
pop();
|
||||
if (node.bcatch) {
|
||||
var def = node.bcatch.argname.definition();
|
||||
references[def.id] = false;
|
||||
if (def = def.redefined()) references[def.id] = false;
|
||||
push();
|
||||
if (node.bfinally) segment.block = node.bcatch;
|
||||
walk_body(node.bcatch, tw);
|
||||
pop();
|
||||
}
|
||||
if (node.bfinally) node.bfinally.walk(tw);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Unary) {
|
||||
if (!unary_arithmetic[node.operator]) return;
|
||||
var sym = node.expression;
|
||||
if (!(sym instanceof AST_SymbolRef)) return;
|
||||
mark(sym, true, true);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_VarDef) {
|
||||
if (node.value) {
|
||||
node.value.walk(tw);
|
||||
mark(node.name, false, true);
|
||||
} else {
|
||||
var id = node.name.definition().id;
|
||||
if (!(id in references)) {
|
||||
declarations.add(id, node.name);
|
||||
} else if (references[id]) {
|
||||
references[id].push(node.name);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
tw.directives = Object.create(compressor.directives);
|
||||
self.walk(tw);
|
||||
var merged = Object.create(null);
|
||||
while (first.length && last.length) {
|
||||
var head = first.pop();
|
||||
var def = head.definition;
|
||||
if (!(def.id in prev)) continue;
|
||||
if (!references[def.id]) continue;
|
||||
var head_refs = {
|
||||
start: references[def.id].start,
|
||||
};
|
||||
while (def.id in merged) def = merged[def.id];
|
||||
head_refs.end = references[def.id].end;
|
||||
var skipped = [];
|
||||
do {
|
||||
var tail = last.pop();
|
||||
if (!tail) continue;
|
||||
if (tail.index > head.index) continue;
|
||||
var id = tail.definition.id;
|
||||
var tail_refs = references[id];
|
||||
if (!tail_refs) continue;
|
||||
if (head_refs.start.block !== tail_refs.start.block
|
||||
|| !mergeable(head_refs, tail_refs)
|
||||
|| head_refs.start.loop && !mergeable(tail_refs, head_refs)) {
|
||||
skipped.unshift(tail);
|
||||
continue;
|
||||
}
|
||||
var orig = [], refs = [];
|
||||
tail_refs.forEach(function(sym) {
|
||||
sym.thedef = def;
|
||||
sym.name = def.name;
|
||||
if (sym instanceof AST_SymbolRef) {
|
||||
refs.push(sym);
|
||||
} else {
|
||||
orig.push(sym);
|
||||
}
|
||||
});
|
||||
def.orig = orig.concat(def.orig);
|
||||
def.references = refs.concat(def.references);
|
||||
def.fixed = tail.definition.fixed && def.fixed;
|
||||
merged[id] = def;
|
||||
break;
|
||||
} while (last.length);
|
||||
if (skipped.length) last = last.concat(skipped);
|
||||
}
|
||||
|
||||
function push() {
|
||||
segment = Object.create(segment);
|
||||
}
|
||||
|
||||
function pop() {
|
||||
segment = Object.getPrototypeOf(segment);
|
||||
}
|
||||
|
||||
function mark(sym, read, write) {
|
||||
var def = sym.definition();
|
||||
if (def.id in references) {
|
||||
var refs = references[def.id];
|
||||
if (!refs) return;
|
||||
if (refs.start.block !== segment.block) return references[def.id] = false;
|
||||
refs.push(sym);
|
||||
refs.end = segment;
|
||||
if (def.id in prev) {
|
||||
last[prev[def.id]] = null;
|
||||
} else if (!read) {
|
||||
return;
|
||||
}
|
||||
} else if (self.variables.get(def.name) !== def || compressor.exposed(def) || sym.name == "arguments") {
|
||||
return references[def.id] = false;
|
||||
} else {
|
||||
var refs = declarations.get(def.id) || [];
|
||||
refs.push(sym);
|
||||
references[def.id] = refs;
|
||||
if (!read) {
|
||||
refs.start = segment;
|
||||
return first.push({
|
||||
index: index++,
|
||||
definition: def,
|
||||
});
|
||||
}
|
||||
if (segment.block !== self) return references[def.id] = false;
|
||||
refs.start = root;
|
||||
}
|
||||
prev[def.id] = last.length;
|
||||
last.push({
|
||||
index: index++,
|
||||
definition: def,
|
||||
});
|
||||
}
|
||||
|
||||
function must_visit(base, segment) {
|
||||
return base === segment || base.isPrototypeOf(segment);
|
||||
}
|
||||
|
||||
function mergeable(head, tail) {
|
||||
return must_visit(head.start, head.end) || must_visit(head.start, tail.start);
|
||||
}
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
|
||||
if (!compressor.option("unused")) return;
|
||||
if (compressor.has_directive("use asm")) return;
|
||||
var self = this;
|
||||
if (self.pinned()) return;
|
||||
var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs;
|
||||
var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars;
|
||||
var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node, props) {
|
||||
@@ -4354,7 +4646,9 @@ merge(Compressor.prototype, {
|
||||
return true; // don't go in nested scopes
|
||||
}
|
||||
if (node instanceof AST_SymbolFunarg && scope === self) {
|
||||
var_defs_by_id.add(node.definition().id, node);
|
||||
var node_def = node.definition();
|
||||
var_defs_by_id.add(node_def.id, node);
|
||||
assignments.add(node_def.id, node);
|
||||
}
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
node.definitions.forEach(function(def) {
|
||||
@@ -4379,6 +4673,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return scan_ref_scoped(node, descend, true);
|
||||
});
|
||||
tw.directives = Object.create(compressor.directives);
|
||||
self.walk(tw);
|
||||
// pass 2: for every used symbol we need to walk its
|
||||
// initialization code to figure out if it uses other
|
||||
@@ -4467,6 +4762,13 @@ merge(Compressor.prototype, {
|
||||
}));
|
||||
}
|
||||
}
|
||||
} else if (node instanceof AST_UnaryPostfix
|
||||
&& node.expression instanceof AST_SymbolRef
|
||||
&& indexOf_assign(node.expression.definition(), node) < 0) {
|
||||
return make_node(AST_UnaryPrefix, node, {
|
||||
operator: "+",
|
||||
expression: node.expression
|
||||
});
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_Call) calls_to_drop_args.push(node);
|
||||
@@ -4487,14 +4789,16 @@ merge(Compressor.prototype, {
|
||||
var trim = compressor.drop_fargs(node, parent);
|
||||
for (var a = node.argnames, i = a.length; --i >= 0;) {
|
||||
var sym = a[i];
|
||||
if (!(sym.definition().id in in_use_ids)) {
|
||||
var def = sym.definition();
|
||||
if (def.id in in_use_ids) {
|
||||
trim = false;
|
||||
if (indexOf_assign(def, sym) < 0) sym.__unused = null;
|
||||
} else {
|
||||
sym.__unused = true;
|
||||
if (trim) {
|
||||
log(sym, "Dropping unused function argument {name}");
|
||||
a.pop();
|
||||
}
|
||||
} else {
|
||||
trim = false;
|
||||
}
|
||||
}
|
||||
fns_with_marked_args.push(node);
|
||||
@@ -4539,14 +4843,16 @@ merge(Compressor.prototype, {
|
||||
&& !compressor.option("ie8")
|
||||
&& var_defs.length == 1
|
||||
&& sym.assignments == 0
|
||||
&& def.value === def.name.fixed_value()
|
||||
&& def.value instanceof AST_Function
|
||||
&& (sym.references.length ? all(sym.references, function(ref) {
|
||||
return def.value === ref.fixed_value();
|
||||
}) : def.value === def.name.fixed_value())
|
||||
&& (!def.value.name || (old_def = def.value.name.definition()).assignments == 0
|
||||
&& (old_def.name == def.name.name || all(old_def.references, function(ref) {
|
||||
return ref.scope.find_variable(def.name) === def.name.definition();
|
||||
})))
|
||||
&& can_rename(def.value, def.name.name)
|
||||
&& (!compressor.has_directive("use strict") || parent instanceof AST_Scope)) {
|
||||
&& can_declare_defun()
|
||||
&& can_rename(def.value, def.name.name)) {
|
||||
AST_Node.warn("Declaring {name} as function [{file}:{line},{col}]", template(def.name));
|
||||
var defun = make_node(AST_Defun, def, def.value);
|
||||
defun.name = make_node(AST_SymbolDefun, def.name, def.name);
|
||||
@@ -4602,6 +4908,13 @@ merge(Compressor.prototype, {
|
||||
var def = fn.variables.get(name);
|
||||
return !def || fn.name && def === fn.name.definition();
|
||||
}
|
||||
|
||||
function can_declare_defun() {
|
||||
if (compressor.has_directive("use strict")) return parent instanceof AST_Scope;
|
||||
return parent instanceof AST_Block
|
||||
|| parent instanceof AST_For && parent.init === node
|
||||
|| parent instanceof AST_If;
|
||||
}
|
||||
});
|
||||
switch (head.length) {
|
||||
case 0:
|
||||
@@ -4855,6 +5168,13 @@ merge(Compressor.prototype, {
|
||||
if (!(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
if (node.scope !== node_def.scope) {
|
||||
var redef = node_def.redefined();
|
||||
if (redef && !(redef.id in in_use_ids)) {
|
||||
in_use_ids[redef.id] = true;
|
||||
in_use.push(redef);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (track_assigns(node_def, node)) add_assigns(node_def, node);
|
||||
return true;
|
||||
@@ -5083,7 +5403,7 @@ merge(Compressor.prototype, {
|
||||
process_boolean_returns(this, compressor);
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("var_names", function() {
|
||||
AST_BlockScope.DEFMETHOD("var_names", function() {
|
||||
var var_names = this._var_names;
|
||||
if (!var_names) {
|
||||
this._var_names = var_names = Object.create(null);
|
||||
@@ -6154,10 +6474,10 @@ merge(Compressor.prototype, {
|
||||
var side_effects = [];
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
var trim = i >= fn.argnames.length;
|
||||
if (trim || fn.argnames[i].__unused) {
|
||||
if (trim || "__unused" in fn.argnames[i]) {
|
||||
var node = args[i].drop_side_effect_free(compressor);
|
||||
if (drop_fargs) {
|
||||
fn.argnames.splice(i, 1);
|
||||
if (drop_fargs && (trim || fn.argnames[i].__unused)) {
|
||||
if (!trim) fn.argnames.splice(i, 1);
|
||||
args.splice(i, 1);
|
||||
if (node) side_effects.push(node);
|
||||
i--;
|
||||
@@ -6168,15 +6488,14 @@ merge(Compressor.prototype, {
|
||||
side_effects = [];
|
||||
} else if (!trim) {
|
||||
if (side_effects.length) {
|
||||
node = make_sequence(call, side_effects);
|
||||
args[pos++] = make_sequence(call, side_effects);
|
||||
side_effects = [];
|
||||
} else {
|
||||
node = make_node(AST_Number, args[i], {
|
||||
args[pos++] = make_node(AST_Number, args[i], {
|
||||
value: 0
|
||||
});
|
||||
continue;
|
||||
}
|
||||
args[pos++] = node;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
side_effects.push(args[i]);
|
||||
@@ -6429,7 +6748,7 @@ merge(Compressor.prototype, {
|
||||
if (self.args.length == 0) return make_node(AST_Function, self, {
|
||||
argnames: [],
|
||||
body: []
|
||||
});
|
||||
}).init_scope_vars(exp.scope);
|
||||
if (all(self.args, function(x) {
|
||||
return x instanceof AST_String;
|
||||
})) {
|
||||
@@ -6497,7 +6816,7 @@ merge(Compressor.prototype, {
|
||||
&& !fn.pinned()
|
||||
&& !(fn.name && fn instanceof AST_Function)
|
||||
&& (exp === fn || !recursive_ref(compressor, def = exp.definition())
|
||||
&& fn.is_constant_expression(compressor.find_parent(AST_Scope)))
|
||||
&& fn.is_constant_expression(compressor.find_parent(AST_BlockScope)))
|
||||
&& (value = can_flatten_body(stat))
|
||||
&& !fn.contains_this()) {
|
||||
var replacing = exp === fn || compressor.option("unused") && def.references.length - def.replaced == 1;
|
||||
@@ -6523,7 +6842,7 @@ merge(Compressor.prototype, {
|
||||
return arg;
|
||||
})).optimize(compressor);
|
||||
node = maintain_this_binding(compressor, compressor.parent(), compressor.self(), node);
|
||||
if (replacing || best_of(compressor, self, node) === node) {
|
||||
if (replacing || best_of_expression(node, self) === node) {
|
||||
refs.forEach(function(ref) {
|
||||
var def = ref.definition();
|
||||
def.references.push(ref);
|
||||
@@ -6739,9 +7058,10 @@ merge(Compressor.prototype, {
|
||||
value: null
|
||||
}));
|
||||
}
|
||||
if (!value) return;
|
||||
var sym = make_node(AST_SymbolRef, name, name);
|
||||
def.references.push(sym);
|
||||
if (value) expressions.push(make_node(AST_Assign, self, {
|
||||
expressions.push(make_node(AST_Assign, self, {
|
||||
operator: "=",
|
||||
left: sym,
|
||||
right: value
|
||||
@@ -6762,7 +7082,12 @@ merge(Compressor.prototype, {
|
||||
var symbol = make_node(AST_SymbolVar, name, name);
|
||||
name.definition().orig.push(symbol);
|
||||
if (!value && in_loop) value = make_node(AST_Undefined, self);
|
||||
append_var(decls, expressions, symbol, value);
|
||||
if ("__unused" in name) {
|
||||
append_var(decls, expressions, symbol);
|
||||
if (value) expressions.push(value);
|
||||
} else {
|
||||
append_var(decls, expressions, symbol, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
decls.reverse();
|
||||
@@ -7065,10 +7390,9 @@ merge(Compressor.prototype, {
|
||||
var indexFns = makePredicate("indexOf lastIndexOf");
|
||||
var commutativeOperators = makePredicate("== === != !== * & | ^");
|
||||
function is_object(node) {
|
||||
while ((node = node.tail_node()) instanceof AST_SymbolRef) {
|
||||
node = node.fixed_value();
|
||||
if (!node) return false;
|
||||
}
|
||||
if (node instanceof AST_Assign) return node.operator == "=" && is_object(node.right);
|
||||
if (node instanceof AST_Sequence) return is_object(node.tail_node());
|
||||
if (node instanceof AST_SymbolRef) return is_object(node.fixed_value());
|
||||
return node instanceof AST_Array
|
||||
|| node instanceof AST_Lambda
|
||||
|| node instanceof AST_New
|
||||
@@ -7399,29 +7723,6 @@ merge(Compressor.prototype, {
|
||||
})
|
||||
});
|
||||
}
|
||||
// (x + "foo") + ("bar" + y) => (x + "foobar") + y
|
||||
if (self.left instanceof AST_Binary
|
||||
&& self.left.operator == "+"
|
||||
&& self.left.is_string(compressor)
|
||||
&& self.left.right instanceof AST_Constant
|
||||
&& self.right instanceof AST_Binary
|
||||
&& self.right.operator == "+"
|
||||
&& self.right.left instanceof AST_Constant
|
||||
&& self.right.is_string(compressor)) {
|
||||
self = make_node(AST_Binary, self, {
|
||||
operator: "+",
|
||||
left: make_node(AST_Binary, self.left, {
|
||||
operator: "+",
|
||||
left: self.left.left,
|
||||
right: make_node(AST_String, self.left.right, {
|
||||
value: "" + self.left.right.value + self.right.left.value,
|
||||
start: self.left.right.start,
|
||||
end: self.right.left.end
|
||||
})
|
||||
}),
|
||||
right: self.right.right
|
||||
});
|
||||
}
|
||||
// a + -b => a - b
|
||||
if (self.right instanceof AST_UnaryPrefix
|
||||
&& self.right.operator == "-"
|
||||
@@ -7483,14 +7784,15 @@ merge(Compressor.prototype, {
|
||||
associative = compressor.option("unsafe_math");
|
||||
// +a - b => a - b
|
||||
// a - +b => a - b
|
||||
if (self.operator != "+") {
|
||||
if (self.left instanceof AST_UnaryPrefix && self.left.operator == "+") {
|
||||
self.left = self.left.expression;
|
||||
if (self.operator != "+") [ "left", "right" ].forEach(function(operand) {
|
||||
var node = self[operand];
|
||||
if (node instanceof AST_UnaryPrefix && node.operator == "+") {
|
||||
var exp = node.expression;
|
||||
if (exp.is_boolean(compressor) || exp.is_number(compressor) || exp.is_string(compressor)) {
|
||||
self[operand] = exp;
|
||||
}
|
||||
}
|
||||
if (self.right instanceof AST_UnaryPrefix && self.right.operator == "+") {
|
||||
self.right = self.right.expression;
|
||||
}
|
||||
}
|
||||
});
|
||||
case "&":
|
||||
case "|":
|
||||
case "^":
|
||||
@@ -7779,9 +8081,11 @@ merge(Compressor.prototype, {
|
||||
single_use = false;
|
||||
} else if (recursive_ref(compressor, def)) {
|
||||
single_use = false;
|
||||
} else if (compressor.option("ie8") && fixed.name && def !== fixed.name.definition()) {
|
||||
} else if (fixed.name && fixed.name.definition() !== def) {
|
||||
single_use = false;
|
||||
} else if (def.scope !== self.scope || def.orig[0] instanceof AST_SymbolFunarg) {
|
||||
} else if (fixed.parent_scope !== self.scope
|
||||
|| !(self.scope instanceof AST_Scope)
|
||||
|| def.orig[0] instanceof AST_SymbolFunarg) {
|
||||
single_use = fixed.is_constant_expression(self.scope);
|
||||
if (single_use == "f") {
|
||||
var scope = self.scope;
|
||||
@@ -7929,7 +8233,7 @@ merge(Compressor.prototype, {
|
||||
|
||||
OPT(AST_Undefined, function(self, compressor) {
|
||||
if (compressor.option("unsafe_undefined")) {
|
||||
var undef = find_variable(compressor, "undefined");
|
||||
var undef = compressor.find_parent(AST_BlockScope).find_variable("undefined");
|
||||
if (undef) {
|
||||
var ref = make_node(AST_SymbolRef, self, {
|
||||
name : "undefined",
|
||||
@@ -7955,7 +8259,7 @@ merge(Compressor.prototype, {
|
||||
if (lhs && is_atomic(lhs, self)) return self;
|
||||
if (compressor.option("keep_infinity")
|
||||
&& !(lhs && !is_atomic(lhs, self))
|
||||
&& !find_variable(compressor, "Infinity"))
|
||||
&& !compressor.find_parent(AST_BlockScope).find_variable("Infinity"))
|
||||
return self;
|
||||
return make_node(AST_Binary, self, {
|
||||
operator: "/",
|
||||
@@ -7971,7 +8275,7 @@ merge(Compressor.prototype, {
|
||||
OPT(AST_NaN, function(self, compressor) {
|
||||
var lhs = is_lhs(compressor.self(), compressor.parent());
|
||||
if (lhs && !is_atomic(lhs, self)
|
||||
|| find_variable(compressor, "NaN")) {
|
||||
|| compressor.find_parent(AST_BlockScope).find_variable("NaN")) {
|
||||
return make_node(AST_Binary, self, {
|
||||
operator: "/",
|
||||
left: make_node(AST_Number, self, {
|
||||
@@ -8208,35 +8512,26 @@ merge(Compressor.prototype, {
|
||||
condition,
|
||||
consequent
|
||||
]).optimize(compressor);
|
||||
// x ? y.p : z.p => (x ? y : z).p
|
||||
// x ? y(a) : z(a) => (x ? y : z)(a)
|
||||
// x ? y.f(a) : z.f(a) => (x ? y : z).f(a)
|
||||
var combined = combine_tail(consequent, alternative, true);
|
||||
if (combined) return combined;
|
||||
// x ? y(a) : y(b) => y(x ? a : b)
|
||||
var arg_index;
|
||||
if (consequent instanceof AST_Call
|
||||
&& alternative.TYPE === consequent.TYPE
|
||||
&& consequent.args.length == alternative.args.length) {
|
||||
var arg_index = arg_diff();
|
||||
// x ? y(a) : z(a) => (x ? y : z)(a)
|
||||
if (arg_index == -1
|
||||
&& !(consequent.expression instanceof AST_PropAccess)
|
||||
&& !(alternative.expression instanceof AST_PropAccess)) {
|
||||
var node = consequent.clone();
|
||||
node.expression = make_node(AST_Conditional, self, {
|
||||
condition: condition,
|
||||
consequent: consequent.expression,
|
||||
alternative: alternative.expression
|
||||
});
|
||||
return node;
|
||||
}
|
||||
// x ? y(a) : y(b) => y(x ? a : b)
|
||||
if (arg_index >= 0
|
||||
&& consequent.expression.equivalent_to(alternative.expression)
|
||||
&& !condition.has_side_effects(compressor)
|
||||
&& !consequent.expression.has_side_effects(compressor)) {
|
||||
var node = consequent.clone();
|
||||
node.args[arg_index] = make_node(AST_Conditional, self, {
|
||||
condition: condition,
|
||||
consequent: consequent.args[arg_index],
|
||||
alternative: alternative.args[arg_index]
|
||||
});
|
||||
return node;
|
||||
}
|
||||
&& alternative.TYPE == consequent.TYPE
|
||||
&& (arg_index = arg_diff(consequent, alternative)) >= 0
|
||||
&& consequent.expression.equivalent_to(alternative.expression)
|
||||
&& !condition.has_side_effects(compressor)
|
||||
&& !consequent.expression.has_side_effects(compressor)) {
|
||||
var node = consequent.clone();
|
||||
node.args[arg_index] = make_node(AST_Conditional, self, {
|
||||
condition: condition,
|
||||
consequent: consequent.args[arg_index],
|
||||
alternative: alternative.args[arg_index]
|
||||
});
|
||||
return node;
|
||||
}
|
||||
// x ? (y ? a : b) : b => x && y ? a : b
|
||||
if (consequent instanceof AST_Conditional
|
||||
@@ -8437,10 +8732,12 @@ merge(Compressor.prototype, {
|
||||
&& node.expression.value);
|
||||
}
|
||||
|
||||
function arg_diff() {
|
||||
function arg_diff(consequent, alternative) {
|
||||
var a = consequent.args;
|
||||
var b = alternative.args;
|
||||
for (var i = 0, len = a.length; i < len; i++) {
|
||||
var len = a.length;
|
||||
if (len != b.length) return -2;
|
||||
for (var i = 0; i < len; i++) {
|
||||
if (!a[i].equivalent_to(b[i])) {
|
||||
for (var j = i + 1; j < len; j++) {
|
||||
if (!a[j].equivalent_to(b[j])) return -2;
|
||||
@@ -8451,6 +8748,32 @@ merge(Compressor.prototype, {
|
||||
return -1;
|
||||
}
|
||||
|
||||
function is_tail_equivalent(consequent, alternative) {
|
||||
if (consequent.TYPE != alternative.TYPE) return;
|
||||
if (consequent instanceof AST_Call) {
|
||||
if (arg_diff(consequent, alternative) != -1) return;
|
||||
return consequent.TYPE != "Call"
|
||||
|| !(consequent.expression instanceof AST_PropAccess
|
||||
|| alternative.expression instanceof AST_PropAccess)
|
||||
|| is_tail_equivalent(consequent.expression, alternative.expression);
|
||||
}
|
||||
if (consequent instanceof AST_Dot) return consequent.property == alternative.property;
|
||||
if (consequent instanceof AST_Sub) return consequent.property.equivalent_to(alternative.property);
|
||||
}
|
||||
|
||||
function combine_tail(consequent, alternative, top) {
|
||||
if (!is_tail_equivalent(consequent, alternative)) return !top && make_node(AST_Conditional, self, {
|
||||
condition: condition,
|
||||
consequent: consequent,
|
||||
alternative: alternative
|
||||
});
|
||||
var exp = combine_tail(consequent.expression, alternative.expression);
|
||||
if (!exp) return;
|
||||
var node = consequent.clone();
|
||||
node.expression = exp;
|
||||
return node;
|
||||
}
|
||||
|
||||
function can_shift_lhs_of_tail(node) {
|
||||
return node === node.tail_node() || all(node.expressions.slice(0, -1), function(expr) {
|
||||
return !expr.has_side_effects(compressor);
|
||||
@@ -8717,7 +9040,7 @@ merge(Compressor.prototype, {
|
||||
self.expression = make_node(AST_Function, self.expression, {
|
||||
argnames: [],
|
||||
body: []
|
||||
});
|
||||
}).init_scope_vars(exp.scope);
|
||||
break;
|
||||
case "Number":
|
||||
self.expression = make_node(AST_Number, self.expression, {
|
||||
|
||||
15
lib/parse.js
15
lib/parse.js
@@ -44,11 +44,14 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var void while with';
|
||||
var KEYWORDS_ATOM = 'false null true';
|
||||
var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface let long native package private protected public short static super synchronized this throws transient volatile yield'
|
||||
+ " " + KEYWORDS_ATOM + " " + KEYWORDS;
|
||||
var KEYWORDS_BEFORE_EXPRESSION = 'return new delete throw else case';
|
||||
var KEYWORDS = "break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var void while with";
|
||||
var KEYWORDS_ATOM = "false null true";
|
||||
var RESERVED_WORDS = [
|
||||
"abstract boolean byte char class double enum export extends final float goto implements import int interface let long native package private protected public short static super synchronized this throws transient volatile yield",
|
||||
KEYWORDS_ATOM,
|
||||
KEYWORDS,
|
||||
].join(" ");
|
||||
var KEYWORDS_BEFORE_EXPRESSION = "return new delete throw else case";
|
||||
|
||||
KEYWORDS = makePredicate(KEYWORDS);
|
||||
RESERVED_WORDS = makePredicate(RESERVED_WORDS);
|
||||
@@ -438,7 +441,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function() {
|
||||
var regex_allowed = S.regex_allowed;
|
||||
var i = find("*/", true);
|
||||
var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, '\n');
|
||||
var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, "\n");
|
||||
// update stream position
|
||||
forward(text.length /* doesn't count \r\n as 2 char while S.pos - i does */ + 2);
|
||||
S.comments_before.push(token("comment2", text, true));
|
||||
|
||||
130
lib/scope.js
130
lib/scope.js
@@ -59,13 +59,9 @@ function SymbolDef(id, scope, orig, init) {
|
||||
}
|
||||
|
||||
SymbolDef.prototype = {
|
||||
unmangleable: function(options) {
|
||||
return this.global && !options.toplevel
|
||||
|| this.undeclared
|
||||
|| !options.eval && this.scope.pinned()
|
||||
|| options.keep_fnames
|
||||
&& (this.orig[0] instanceof AST_SymbolLambda
|
||||
|| this.orig[0] instanceof AST_SymbolDefun);
|
||||
forEach: function(fn) {
|
||||
this.orig.forEach(fn);
|
||||
this.references.forEach(fn);
|
||||
},
|
||||
mangle: function(options) {
|
||||
var cache = options.cache && options.cache.props;
|
||||
@@ -85,7 +81,15 @@ SymbolDef.prototype = {
|
||||
},
|
||||
redefined: function() {
|
||||
return this.defun && this.defun.variables.get(this.name);
|
||||
}
|
||||
},
|
||||
unmangleable: function(options) {
|
||||
return this.global && !options.toplevel
|
||||
|| this.undeclared
|
||||
|| !options.eval && this.scope.pinned()
|
||||
|| options.keep_fnames
|
||||
&& (this.orig[0] instanceof AST_SymbolLambda
|
||||
|| this.orig[0] instanceof AST_SymbolDefun);
|
||||
},
|
||||
};
|
||||
|
||||
AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
|
||||
@@ -100,22 +104,18 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
|
||||
var next_def_id = 0;
|
||||
var scope = self.parent_scope = null;
|
||||
var tw = new TreeWalker(function(node, descend) {
|
||||
if (node instanceof AST_Catch) {
|
||||
var save_scope = scope;
|
||||
scope = new AST_Scope(node);
|
||||
scope.init_scope_vars(save_scope);
|
||||
descend();
|
||||
scope = save_scope;
|
||||
if (node instanceof AST_Defun) {
|
||||
node.name.walk(tw);
|
||||
walk_scope(function() {
|
||||
node.argnames.forEach(function(argname) {
|
||||
argname.walk(tw);
|
||||
});
|
||||
walk_body(node, tw);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
node.init_scope_vars(scope);
|
||||
var save_scope = scope;
|
||||
var save_defun = defun;
|
||||
defun = scope = node;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
defun = save_defun;
|
||||
if (node instanceof AST_BlockScope) {
|
||||
walk_scope(descend);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_With) {
|
||||
@@ -129,25 +129,41 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
|
||||
node.thedef = node;
|
||||
node.references = [];
|
||||
}
|
||||
if (node instanceof AST_SymbolDefun) {
|
||||
// This should be defined in the parent scope, as we encounter the
|
||||
// AST_Defun node before getting to its AST_Symbol.
|
||||
(node.scope = defun.parent_scope.resolve()).def_function(node, defun);
|
||||
if (node instanceof AST_SymbolCatch) {
|
||||
scope.def_variable(node).defun = defun;
|
||||
} else if (node instanceof AST_SymbolDefun) {
|
||||
defun.def_function(node, tw.parent());
|
||||
entangle(defun, scope);
|
||||
} else if (node instanceof AST_SymbolFunarg) {
|
||||
defun.def_variable(node);
|
||||
entangle(defun, scope);
|
||||
} else if (node instanceof AST_SymbolLambda) {
|
||||
var def = defun.def_function(node, node.name == "arguments" ? undefined : defun);
|
||||
if (options.ie8) def.defun = defun.parent_scope.resolve();
|
||||
} else if (node instanceof AST_SymbolVar) {
|
||||
defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined);
|
||||
if (defun !== scope) {
|
||||
node.mark_enclosed(options);
|
||||
var def = scope.find_variable(node);
|
||||
if (node.thedef !== def) {
|
||||
node.thedef = def;
|
||||
}
|
||||
node.reference(options);
|
||||
}
|
||||
} else if (node instanceof AST_SymbolCatch) {
|
||||
scope.def_variable(node).defun = defun;
|
||||
defun.def_variable(node, null);
|
||||
entangle(defun, scope);
|
||||
}
|
||||
|
||||
function walk_scope(descend) {
|
||||
node.init_scope_vars(scope);
|
||||
var save_defun = defun;
|
||||
var save_scope = scope;
|
||||
if (node instanceof AST_Scope) defun = node;
|
||||
scope = node;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
defun = save_defun;
|
||||
}
|
||||
|
||||
function entangle(defun, scope) {
|
||||
if (defun === scope) return;
|
||||
node.mark_enclosed(options);
|
||||
var def = scope.find_variable(node);
|
||||
if (node.thedef === def) return;
|
||||
node.thedef = def;
|
||||
def.orig.push(node);
|
||||
node.mark_enclosed(options);
|
||||
}
|
||||
});
|
||||
self.make_def = function(orig, init) {
|
||||
@@ -234,7 +250,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
|
||||
new_def = scope.def_variable(node);
|
||||
}
|
||||
old_def.defun = new_def.scope;
|
||||
old_def.orig.concat(old_def.references).forEach(function(node) {
|
||||
old_def.forEach(function(node) {
|
||||
node.redef = true;
|
||||
node.thedef = new_def;
|
||||
node.reference(options);
|
||||
});
|
||||
@@ -267,7 +284,7 @@ function init_scope_vars(scope, parent) {
|
||||
if (parent) scope.make_def = parent.make_def; // top-level tracking of SymbolDef instances
|
||||
}
|
||||
|
||||
AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope) {
|
||||
AST_BlockScope.DEFMETHOD("init_scope_vars", function(parent_scope) {
|
||||
init_scope_vars(this, parent_scope);
|
||||
});
|
||||
|
||||
@@ -279,6 +296,7 @@ AST_Lambda.DEFMETHOD("init_scope_vars", function(parent_scope) {
|
||||
start: this.start,
|
||||
end: this.end,
|
||||
}));
|
||||
return this;
|
||||
});
|
||||
|
||||
AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
|
||||
@@ -299,20 +317,20 @@ AST_Symbol.DEFMETHOD("reference", function(options) {
|
||||
this.mark_enclosed(options);
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("find_variable", function(name) {
|
||||
AST_BlockScope.DEFMETHOD("find_variable", function(name) {
|
||||
if (name instanceof AST_Symbol) name = name.name;
|
||||
return this.variables.get(name)
|
||||
|| (this.parent_scope && this.parent_scope.find_variable(name));
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("def_function", function(symbol, init) {
|
||||
AST_BlockScope.DEFMETHOD("def_function", function(symbol, init) {
|
||||
var def = this.def_variable(symbol, init);
|
||||
if (!def.init || def.init instanceof AST_Defun) def.init = init;
|
||||
this.functions.set(symbol.name, def);
|
||||
return def;
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("def_variable", function(symbol, init) {
|
||||
AST_BlockScope.DEFMETHOD("def_variable", function(symbol, init) {
|
||||
var def = this.variables.get(symbol.name);
|
||||
if (def) {
|
||||
def.orig.push(symbol);
|
||||
@@ -325,16 +343,10 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol, init) {
|
||||
return symbol.thedef = def;
|
||||
});
|
||||
|
||||
AST_Lambda.DEFMETHOD("resolve", return_this);
|
||||
AST_Scope.DEFMETHOD("resolve", function() {
|
||||
return this.parent_scope.resolve();
|
||||
});
|
||||
AST_Toplevel.DEFMETHOD("resolve", return_this);
|
||||
|
||||
function names_in_use(scope, options) {
|
||||
var names = scope.names_in_use;
|
||||
if (!names) {
|
||||
scope.names_in_use = names = Object.create(scope.mangled_names || null);
|
||||
scope.names_in_use = names = Object.create(null);
|
||||
scope.cname_holes = [];
|
||||
var cache = options.cache && options.cache.props;
|
||||
scope.enclosed.forEach(function(def) {
|
||||
@@ -352,7 +364,7 @@ function next_mangled_name(scope, options, def) {
|
||||
var holes = scope.cname_holes;
|
||||
var names = Object.create(null);
|
||||
var scopes = [ scope ];
|
||||
def.references.forEach(function(sym) {
|
||||
def.forEach(function(sym) {
|
||||
var scope = sym.scope;
|
||||
do {
|
||||
if (scopes.indexOf(scope) < 0) {
|
||||
@@ -368,7 +380,7 @@ function next_mangled_name(scope, options, def) {
|
||||
name = base54(holes[i]);
|
||||
if (names[name]) continue;
|
||||
holes.splice(i, 1);
|
||||
scope.names_in_use[name] = true;
|
||||
in_use[name] = true;
|
||||
return name;
|
||||
}
|
||||
while (true) {
|
||||
@@ -377,7 +389,7 @@ function next_mangled_name(scope, options, def) {
|
||||
if (!names[name]) break;
|
||||
holes.push(scope.cname);
|
||||
}
|
||||
scope.names_in_use[name] = true;
|
||||
in_use[name] = true;
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -418,7 +430,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
|
||||
var lname = -1;
|
||||
|
||||
if (options.cache && options.cache.props) {
|
||||
var mangled_names = this.mangled_names = Object.create(null);
|
||||
var mangled_names = names_in_use(this, options);
|
||||
options.cache.props.each(function(mangled_name) {
|
||||
mangled_names[mangled_name] = true;
|
||||
});
|
||||
@@ -490,12 +502,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) {
|
||||
|
||||
AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
|
||||
var cache = options.cache && options.cache.props;
|
||||
var avoid = Object.create(null);
|
||||
var avoid = Object.create(RESERVED_WORDS);
|
||||
options.reserved.forEach(to_avoid);
|
||||
this.globals.each(add_def);
|
||||
this.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_Scope) node.variables.each(add_def);
|
||||
if (node instanceof AST_SymbolCatch) add_def(node.definition());
|
||||
if (node instanceof AST_BlockScope) node.variables.each(add_def);
|
||||
}));
|
||||
return avoid;
|
||||
|
||||
@@ -519,15 +530,14 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
||||
var cname = 0;
|
||||
this.globals.each(rename);
|
||||
this.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_Scope) node.variables.each(rename);
|
||||
if (node instanceof AST_SymbolCatch) rename(node.definition());
|
||||
if (node instanceof AST_BlockScope) node.variables.each(rename);
|
||||
}));
|
||||
|
||||
function next_name() {
|
||||
var name;
|
||||
do {
|
||||
name = base54(cname++);
|
||||
} while (avoid[name] || RESERVED_WORDS[name]);
|
||||
} while (avoid[name]);
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -538,7 +548,7 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
||||
var redef = def.redefined();
|
||||
var name = redef ? redef.rename || redef.name : next_name();
|
||||
def.rename = name;
|
||||
def.orig.concat(def.references).forEach(function(sym) {
|
||||
def.forEach(function(sym) {
|
||||
if (sym.definition() === def) sym.name = name;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"description": "JavaScript parser, mangler/compressor and beautifier toolkit",
|
||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||
"license": "BSD-2-Clause",
|
||||
"version": "3.10.4",
|
||||
"version": "3.11.1",
|
||||
"engines": {
|
||||
"node": ">=0.8.0"
|
||||
},
|
||||
|
||||
@@ -95,7 +95,7 @@ asm_mixed: {
|
||||
return +sum;
|
||||
}
|
||||
function geometricMean(start, end) {
|
||||
return start |= 0, end |= 0, +exp(logSum(start, end) / (end - start | 0));
|
||||
return start |= 0, end |= 0, +exp(+logSum(start, end) / (end - start | 0));
|
||||
}
|
||||
var exp = stdlib.Math.exp, log = stdlib.Math.log, values = new stdlib.Float64Array(buffer);
|
||||
return { geometricMean: geometricMean };
|
||||
|
||||
@@ -62,18 +62,18 @@ collapse_vars_side_effects_1: {
|
||||
expect: {
|
||||
function f1() {
|
||||
var s = "abcdef", i = 2;
|
||||
console.log.bind(console)(s.charAt(i++), s.charAt(i++), s.charAt(4), 7);
|
||||
console.log.bind(console)(s.charAt(i++), s.charAt(+i), s.charAt(4), 7);
|
||||
}
|
||||
function f2() {
|
||||
var s = "abcdef", i = 2;
|
||||
console.log.bind(console)(s.charAt(i++), 5, s.charAt(i++), s.charAt(i++), 7);
|
||||
console.log.bind(console)(s.charAt(i++), 5, s.charAt(i++), s.charAt(+i), 7);
|
||||
}
|
||||
function f3() {
|
||||
var s = "abcdef",
|
||||
i = 2,
|
||||
log = console.log.bind(console),
|
||||
x = s.charAt(i++),
|
||||
y = s.charAt(i++);
|
||||
y = s.charAt(+i);
|
||||
log(x, s.charAt(4), y, 7);
|
||||
}
|
||||
function f4() {
|
||||
@@ -3073,7 +3073,6 @@ issue_2298: {
|
||||
expect: {
|
||||
!function() {
|
||||
(function() {
|
||||
0;
|
||||
try {
|
||||
!function(b) {
|
||||
(void 0)[1] = "foo";
|
||||
|
||||
@@ -238,6 +238,41 @@ concat_8: {
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
concat_9: {
|
||||
options = {
|
||||
booleans: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
strings: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var a = "foo";
|
||||
console.log(
|
||||
12 + (34 + a),
|
||||
null + (34 + a),
|
||||
12 + (null + a),
|
||||
false + (34 + a),
|
||||
12 + (false + a),
|
||||
"bar" + (34 + a),
|
||||
12 + ("bar" + a)
|
||||
);
|
||||
}
|
||||
expect: {
|
||||
var a = "foo";
|
||||
console.log(
|
||||
"1234" + a,
|
||||
"null34" + a,
|
||||
"12null" + a,
|
||||
!1 + (34 + a),
|
||||
12 + (!1 + a),
|
||||
"bar34" + a,
|
||||
"12bar" + a
|
||||
);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
issue_3689: {
|
||||
options = {
|
||||
strings: true,
|
||||
|
||||
@@ -783,6 +783,28 @@ cond_12: {
|
||||
}
|
||||
}
|
||||
|
||||
cond_13: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
}
|
||||
input: {
|
||||
x ? y(a) : z(a);
|
||||
x ? y.f(a) : z.f(a);
|
||||
x ? y.f(a) : z.g(a);
|
||||
x ? y.f()(a) : z.g()(a);
|
||||
x ? y.f.u(a) : z.g.u(a);
|
||||
x ? y.f().u(a) : z.g().u(a);
|
||||
}
|
||||
expect: {
|
||||
(x ? y : z)(a);
|
||||
(x ? y : z).f(a);
|
||||
x ? y.f(a) : z.g(a);
|
||||
(x ? y.f() : z.g())(a);
|
||||
(x ? y.f : z.g).u(a);
|
||||
(x ? y.f() : z.g()).u(a);
|
||||
}
|
||||
}
|
||||
|
||||
ternary_boolean_consequent: {
|
||||
options = {
|
||||
booleans: true,
|
||||
|
||||
@@ -1730,7 +1730,7 @@ chained_3: {
|
||||
expect: {
|
||||
console.log(function(a, b) {
|
||||
var c = b;
|
||||
b++;
|
||||
+b;
|
||||
return c;
|
||||
}(0, 2));
|
||||
}
|
||||
@@ -1997,7 +1997,7 @@ issue_3146_4: {
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_3192: {
|
||||
issue_3192_1: {
|
||||
options = {
|
||||
unused: true,
|
||||
}
|
||||
@@ -2025,6 +2025,26 @@ issue_3192: {
|
||||
]
|
||||
}
|
||||
|
||||
issue_3192_2: {
|
||||
options = {
|
||||
keep_fargs: "strict",
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
"use strict";
|
||||
(function(a) {
|
||||
console.log(a = "foo", arguments[0]);
|
||||
})("bar");
|
||||
}
|
||||
expect: {
|
||||
"use strict";
|
||||
(function() {
|
||||
console.log("foo", arguments[0]);
|
||||
})("bar");
|
||||
}
|
||||
expect_stdout: "foo bar"
|
||||
}
|
||||
|
||||
issue_3233: {
|
||||
options = {
|
||||
pure_getters: "strict",
|
||||
@@ -2161,8 +2181,7 @@ issue_3515_1: {
|
||||
expect: {
|
||||
var c = 0;
|
||||
(function() {
|
||||
this[c++] = 0;
|
||||
for (var key20 in !0);
|
||||
for (var key20 in !(this[c++] = 0));
|
||||
})();
|
||||
console.log(c);
|
||||
}
|
||||
@@ -2718,7 +2737,7 @@ issue_3962_1: {
|
||||
0..toString();
|
||||
} while (0);
|
||||
if (c) console.log("PASS");
|
||||
}((a--, 1)), 0);
|
||||
}(1), 0);
|
||||
void 0;
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
@@ -2751,7 +2770,7 @@ issue_3962_2: {
|
||||
0..toString();
|
||||
} while (0);
|
||||
if (c) console.log("PASS");
|
||||
}((a--, 1)), 0);
|
||||
}(1), 0);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
@@ -2834,11 +2853,11 @@ issue_4025: {
|
||||
console.log(a, b, d);
|
||||
}
|
||||
expect: {
|
||||
var d, c = 0;
|
||||
var c = 0;
|
||||
try {
|
||||
console.log(c);
|
||||
} finally {
|
||||
d = c + 1;
|
||||
var d = c + 1;
|
||||
c = 0;
|
||||
}
|
||||
console.log(1, 1, d);
|
||||
@@ -2905,3 +2924,102 @@ forin_var_2: {
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
issue_4133: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
merge_vars: true,
|
||||
pure_getters: "strict",
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 1;
|
||||
var b = [ a-- ], c = b && b[c];
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var b = 1;
|
||||
console.log(0);
|
||||
}
|
||||
expect_stdout: "0"
|
||||
}
|
||||
|
||||
issue_4144: {
|
||||
options = {
|
||||
keep_fargs: "strict",
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function(a, b) {
|
||||
var b = console, c = ++b;
|
||||
})(console.log("PASS"), 0);
|
||||
}
|
||||
expect: {
|
||||
(function(b) {
|
||||
b = console,
|
||||
++b;
|
||||
})(console.log("PASS"));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_4146: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
function g() {}
|
||||
var a = g;
|
||||
var c = b;
|
||||
c.p;
|
||||
console.log(typeof a);
|
||||
}
|
||||
f("FAIL", 42);
|
||||
}
|
||||
expect: {
|
||||
(function(a, b) {
|
||||
a = function () {};
|
||||
var c = b;
|
||||
c.p;
|
||||
console.log(typeof a);
|
||||
})(0, 42);
|
||||
}
|
||||
expect_stdout: "function"
|
||||
}
|
||||
|
||||
single_use_catch_redefined: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 1;
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
console.log(g());
|
||||
}
|
||||
expect: {
|
||||
var a = 1;
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
console.log(g());
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
@@ -2908,3 +2908,109 @@ issue_4077: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_4119_1: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
dead_code: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
var a, b;
|
||||
b = a = [];
|
||||
a[0] += 0;
|
||||
if (+b + 1) {
|
||||
console.log("FAIL");
|
||||
} else {
|
||||
console.log("PASS");
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
var a, b;
|
||||
b = a = [];
|
||||
a[0] += 0;
|
||||
+b + 1 ? console.log("FAIL") : console.log("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_4119_2: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
var a;
|
||||
(function(b) {
|
||||
a[0] += 0;
|
||||
console.log(+b + 1 ? "FAIL" : "PASS");
|
||||
})(a = []);
|
||||
}
|
||||
expect: {
|
||||
var a;
|
||||
(function(b) {
|
||||
a[0] += 0;
|
||||
console.log(+b + 1 ? "FAIL" : "PASS");
|
||||
})(a = []);
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_4119_3: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
var a, b;
|
||||
b = a = {
|
||||
p: 42,
|
||||
};
|
||||
delete a.p;
|
||||
console.log(b.p ? "FAIL" : "PASS");
|
||||
}
|
||||
expect: {
|
||||
var a, b;
|
||||
b = a = {
|
||||
p: 42,
|
||||
};
|
||||
delete a.p;
|
||||
console.log(b.p ? "FAIL" : "PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_4119_4: {
|
||||
options = {
|
||||
booleans: true,
|
||||
conditionals: true,
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var a, b;
|
||||
b = a = {
|
||||
p: 42,
|
||||
};
|
||||
delete a.p;
|
||||
console.log(!b ? "FAIL" : "PASS");
|
||||
}
|
||||
expect: {
|
||||
var a, b;
|
||||
b = a = {
|
||||
p: 42,
|
||||
};
|
||||
delete a.p;
|
||||
console.log((b, 0, "PASS"));
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -2676,7 +2676,7 @@ cross_references_3: {
|
||||
};
|
||||
return Math.square(n) + Math.cube(n);
|
||||
};
|
||||
}(Math)(2));
|
||||
}()(2));
|
||||
console.log(Math.square(3), Math.cube(3));
|
||||
}
|
||||
expect_stdout: [
|
||||
@@ -4777,3 +4777,213 @@ issue_4006: {
|
||||
}
|
||||
expect_stdout: "-1"
|
||||
}
|
||||
|
||||
issue_4155: {
|
||||
options = {
|
||||
functions: true,
|
||||
inline: true,
|
||||
merge_vars: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function() {
|
||||
var a;
|
||||
(function() {
|
||||
console.log(a);
|
||||
})(a);
|
||||
var b = function() {};
|
||||
b && console.log(typeof b);
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
void console.log(b);
|
||||
var b = function() {};
|
||||
b && console.log(typeof b);
|
||||
})();
|
||||
}
|
||||
expect_stdout: [
|
||||
"undefined",
|
||||
"function",
|
||||
]
|
||||
}
|
||||
|
||||
issue_4159: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 42, c = function(b) {
|
||||
(b = a) && console.log(a++, b);
|
||||
}(c = a);
|
||||
}
|
||||
expect: {
|
||||
var a = 42;
|
||||
(b = a) && console.log(a++, b);
|
||||
var b;
|
||||
}
|
||||
expect_stdout: "42 42"
|
||||
}
|
||||
|
||||
direct_inline: {
|
||||
options = {
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
function g(c) {
|
||||
return c >> 1;
|
||||
}
|
||||
return g(a) + g(b);
|
||||
}
|
||||
console.log(f(13, 31));
|
||||
}
|
||||
expect: {
|
||||
function f(a, b) {
|
||||
return (a >> 1) + (b >> 1);
|
||||
}
|
||||
console.log(f(13, 31));
|
||||
}
|
||||
expect_stdout: "21"
|
||||
}
|
||||
|
||||
direct_inline_catch_redefined: {
|
||||
options = {
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var a = 1;
|
||||
function f() {
|
||||
return a;
|
||||
}
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
console.log(a, f(), g());
|
||||
}
|
||||
console.log(a, f(), g());
|
||||
}
|
||||
expect: {
|
||||
var a = 1;
|
||||
function f() {
|
||||
return a;
|
||||
}
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
console.log(a, f(), g());
|
||||
}
|
||||
console.log(a, a, g());
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
issue_4171_1: {
|
||||
options = {
|
||||
functions: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function(a) {
|
||||
try {
|
||||
while (a)
|
||||
var e = function() {};
|
||||
} catch (e) {
|
||||
return function() {
|
||||
return e;
|
||||
};
|
||||
}
|
||||
}(!console));
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
try {
|
||||
while (a)
|
||||
var e = function() {};
|
||||
} catch (e) {
|
||||
return function() {
|
||||
return e;
|
||||
};
|
||||
}
|
||||
}(!console));
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
issue_4171_2: {
|
||||
options = {
|
||||
functions: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
console.log(function(a) {
|
||||
try {
|
||||
while (a);
|
||||
} catch (e) {
|
||||
return function() {
|
||||
return e;
|
||||
};
|
||||
} finally {
|
||||
var e = function() {};
|
||||
}
|
||||
}(!console));
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
try {
|
||||
while (a);
|
||||
} catch (e) {
|
||||
return function() {
|
||||
return e;
|
||||
};
|
||||
} finally {
|
||||
function e() {}
|
||||
}
|
||||
}(!console));
|
||||
}
|
||||
expect_stdout: "undefined"
|
||||
}
|
||||
|
||||
catch_defun: {
|
||||
mangle = {
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
try {
|
||||
throw 42;
|
||||
} catch (a) {
|
||||
function f() {
|
||||
return typeof a;
|
||||
}
|
||||
}
|
||||
console.log(f());
|
||||
}
|
||||
expect: {
|
||||
try {
|
||||
throw 42;
|
||||
} catch (o) {
|
||||
function t() {
|
||||
return typeof o;
|
||||
}
|
||||
}
|
||||
console.log(t());
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
@@ -2714,3 +2714,108 @@ issue_2737: {
|
||||
}
|
||||
expect_stdout: "function"
|
||||
}
|
||||
|
||||
single_use_catch_redefined: {
|
||||
options = {
|
||||
ie8: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 1;
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
console.log(g());
|
||||
}
|
||||
expect: {
|
||||
var a = 1;
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
console.log(g());
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
single_use_inline_catch_redefined: {
|
||||
options = {
|
||||
ie8: true,
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 1;
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
console.log(g());
|
||||
}
|
||||
expect: {
|
||||
var a = 1;
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
console.log(g());
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
direct_inline_catch_redefined: {
|
||||
options = {
|
||||
ie8: true,
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
}
|
||||
input: {
|
||||
var a = 1;
|
||||
function f() {
|
||||
return a;
|
||||
}
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
console.log(a, f(), g());
|
||||
}
|
||||
console.log(a, f(), g());
|
||||
}
|
||||
expect: {
|
||||
var a = 1;
|
||||
function f() {
|
||||
return a;
|
||||
}
|
||||
try {
|
||||
throw 2;
|
||||
} catch (a) {
|
||||
function g() {
|
||||
return a;
|
||||
}
|
||||
console.log(a, f(), g());
|
||||
}
|
||||
console.log(a, a, g());
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
@@ -594,3 +594,157 @@ iife_if_return_simple: {
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
nested_if_break: {
|
||||
options = {
|
||||
if_return: true,
|
||||
}
|
||||
input: {
|
||||
for (var i = 0; i < 3; i++)
|
||||
L1: if ("number" == typeof i) {
|
||||
if (0 === i) break L1;
|
||||
console.log(i);
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
for (var i = 0; i < 3; i++)
|
||||
L1: if ("number" == typeof i)
|
||||
if (0 !== i) console.log(i);
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"2",
|
||||
]
|
||||
}
|
||||
|
||||
nested_if_continue: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
join_vars: true,
|
||||
loops: true,
|
||||
}
|
||||
input: {
|
||||
function f(n) {
|
||||
var i = 0;
|
||||
do {
|
||||
if ("number" == typeof n) {
|
||||
if (0 === n) {
|
||||
console.log("even", i);
|
||||
continue;
|
||||
}
|
||||
if (1 === n) {
|
||||
console.log("odd", i);
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
} while (0 <= (n -= 2));
|
||||
}
|
||||
f(37);
|
||||
f(42);
|
||||
}
|
||||
expect: {
|
||||
function f(n) {
|
||||
for (var i = 0;
|
||||
"number" == typeof n
|
||||
&& (0 !== n
|
||||
? 1 !== n
|
||||
? i++
|
||||
: console.log("odd", i)
|
||||
: console.log("even", i)),
|
||||
0 <= (n -= 2););
|
||||
}
|
||||
f(37);
|
||||
f(42);
|
||||
}
|
||||
expect_stdout: [
|
||||
"odd 18",
|
||||
"even 21",
|
||||
]
|
||||
}
|
||||
|
||||
nested_if_return: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
}
|
||||
input: {
|
||||
function f() {
|
||||
if (A) {
|
||||
if (B)
|
||||
return B;
|
||||
if (C)
|
||||
return D;
|
||||
if (E)
|
||||
return F;
|
||||
if (G)
|
||||
return H;
|
||||
if (I) {
|
||||
if (J)
|
||||
return K;
|
||||
return;
|
||||
}
|
||||
if (L) {
|
||||
if (M)
|
||||
return;
|
||||
return N;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f() {
|
||||
if (A)
|
||||
return B || (C ? D : E ? F : G ? H : I ? J ? K : void 0 : L && !M ? N : void 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_866_1: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
sequences: false,
|
||||
};
|
||||
input: {
|
||||
function f(a) {
|
||||
if (a)
|
||||
return "";
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f(a) {
|
||||
if (a)
|
||||
return "";
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_866_2: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
if_return: true,
|
||||
sequences: true,
|
||||
}
|
||||
input: {
|
||||
(function() {
|
||||
if (a)
|
||||
if (b)
|
||||
c;
|
||||
else
|
||||
return d;
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
if (a) {
|
||||
if (!b)
|
||||
return d;
|
||||
c;
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ multiple_functions: {
|
||||
( function() {
|
||||
// NOTE: other compression steps will reduce this
|
||||
// down to just `window`.
|
||||
if ( window );
|
||||
if ( !window );
|
||||
function f() {}
|
||||
function g() {}
|
||||
} )();
|
||||
@@ -38,7 +38,7 @@ single_function: {
|
||||
}
|
||||
expect: {
|
||||
( function() {
|
||||
if ( window );
|
||||
if ( !window );
|
||||
function f() {}
|
||||
} )();
|
||||
}
|
||||
@@ -67,7 +67,7 @@ deeply_nested: {
|
||||
// NOTE: other compression steps will reduce this
|
||||
// down to just `window`.
|
||||
if ( window )
|
||||
if (document);
|
||||
if ( !document );
|
||||
function f() {}
|
||||
function g() {}
|
||||
function h() {}
|
||||
|
||||
@@ -306,7 +306,6 @@ issue_2298: {
|
||||
expect: {
|
||||
!function() {
|
||||
(function() {
|
||||
0;
|
||||
try {
|
||||
!function() {
|
||||
(void 0)[1] = "foo";
|
||||
|
||||
3010
test/compress/merge_vars.js
Normal file
3010
test/compress/merge_vars.js
Normal file
File diff suppressed because it is too large
Load Diff
@@ -91,7 +91,7 @@ evaluate_1: {
|
||||
expect: {
|
||||
console.log(
|
||||
x + 1 + 2,
|
||||
2 * x,
|
||||
2 * +x,
|
||||
+x + 1 + 2,
|
||||
1 + x + 2 + 3,
|
||||
3 | x,
|
||||
@@ -130,7 +130,7 @@ evaluate_1_unsafe_math: {
|
||||
expect: {
|
||||
console.log(
|
||||
x + 1 + 2,
|
||||
2 * x,
|
||||
2 * +x,
|
||||
+x + 3,
|
||||
1 + x + 2 + 3,
|
||||
3 | x,
|
||||
@@ -148,45 +148,52 @@ evaluate_1_unsafe_math: {
|
||||
evaluate_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe_math: false,
|
||||
}
|
||||
input: {
|
||||
var x = "42", y = null;
|
||||
[
|
||||
x + 1 + 2,
|
||||
x * 1 * 2,
|
||||
+x + 1 + 2,
|
||||
1 + x + 2 + 3,
|
||||
1 | x | 2 | 3,
|
||||
1 + x-- + 2 + 3,
|
||||
1 + (x*y + 2) + 3,
|
||||
1 + (2 + x + 3),
|
||||
1 + (2 + ~x + 3),
|
||||
-y + (2 + ~x + 3),
|
||||
1 & (2 & x & 3),
|
||||
1 + (2 + (x |= 0) + 3),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var x = "" + num, y = null;
|
||||
[
|
||||
x + 1 + 2,
|
||||
x * 1 * 2,
|
||||
+x + 1 + 2,
|
||||
1 + x + 2 + 3,
|
||||
1 | x | 2 | 3,
|
||||
1 + x-- + 2 + 3,
|
||||
1 + (x*y + 2) + 3,
|
||||
1 + (2 + x + 3),
|
||||
1 + (2 + ~x + 3),
|
||||
-y + (2 + ~x + 3),
|
||||
1 & (2 & x & 3),
|
||||
1 + (2 + (x |= 0) + 3),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect: {
|
||||
var x = "42", y = null;
|
||||
[
|
||||
x + 1 + 2,
|
||||
2 * x,
|
||||
+x + 1 + 2,
|
||||
1 + x + 2 + 3,
|
||||
3 | x,
|
||||
1 + x-- + 2 + 3,
|
||||
x*y + 2 + 1 + 3,
|
||||
1 + (2 + x + 3),
|
||||
2 + ~x + 3 + 1,
|
||||
2 + ~x + 3 - y,
|
||||
0 & x,
|
||||
2 + (x |= 0) + 3 + 1,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var x = "" + num, y = null;
|
||||
[
|
||||
x + "12",
|
||||
2 * x,
|
||||
+x + 1 + 2,
|
||||
1 + x + "23",
|
||||
3 | x,
|
||||
1 + x-- + 2 + 3,
|
||||
x*y + 2 + 1 + 3,
|
||||
2 + x + 3 + 1,
|
||||
2 + ~x + 3 + 1,
|
||||
2 + ~x + 3,
|
||||
0 & x,
|
||||
2 + (x |= 0) + 3 + 1,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect_stdout: [
|
||||
"string 4212",
|
||||
@@ -207,45 +214,52 @@ evaluate_2: {
|
||||
evaluate_2_unsafe_math: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe_math: true,
|
||||
}
|
||||
input: {
|
||||
var x = "42", y = null;
|
||||
[
|
||||
x + 1 + 2,
|
||||
x * 1 * 2,
|
||||
+x + 1 + 2,
|
||||
1 + x + 2 + 3,
|
||||
1 | x | 2 | 3,
|
||||
1 + x-- + 2 + 3,
|
||||
1 + (x*y + 2) + 3,
|
||||
1 + (2 + x + 3),
|
||||
1 + (2 + ~x + 3),
|
||||
-y + (2 + ~x + 3),
|
||||
1 & (2 & x & 3),
|
||||
1 + (2 + (x |= 0) + 3),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var x = "" + num, y = null;
|
||||
[
|
||||
x + 1 + 2,
|
||||
x * 1 * 2,
|
||||
+x + 1 + 2,
|
||||
1 + x + 2 + 3,
|
||||
1 | x | 2 | 3,
|
||||
1 + x-- + 2 + 3,
|
||||
1 + (x*y + 2) + 3,
|
||||
1 + (2 + x + 3),
|
||||
1 + (2 + ~x + 3),
|
||||
-y + (2 + ~x + 3),
|
||||
1 & (2 & x & 3),
|
||||
1 + (2 + (x |= 0) + 3),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect: {
|
||||
var x = "42", y = null;
|
||||
[
|
||||
x + 1 + 2,
|
||||
2 * x,
|
||||
+x + 3,
|
||||
1 + x + 2 + 3,
|
||||
3 | x,
|
||||
6 + x--,
|
||||
x*y + 6,
|
||||
1 + (2 + x + 3),
|
||||
6 + ~x,
|
||||
5 + ~x - y,
|
||||
0 & x,
|
||||
6 + (x |= 0),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var x = "" + num, y = null;
|
||||
[
|
||||
x + "12",
|
||||
2 * x,
|
||||
+x + 3,
|
||||
1 + x + "23",
|
||||
3 | x,
|
||||
6 + x--,
|
||||
x*y + 6,
|
||||
6 + x,
|
||||
6 + ~x,
|
||||
5 + ~x,
|
||||
0 & x,
|
||||
6 + (x |= 0),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect_stdout: [
|
||||
"string 4212",
|
||||
@@ -310,45 +324,52 @@ evaluate_4: {
|
||||
evaluate_5: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe_math: false,
|
||||
}
|
||||
input: {
|
||||
var a = "1";
|
||||
[
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
+a - 2 + 3,
|
||||
+a - 2 - 3,
|
||||
2 + +a + 3,
|
||||
2 + +a - 3,
|
||||
2 - +a + 3,
|
||||
2 - +a - 3,
|
||||
2 + 3 + +a,
|
||||
2 + 3 - +a,
|
||||
2 - 3 + +a,
|
||||
2 - 3 - +a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var a = "" + num;
|
||||
[
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
+a - 2 + 3,
|
||||
+a - 2 - 3,
|
||||
2 + +a + 3,
|
||||
2 + +a - 3,
|
||||
2 - +a + 3,
|
||||
2 - +a - 3,
|
||||
2 + 3 + +a,
|
||||
2 + 3 - +a,
|
||||
2 - 3 + +a,
|
||||
2 - 3 - +a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(1);
|
||||
}
|
||||
expect: {
|
||||
var a = "1";
|
||||
[
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
a - 2 + 3,
|
||||
a - 2 - 3,
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
2 - a + 3,
|
||||
2 - a - 3,
|
||||
+a + 5,
|
||||
5 - a,
|
||||
+a - 1,
|
||||
-1 - a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var a = "" + num;
|
||||
[
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
a - 2 + 3,
|
||||
a - 2 - 3,
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
2 - a + 3,
|
||||
2 - a - 3,
|
||||
+a + 5,
|
||||
5 - a,
|
||||
+a - 1,
|
||||
-1 - a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(1);
|
||||
}
|
||||
expect_stdout: [
|
||||
"number 6",
|
||||
@@ -369,45 +390,52 @@ evaluate_5: {
|
||||
evaluate_5_unsafe_math: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe_math: true,
|
||||
}
|
||||
input: {
|
||||
var a = "1";
|
||||
[
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
+a - 2 + 3,
|
||||
+a - 2 - 3,
|
||||
2 + +a + 3,
|
||||
2 + +a - 3,
|
||||
2 - +a + 3,
|
||||
2 - +a - 3,
|
||||
2 + 3 + +a,
|
||||
2 + 3 - +a,
|
||||
2 - 3 + +a,
|
||||
2 - 3 - +a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var a = "" + num;
|
||||
[
|
||||
+a + 2 + 3,
|
||||
+a + 2 - 3,
|
||||
+a - 2 + 3,
|
||||
+a - 2 - 3,
|
||||
2 + +a + 3,
|
||||
2 + +a - 3,
|
||||
2 - +a + 3,
|
||||
2 - +a - 3,
|
||||
2 + 3 + +a,
|
||||
2 + 3 - +a,
|
||||
2 - 3 + +a,
|
||||
2 - 3 - +a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(1);
|
||||
}
|
||||
expect: {
|
||||
var a = "1";
|
||||
[
|
||||
+a + 5,
|
||||
+a + -1,
|
||||
a - -1,
|
||||
a - 5,
|
||||
+a + 5,
|
||||
+a + -1,
|
||||
5 - a,
|
||||
-1 - a,
|
||||
+a + 5,
|
||||
5 - a,
|
||||
+a - 1,
|
||||
-1 - a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num) {
|
||||
var a = "" + num;
|
||||
[
|
||||
+a + 5,
|
||||
+a + -1,
|
||||
a - -1,
|
||||
a - 5,
|
||||
+a + 5,
|
||||
+a + -1,
|
||||
5 - a,
|
||||
-1 - a,
|
||||
+a + 5,
|
||||
5 - a,
|
||||
+a - 1,
|
||||
-1 - a,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(1);
|
||||
}
|
||||
expect_stdout: [
|
||||
"number 6",
|
||||
@@ -546,37 +574,44 @@ evaluate_6_unsafe_math: {
|
||||
evaluate_7: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe_math: false,
|
||||
}
|
||||
input: {
|
||||
var x = "42", y;
|
||||
[
|
||||
+x + 2 + (3 + !y),
|
||||
+x + 2 + (3 - !y),
|
||||
+x + 2 - (3 + !y),
|
||||
+x + 2 - (3 - !y),
|
||||
+x - 2 + (3 + !y),
|
||||
+x - 2 + (3 - !y),
|
||||
+x - 2 - (3 + !y),
|
||||
+x - 2 - (3 - !y),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num, y) {
|
||||
var x = "" + num;
|
||||
[
|
||||
+x + 2 + (3 + !y),
|
||||
+x + 2 + (3 - !y),
|
||||
+x + 2 - (3 + !y),
|
||||
+x + 2 - (3 - !y),
|
||||
+x - 2 + (3 + !y),
|
||||
+x - 2 + (3 - !y),
|
||||
+x - 2 - (3 + !y),
|
||||
+x - 2 - (3 - !y),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect: {
|
||||
var x = "42", y;
|
||||
[
|
||||
+x + 2 + (3 + !y),
|
||||
+x + 2 + (3 - !y),
|
||||
+x + 2 - (3 + !y),
|
||||
+x + 2 - (3 - !y),
|
||||
x - 2 + (3 + !y),
|
||||
x - 2 + (3 - !y),
|
||||
x - 2 - (3 + !y),
|
||||
x - 2 - (3 - !y),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num, y) {
|
||||
var x = "" + num;
|
||||
[
|
||||
+x + 2 + (3 + !y),
|
||||
+x + 2 + (3 - !y),
|
||||
+x + 2 - (3 + !y),
|
||||
+x + 2 - (3 - !y),
|
||||
x - 2 + (3 + !y),
|
||||
x - 2 + (3 - !y),
|
||||
x - 2 - (3 + !y),
|
||||
x - 2 - (3 - !y),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect_stdout: [
|
||||
"number 48",
|
||||
@@ -593,37 +628,44 @@ evaluate_7: {
|
||||
evaluate_7_unsafe_math: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
reduce_vars: true,
|
||||
unsafe_math: true,
|
||||
}
|
||||
input: {
|
||||
var x = "42", y;
|
||||
[
|
||||
+x + 2 + (3 + !y),
|
||||
+x + 2 + (3 - !y),
|
||||
+x + 2 - (3 + !y),
|
||||
+x + 2 - (3 - !y),
|
||||
+x - 2 + (3 + !y),
|
||||
+x - 2 + (3 - !y),
|
||||
+x - 2 - (3 + !y),
|
||||
+x - 2 - (3 - !y),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num, y) {
|
||||
var x = "" + num;
|
||||
[
|
||||
+x + 2 + (3 + !y),
|
||||
+x + 2 + (3 - !y),
|
||||
+x + 2 - (3 + !y),
|
||||
+x + 2 - (3 - !y),
|
||||
+x - 2 + (3 + !y),
|
||||
+x - 2 + (3 - !y),
|
||||
+x - 2 - (3 + !y),
|
||||
+x - 2 - (3 - !y),
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect: {
|
||||
var x = "42", y;
|
||||
[
|
||||
+x + 5 + !y,
|
||||
+x + 5 - !y,
|
||||
+x + -1 - !y,
|
||||
+x + -1 + !y,
|
||||
x - -1 + !y,
|
||||
x - -1 - !y,
|
||||
x - 5 - !y,
|
||||
x - 5 + !y,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
function f(num, y) {
|
||||
var x = "" + num;
|
||||
[
|
||||
+x + 5 + !y,
|
||||
+x + 5 - !y,
|
||||
+x + -1 - !y,
|
||||
+x + -1 + !y,
|
||||
x - -1 + !y,
|
||||
x - -1 - !y,
|
||||
x - 5 - !y,
|
||||
x - 5 + !y,
|
||||
].forEach(function(n) {
|
||||
console.log(typeof n, n);
|
||||
});
|
||||
}
|
||||
f(42);
|
||||
}
|
||||
expect_stdout: [
|
||||
"number 48",
|
||||
@@ -1267,3 +1309,29 @@ issue_3695: {
|
||||
}
|
||||
expect_stdout: "NaN"
|
||||
}
|
||||
|
||||
issue_4137: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
}
|
||||
input: {
|
||||
console.log(+(A = []) * (A[0] = 1));
|
||||
}
|
||||
expect: {
|
||||
console.log(+(A = []) * (A[0] = 1));
|
||||
}
|
||||
expect_stdout: "0"
|
||||
}
|
||||
|
||||
issue_4142: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
}
|
||||
input: {
|
||||
console.log("" + +(0 === console));
|
||||
}
|
||||
expect: {
|
||||
console.log("" + +(0 === console));
|
||||
}
|
||||
expect_stdout: "0"
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ modified: {
|
||||
expect: {
|
||||
function f0() {
|
||||
var b = 2;
|
||||
b++;
|
||||
+b;
|
||||
console.log(2);
|
||||
console.log(4);
|
||||
}
|
||||
@@ -1624,7 +1624,7 @@ defun_label: {
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
double_reference: {
|
||||
double_reference_1: {
|
||||
options = {
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
@@ -1638,6 +1638,32 @@ double_reference: {
|
||||
g();
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f() {
|
||||
var g = function g() {
|
||||
g();
|
||||
};
|
||||
g();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double_reference_2: {
|
||||
options = {
|
||||
functions: true,
|
||||
passes: 2,
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f() {
|
||||
var g = function g() {
|
||||
g();
|
||||
};
|
||||
g();
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
function f() {
|
||||
(function g() {
|
||||
@@ -1647,6 +1673,60 @@ double_reference: {
|
||||
}
|
||||
}
|
||||
|
||||
double_reference_3: {
|
||||
options = {
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var x = function f() {
|
||||
return f;
|
||||
};
|
||||
function g() {
|
||||
return x();
|
||||
}
|
||||
console.log(g() === g());
|
||||
}
|
||||
expect: {
|
||||
var x = function f() {
|
||||
return f;
|
||||
};
|
||||
function g() {
|
||||
return x();
|
||||
}
|
||||
console.log(g() === g());
|
||||
}
|
||||
expect_stdout: "true"
|
||||
}
|
||||
|
||||
double_reference_4: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
functions: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var x = function f() {
|
||||
return f;
|
||||
};
|
||||
function g() {
|
||||
return x();
|
||||
}
|
||||
console.log(g() === g());
|
||||
}
|
||||
expect: {
|
||||
console.log(true);
|
||||
}
|
||||
expect_stdout: "true"
|
||||
}
|
||||
|
||||
iife_arguments_1: {
|
||||
options = {
|
||||
reduce_funcs: true,
|
||||
@@ -1686,8 +1766,35 @@ iife_arguments_2: {
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
console.log(function f() {
|
||||
var x = function f() {
|
||||
return f;
|
||||
};
|
||||
console.log(x() === arguments[0]);
|
||||
})();
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
iife_arguments_3: {
|
||||
options = {
|
||||
functions: true,
|
||||
passes: 2,
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
(function() {
|
||||
var x = function f() {
|
||||
return f;
|
||||
};
|
||||
console.log(x() === arguments[0]);
|
||||
})();
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
console.log(function x() {
|
||||
return x;
|
||||
}() === arguments[0]);
|
||||
})();
|
||||
}
|
||||
@@ -2069,6 +2176,7 @@ issue_1670_6: {
|
||||
keep_fargs: false,
|
||||
reduce_funcs: true,
|
||||
reduce_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
switches: true,
|
||||
unused: true,
|
||||
@@ -2086,10 +2194,9 @@ issue_1670_6: {
|
||||
})(1);
|
||||
}
|
||||
expect: {
|
||||
(function(a) {
|
||||
a = 1;
|
||||
console.log(a);
|
||||
})(1);
|
||||
(function() {
|
||||
console.log(1);
|
||||
})();
|
||||
}
|
||||
expect_stdout: "1"
|
||||
}
|
||||
@@ -2308,7 +2415,7 @@ redefine_farg_2: {
|
||||
console.log(typeof [], "number",function(a, b) {
|
||||
a = b;
|
||||
return typeof a;
|
||||
}([]));
|
||||
}());
|
||||
}
|
||||
expect_stdout: "object number undefined"
|
||||
}
|
||||
@@ -5267,11 +5374,11 @@ defun_catch_4: {
|
||||
try {
|
||||
throw 42;
|
||||
} catch (a) {
|
||||
function a() {}
|
||||
console.log(a);
|
||||
}
|
||||
}
|
||||
expect_stdout: "42"
|
||||
node_version: "<=4"
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
defun_catch_5: {
|
||||
@@ -5293,10 +5400,10 @@ defun_catch_5: {
|
||||
throw 42;
|
||||
} catch (a) {
|
||||
console.log(a);
|
||||
function a() {}
|
||||
}
|
||||
}
|
||||
expect_stdout: "42"
|
||||
node_version: "<=4"
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
defun_catch_6: {
|
||||
@@ -5483,7 +5590,7 @@ lvalues_def_1: {
|
||||
}
|
||||
expect: {
|
||||
var b = 1;
|
||||
var a = b++, b = NaN;
|
||||
var a = +b, b = NaN;
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_stdout: "1 NaN"
|
||||
|
||||
@@ -245,6 +245,31 @@ unsafe_builtin_2: {
|
||||
expect_stdout: "object PASS PASS"
|
||||
}
|
||||
|
||||
unsafe_builtin_3: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
var o = {};
|
||||
if (42 < Math.random())
|
||||
o.p = "FAIL";
|
||||
else
|
||||
o.p = "PASS";
|
||||
for (var k in o)
|
||||
console.log(k, o[k]);
|
||||
}
|
||||
expect: {
|
||||
var o = {};
|
||||
o.p = 42 < Math.random() ? "FAIL" : "PASS";
|
||||
for (var k in o)
|
||||
console.log(k, o[k]);
|
||||
}
|
||||
expect_stdout: "p PASS"
|
||||
}
|
||||
|
||||
unsafe_string_replace: {
|
||||
options = {
|
||||
side_effects: true,
|
||||
|
||||
@@ -1,11 +1,47 @@
|
||||
require("../../tools/exit");
|
||||
|
||||
var get = require("https").get;
|
||||
var parse = require("url").parse;
|
||||
var base = process.argv[2];
|
||||
var token = process.argv[3];
|
||||
|
||||
var base, token, run_number, eldest = true;
|
||||
exports.init = function(url, auth, num) {
|
||||
base = url;
|
||||
token = auth;
|
||||
run_number = num;
|
||||
};
|
||||
exports.should_stop = function(callback) {
|
||||
read(base + "/actions/runs?per_page=100", function(reply) {
|
||||
if (!reply || !Array.isArray(reply.workflow_runs)) return;
|
||||
var runs = reply.workflow_runs.filter(function(workflow) {
|
||||
return workflow.status != "completed";
|
||||
}).sort(function(a, b) {
|
||||
return b.run_number - a.run_number;
|
||||
});
|
||||
var found = false, remaining = 20;
|
||||
(function next() {
|
||||
if (!runs.length) return;
|
||||
var workflow = runs.pop();
|
||||
if (workflow.event == "schedule" && workflow.run_number == run_number) found = true;
|
||||
read(workflow.jobs_url, function(reply) {
|
||||
if (!reply || !Array.isArray(reply.jobs)) return;
|
||||
if (!reply.jobs.every(function(job) {
|
||||
if (job.status == "completed") return true;
|
||||
remaining--;
|
||||
return found || workflow.event != "schedule";
|
||||
})) return;
|
||||
if (remaining >= 0) {
|
||||
next();
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
})();
|
||||
});
|
||||
};
|
||||
|
||||
function read(url, callback) {
|
||||
var done = function(reply) {
|
||||
done = function() {};
|
||||
callback(reply);
|
||||
};
|
||||
var options = parse(url);
|
||||
options.headers = {
|
||||
"Authorization": "Token " + token,
|
||||
@@ -17,33 +53,15 @@ function read(url, callback) {
|
||||
response.on("data", function(chunk) {
|
||||
chunks.push(chunk);
|
||||
}).on("end", function() {
|
||||
callback(JSON.parse(chunks.join("")));
|
||||
var reply;
|
||||
try {
|
||||
reply = JSON.parse(chunks.join(""))
|
||||
} catch (e) {}
|
||||
done(reply);
|
||||
}).on("error", function() {
|
||||
done();
|
||||
});
|
||||
}).on("error", function() {
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
var queued = 0, total = 0, earliest, now = Date.now();
|
||||
process.on("beforeExit", function() {
|
||||
if (queued > 3) {
|
||||
process.stdout.write("0");
|
||||
} else if (now - earliest > 0 && total > 1) {
|
||||
process.stdout.write(Math.min(20 * (now - earliest) / (total - 1), 18000000).toFixed(0));
|
||||
} else {
|
||||
process.stdout.write("3600000");
|
||||
}
|
||||
});
|
||||
read(base + "/actions/workflows/ufuzz.yml/runs?event=schedule", function(reply) {
|
||||
reply.workflow_runs.filter(function(workflow) {
|
||||
return /^(in_progress|queued|)$/.test(workflow.status);
|
||||
}).forEach(function(workflow) {
|
||||
read(workflow.jobs_url, function(reply) {
|
||||
reply.jobs.forEach(function(job) {
|
||||
if (job.status == "queued") queued++;
|
||||
total++;
|
||||
if (!job.started_at) return;
|
||||
var start = new Date(job.started_at);
|
||||
if (!(earliest < start)) earliest = start;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,39 +1,69 @@
|
||||
var actions = require("./actions");
|
||||
var child_process = require("child_process");
|
||||
|
||||
var ping = 5 * 60 * 1000;
|
||||
var period = +process.argv[2];
|
||||
var endTime = Date.now() + period;
|
||||
for (var i = 0; i < 2; i++) spawn(endTime);
|
||||
|
||||
function spawn(endTime) {
|
||||
var child = child_process.spawn("node", [
|
||||
"--max-old-space-size=2048",
|
||||
"test/ufuzz"
|
||||
], {
|
||||
stdio: [ "ignore", "pipe", "pipe" ]
|
||||
}).on("exit", respawn);
|
||||
var stdout = "";
|
||||
child.stdout.on("data", function(data) {
|
||||
stdout += data;
|
||||
var args = [
|
||||
"--max-old-space-size=2048",
|
||||
"test/ufuzz",
|
||||
];
|
||||
var iterations;
|
||||
switch (process.argv.length) {
|
||||
case 3:
|
||||
iterations = +process.argv[2];
|
||||
args.push(iterations);
|
||||
break;
|
||||
case 5:
|
||||
actions.init(process.argv[2], process.argv[3], +process.argv[4]);
|
||||
break;
|
||||
default:
|
||||
throw new Error("invalid parameters");
|
||||
}
|
||||
var tasks = [ run(), run() ];
|
||||
if (iterations) return;
|
||||
var alive = setInterval(function() {
|
||||
actions.should_stop(function() {
|
||||
clearInterval(alive);
|
||||
tasks.forEach(function(kill) {
|
||||
kill();
|
||||
});
|
||||
});
|
||||
var stderr = "";
|
||||
child.stderr.on("data", trap).pipe(process.stdout);
|
||||
var keepAlive = setInterval(function() {
|
||||
var end = stdout.lastIndexOf("\r");
|
||||
console.log(stdout.slice(stdout.lastIndexOf("\r", end - 1) + 1, end));
|
||||
stdout = stdout.slice(end + 1);
|
||||
}, ping);
|
||||
var timer = setTimeout(function() {
|
||||
clearInterval(keepAlive);
|
||||
}, 8 * 60 * 1000);
|
||||
|
||||
function run() {
|
||||
var child, stdout, stderr, log;
|
||||
spawn();
|
||||
return function() {
|
||||
clearInterval(log);
|
||||
child.removeListener("exit", respawn);
|
||||
child.kill();
|
||||
}, endTime - Date.now());
|
||||
};
|
||||
|
||||
function spawn() {
|
||||
child = child_process.spawn("node", args, {
|
||||
stdio: [ "ignore", "pipe", "pipe" ]
|
||||
}).on("exit", respawn);
|
||||
stdout = "";
|
||||
child.stdout.on("data", function(data) {
|
||||
stdout += data;
|
||||
});
|
||||
stderr = "";
|
||||
child.stderr.on("data", trap).pipe(process.stdout);
|
||||
log = setInterval(function() {
|
||||
var end = stdout.lastIndexOf("\r");
|
||||
console.log(stdout.slice(stdout.lastIndexOf("\r", end - 1) + 1, end));
|
||||
stdout = stdout.slice(end + 1);
|
||||
}, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
function respawn() {
|
||||
console.log(stdout.replace(/[^\r\n]*\r/g, ""));
|
||||
clearInterval(keepAlive);
|
||||
clearTimeout(timer);
|
||||
spawn(endTime);
|
||||
clearInterval(log);
|
||||
if (!iterations) {
|
||||
spawn();
|
||||
} else if (process.exitCode) {
|
||||
tasks.forEach(function(kill) {
|
||||
kill();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function trap(data) {
|
||||
|
||||
Reference in New Issue
Block a user