aboutsummaryrefslogtreecommitdiff
path: root/tools/eliminator/node_modules/uglify-js/tmp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eliminator/node_modules/uglify-js/tmp')
-rw-r--r--tools/eliminator/node_modules/uglify-js/tmp/hoist.js33
-rw-r--r--tools/eliminator/node_modules/uglify-js/tmp/instrument.js97
-rw-r--r--tools/eliminator/node_modules/uglify-js/tmp/instrument2.js138
-rwxr-xr-xtools/eliminator/node_modules/uglify-js/tmp/test.js16
4 files changed, 284 insertions, 0 deletions
diff --git a/tools/eliminator/node_modules/uglify-js/tmp/hoist.js b/tools/eliminator/node_modules/uglify-js/tmp/hoist.js
new file mode 100644
index 00000000..4bf2b94d
--- /dev/null
+++ b/tools/eliminator/node_modules/uglify-js/tmp/hoist.js
@@ -0,0 +1,33 @@
+function foo(arg1, arg2, arg3, arg4, arg5, arg6) {
+ var a = 5;
+ {
+ var d = 10, mak = 20, buz = 30;
+ var q = buz * 2;
+ }
+ if (moo) {
+ var a, b, c;
+ }
+ for (var arg1 = 0, d = 20; arg1 < 10; ++arg1)
+ console.log(arg3);
+ for (var i in mak) {}
+ for (j in d) {}
+ var d;
+
+ function test() {
+
+ };
+
+ //test();
+
+ (function moo(first, second){
+ console.log(first);
+ })(1);
+
+ (function moo(first, second){
+ console.log(moo());
+ })(1);
+}
+
+
+var foo;
+var bar;
diff --git a/tools/eliminator/node_modules/uglify-js/tmp/instrument.js b/tools/eliminator/node_modules/uglify-js/tmp/instrument.js
new file mode 100644
index 00000000..c6a9d798
--- /dev/null
+++ b/tools/eliminator/node_modules/uglify-js/tmp/instrument.js
@@ -0,0 +1,97 @@
+// sample on how to use the parser and walker API to instrument some code
+
+var jsp = require("uglify-js").parser;
+var pro = require("uglify-js").uglify;
+
+function instrument(code) {
+ var ast = jsp.parse(code, false, true); // true for the third arg specifies that we want
+ // to have start/end tokens embedded in the
+ // statements
+ var w = pro.ast_walker();
+
+ // we're gonna need this to push elements that we're currently looking at, to avoid
+ // endless recursion.
+ var analyzing = [];
+ function do_stat() {
+ var ret;
+ if (this[0].start && analyzing.indexOf(this) < 0) {
+ // without the `analyzing' hack, w.walk(this) would re-enter here leading
+ // to infinite recursion
+ analyzing.push(this);
+ ret = [ "splice", // XXX: "block" is safer
+ [ [ "stat",
+ [ "call", [ "name", "trace" ],
+ [ [ "string", this[0].toString() ],
+ [ "num", this[0].start.line ],
+ [ "num", this[0].start.col ],
+ [ "num", this[0].end.line ],
+ [ "num", this[0].end.col ]]]],
+ w.walk(this) ]];
+ analyzing.pop(this);
+ }
+ return ret;
+ };
+ var new_ast = w.with_walkers({
+ "stat" : do_stat,
+ "label" : do_stat,
+ "break" : do_stat,
+ "continue" : do_stat,
+ "debugger" : do_stat,
+ "var" : do_stat,
+ "const" : do_stat,
+ "return" : do_stat,
+ "throw" : do_stat,
+ "try" : do_stat,
+ "defun" : do_stat,
+ "if" : do_stat,
+ "while" : do_stat,
+ "do" : do_stat,
+ "for" : do_stat,
+ "for-in" : do_stat,
+ "switch" : do_stat,
+ "with" : do_stat
+ }, function(){
+ return w.walk(ast);
+ });
+ return pro.gen_code(new_ast, { beautify: true });
+}
+
+
+
+
+////// test code follows.
+
+var code = instrument(test.toString());
+console.log(code);
+
+function test() {
+ // simple stats
+ a = 5;
+ c += a + b;
+ "foo";
+
+ // var
+ var foo = 5;
+ const bar = 6, baz = 7;
+
+ // switch block. note we can't track case lines the same way.
+ switch ("foo") {
+ case "foo":
+ return 1;
+ case "bar":
+ return 2;
+ }
+
+ // for/for in
+ for (var i = 0; i < 5; ++i) {
+ console.log("Hello " + i);
+ }
+ for (var i in [ 1, 2, 3]) {
+ console.log(i);
+ }
+
+ // note however that the following is broken. I guess we
+ // should add the block brackets in this case...
+ for (var i = 0; i < 5; ++i)
+ console.log("foo");
+}
diff --git a/tools/eliminator/node_modules/uglify-js/tmp/instrument2.js b/tools/eliminator/node_modules/uglify-js/tmp/instrument2.js
new file mode 100644
index 00000000..6aee5f3f
--- /dev/null
+++ b/tools/eliminator/node_modules/uglify-js/tmp/instrument2.js
@@ -0,0 +1,138 @@
+// sample on how to use the parser and walker API to instrument some code
+
+var jsp = require("uglify-js").parser;
+var pro = require("uglify-js").uglify;
+
+function instrument(code) {
+ var ast = jsp.parse(code, false, true); // true for the third arg specifies that we want
+ // to have start/end tokens embedded in the
+ // statements
+ var w = pro.ast_walker();
+
+ function trace (line, comment) {
+ var code = pro.gen_code(line, { beautify: true });
+ var data = line[0]
+
+ var args = []
+ if (!comment) comment = ""
+ if (typeof data === "object") {
+ code = code.split(/\n/).shift()
+ args = [ [ "string", data.toString() ],
+ [ "string", code ],
+ [ "num", data.start.line ],
+ [ "num", data.start.col ],
+ [ "num", data.end.line ],
+ [ "num", data.end.col ]]
+ } else {
+ args = [ [ "string", data ],
+ [ "string", code ]]
+
+ }
+ return [ "call", [ "name", "trace" ], args ];
+ }
+
+ // we're gonna need this to push elements that we're currently looking at, to avoid
+ // endless recursion.
+ var analyzing = [];
+ function do_stat() {
+ var ret;
+ if (this[0].start && analyzing.indexOf(this) < 0) {
+ // without the `analyzing' hack, w.walk(this) would re-enter here leading
+ // to infinite recursion
+ analyzing.push(this);
+ ret = [ "splice",
+ [ [ "stat", trace(this) ],
+ w.walk(this) ]];
+ analyzing.pop(this);
+ }
+ return ret;
+ }
+
+ function do_cond(c, t, f) {
+ return [ this[0], w.walk(c),
+ ["seq", trace(t), w.walk(t) ],
+ ["seq", trace(f), w.walk(f) ]];
+ }
+
+ function do_binary(c, l, r) {
+ if (c !== "&&" && c !== "||") {
+ return [this[0], c, w.walk(l), w.walk(r)];
+ }
+ return [ this[0], c,
+ ["seq", trace(l), w.walk(l) ],
+ ["seq", trace(r), w.walk(r) ]];
+ }
+
+ var new_ast = w.with_walkers({
+ "stat" : do_stat,
+ "label" : do_stat,
+ "break" : do_stat,
+ "continue" : do_stat,
+ "debugger" : do_stat,
+ "var" : do_stat,
+ "const" : do_stat,
+ "return" : do_stat,
+ "throw" : do_stat,
+ "try" : do_stat,
+ "defun" : do_stat,
+ "if" : do_stat,
+ "while" : do_stat,
+ "do" : do_stat,
+ "for" : do_stat,
+ "for-in" : do_stat,
+ "switch" : do_stat,
+ "with" : do_stat,
+ "conditional" : do_cond,
+ "binary" : do_binary
+ }, function(){
+ return w.walk(ast);
+ });
+ return pro.gen_code(new_ast, { beautify: true });
+}
+
+
+////// test code follows.
+
+var code = instrument(test.toString());
+console.log(code);
+
+function test() {
+ // simple stats
+ a = 5;
+ c += a + b;
+ "foo";
+
+ // var
+ var foo = 5;
+ const bar = 6, baz = 7;
+
+ // switch block. note we can't track case lines the same way.
+ switch ("foo") {
+ case "foo":
+ return 1;
+ case "bar":
+ return 2;
+ }
+
+ // for/for in
+ for (var i = 0; i < 5; ++i) {
+ console.log("Hello " + i);
+ }
+ for (var i in [ 1, 2, 3]) {
+ console.log(i);
+ }
+
+ for (var i = 0; i < 5; ++i)
+ console.log("foo");
+
+ for (var i = 0; i < 5; ++i) {
+ console.log("foo");
+ }
+
+ var k = plurp() ? 1 : 0;
+ var x = a ? doX(y) && goZoo("zoo")
+ : b ? blerg({ x: y })
+ : null;
+
+ var x = X || Y;
+}
diff --git a/tools/eliminator/node_modules/uglify-js/tmp/test.js b/tools/eliminator/node_modules/uglify-js/tmp/test.js
new file mode 100755
index 00000000..46842f60
--- /dev/null
+++ b/tools/eliminator/node_modules/uglify-js/tmp/test.js
@@ -0,0 +1,16 @@
+#! /usr/bin/env node
+
+global.sys = require(/^v0\.[012]/.test(process.version) ? "sys" : "util");
+var fs = require("fs");
+var uglify = require("uglify-js"), // symlink ~/.node_libraries/uglify-js.js to ../uglify-js.js
+ jsp = uglify.parser,
+ pro = uglify.uglify;
+
+var code = fs.readFileSync("hoist.js", "utf8");
+var ast = jsp.parse(code);
+
+ast = pro.ast_lift_variables(ast);
+
+console.log(pro.gen_code(ast, {
+ beautify: true
+}));