Skip to content

Commit

Permalink
work on evanw#184: instrument blocks and statements
Browse files Browse the repository at this point in the history
  • Loading branch information
stoand committed Mar 13, 2023
1 parent 65c935b commit fa135dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
15 changes: 8 additions & 7 deletions inst_experiments/out.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const asdf = __INST(13, 1234);
function asdf2() {
__INST(43, __INST(43, __INST(43, console).log)(__INST(55, "asdf")));
let b = __INST(81, 3);
let c = __INST(96, 4);
let a = __INST(111, __INST(111, b) + __INST(115, 1) + __INST(119, c));
__INST(0, null);const asdf = __INST(13, null, 1234);
__INST(20, null);function asdf2() {
__INST(37, 122);
__INST(43, null); __INST(43, null, __INST(43, null, __INST(43, null, console).log)(__INST(55, null, "asdf")));
__INST(73, null); let b = __INST(81, null, 3);
__INST(88, null); let c = __INST(96, null, 4);
__INST(103, null); let a = __INST(111, null, __INST(111, null, b) + __INST(115, null, 1) + __INST(119, null, c));
}
__INST(125, __INST(125, asdf2)());
__INST(125, null);__INST(125, null, __INST(125, null, asdf2)());
6 changes: 3 additions & 3 deletions inst_experiments/run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let offsetsCovered = new Set();
let offsetsCovered = [];

function __INST(offset, expr) {
offsetsCovered.add(offset);
function __INST(start, end, expr = undefined) {
offsetsCovered.push([start, end]);
return expr;
}

Expand Down
24 changes: 16 additions & 8 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1757,8 +1757,9 @@ const (

func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFlags) {

// findme
p.print(fmt.Sprintf("__INST(%d, ", expr.Loc.Start))
// findme
// instrument
p.print(fmt.Sprintf("__INST(%d, null, ", expr.Loc.Start))

// If syntax compression is enabled, do a pre-pass over unary and binary
// operators to inline bitwise operations of cross-module inlined constants.
Expand Down Expand Up @@ -2958,8 +2959,8 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla
panic(fmt.Sprintf("Unexpected expression of type %T", expr.Data))
}

// findme
p.print(")")
// findme
p.print(")")
// p.print(fmt.Sprintf("/**(end-ex-%d)*/", expr.Loc.Start))
}

Expand Down Expand Up @@ -3363,13 +3364,17 @@ func (p *printer) printBody(body js_ast.Stmt) {
}

func (p *printer) printBlock(loc logger.Loc, block js_ast.SBlock) {
// findme
// findme
// p.print(fmt.Sprintf("/**{block-st-%d}*/", loc.Start))

p.addSourceMapping(loc)
p.print("{")
p.printNewline()

// instrument
p.print(fmt.Sprintf("__INST(%d, %d);", loc.Start, block.CloseBraceLoc.Start))
p.printNewline()

p.options.Indent++
for _, stmt := range block.Stmts {
p.printSemicolonIfNeeded()
Expand All @@ -3384,7 +3389,7 @@ func (p *printer) printBlock(loc logger.Loc, block js_ast.SBlock) {
}
p.print("}")

// findme
// findme
// p.print(fmt.Sprintf("/**{block-end-%d}*/", loc.Start))
}

Expand Down Expand Up @@ -3700,8 +3705,11 @@ const (

func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) {

// findme
// findme
// p.print(fmt.Sprintf("/**(start-st-%d)*/", stmt.Loc.Start))

// instrument
p.print(fmt.Sprintf("__INST(%d, null);", stmt.Loc.Start))

switch s := stmt.Data.(type) {
case *js_ast.SComment:
Expand Down Expand Up @@ -4470,7 +4478,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) {
panic(fmt.Sprintf("Unexpected statement of type %T", stmt.Data))
}

// findme
// findme
// p.print(fmt.Sprintf("/**(end-st-%d)*/", stmt.Loc.Start))
}

Expand Down

0 comments on commit fa135dc

Please sign in to comment.