Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Instrument binary/conditional paths as well

  • Loading branch information...
commit dca844154a6d8a07a9467b9bbb9d2738ff841b34 1 parent b01fa40
isaacs authored
Showing with 72 additions and 31 deletions.
  1. +72 −31 tmp/instrument.js
103 tmp/instrument.js
View
@@ -9,6 +9,28 @@ function instrument(code) {
// 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 = [];
@@ -18,38 +40,50 @@ function instrument(code) {
// 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 ]]]],
+ ret = [ "block",
+ [ [ "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
+ "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);
});
@@ -57,8 +91,6 @@ function instrument(code) {
}
-
-
////// test code follows.
var code = instrument(test.toString());
@@ -90,8 +122,17 @@ function test() {
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");
+
+ 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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.