Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library sourcemap.js_tracer; | |
| 6 | |
| 7 import 'package:compiler/src/io/source_information.dart'; | |
| 8 import 'package:compiler/src/io/position_information.dart'; | |
| 9 import 'package:compiler/src/js/js.dart' as js; | |
| 10 import 'sourcemap_helper.dart'; | |
| 11 import 'trace_graph.dart'; | |
| 12 | |
| 13 /// Create a [TraceGraph] for [info] registering usage in [coverage]. | |
| 14 TraceGraph createTraceGraph(SourceMapInfo info, Coverage coverage) { | |
|
Siggi Cherem (dart-lang)
2016/01/21 18:18:34
should this function move to trace_graph.dart? Or
Johnni Winther
2016/01/22 15:12:38
This for JS trace graphs, later we will have Dart
| |
| 15 TraceGraph graph = new TraceGraph(); | |
| 16 TraceListener listener = new StepTraceListener(graph); | |
| 17 CodePositionMap codePositions = | |
| 18 new CodePositionCoverage(info.jsCodePositions, coverage); | |
| 19 JavaScriptTracer tracer = new JavaScriptTracer( | |
| 20 codePositions, [new CoverageListener(coverage), listener]); | |
| 21 info.node.accept(tracer); | |
| 22 return graph; | |
| 23 } | |
| 24 | |
| 25 class StepTraceListener extends TraceListener { | |
| 26 Map<js.Node, TraceStep> steppableMap = <js.Node, TraceStep>{}; | |
| 27 final TraceGraph graph; | |
| 28 | |
| 29 StepTraceListener(this.graph); | |
| 30 | |
| 31 @override | |
| 32 void onStep(js.Node node, Offset offset, StepKind kind) { | |
| 33 SourceInformation sourceInformation = node.sourceInformation; | |
| 34 SourcePositionKind sourcePositionKind = SourcePositionKind.START; | |
| 35 List text = [node]; | |
| 36 switch (kind) { | |
| 37 case StepKind.FUN: | |
| 38 sourcePositionKind = SourcePositionKind.CLOSING; | |
| 39 text = ['<exit>']; | |
| 40 break; | |
| 41 case StepKind.CALL: | |
| 42 CallPosition callPosition = | |
| 43 CallPosition.getSemanticPositionForCall(node); | |
| 44 sourcePositionKind = callPosition.sourcePositionKind; | |
| 45 break; | |
| 46 case StepKind.NEW: | |
| 47 case StepKind.RETURN: | |
| 48 case StepKind.BREAK: | |
| 49 case StepKind.CONTINUE: | |
| 50 case StepKind.THROW: | |
| 51 case StepKind.EXPRESSION_STATEMENT: | |
| 52 break; | |
| 53 case StepKind.IF_CONDITION: | |
| 54 js.If ifNode = node; | |
| 55 text = ['if(', ifNode.condition, ') ...']; | |
| 56 break; | |
| 57 case StepKind.FOR_INITIALIZER: | |
| 58 js.For forNode = node; | |
| 59 text = ['for(', forNode.init, '; ...) ...']; | |
| 60 break; | |
| 61 case StepKind.FOR_CONDITION: | |
| 62 js.For forNode = node; | |
| 63 text = ['for(...;', forNode.condition, '; ...) ...']; | |
| 64 break; | |
| 65 case StepKind.FOR_UPDATE: | |
| 66 js.For forNode = node; | |
| 67 text = ['for(...; ...', forNode.update, ') ...']; | |
| 68 break; | |
| 69 case StepKind.WHILE_CONDITION: | |
| 70 js.While whileNode = node; | |
| 71 text = ['while(', whileNode.condition, ') ...']; | |
| 72 break; | |
| 73 case StepKind.DO_CONDITION: | |
| 74 js.Do doNode = node; | |
| 75 text = ['do {... } (', doNode.condition, ')']; | |
| 76 break; | |
| 77 case StepKind.SWITCH_EXPRESSION: | |
| 78 js.Switch switchNode = node; | |
| 79 text = ['switch(', switchNode.key, ') ...']; | |
| 80 break; | |
| 81 | |
| 82 } | |
| 83 createTraceStep( | |
| 84 node, | |
| 85 offset: offset, | |
| 86 sourceLocation: getSourceLocation( | |
| 87 node.sourceInformation, sourcePositionKind), | |
| 88 text: text); | |
| 89 } | |
| 90 | |
| 91 void createTraceStep( | |
| 92 js.Node node, | |
| 93 {Offset offset, | |
| 94 List text, | |
| 95 String note, | |
| 96 SourceLocation sourceLocation}) { | |
| 97 int id = steppableMap.length; | |
| 98 | |
| 99 if (text == null) { | |
| 100 text = [node]; | |
| 101 } | |
| 102 | |
| 103 TraceStep step = new TraceStep( | |
| 104 id, node, | |
| 105 offset, | |
| 106 text, sourceLocation); | |
| 107 graph.addStep(step); | |
| 108 | |
| 109 steppableMap[node] = step; | |
| 110 } | |
| 111 | |
| 112 | |
| 113 void pushBranch(BranchKind kind, [value]) { | |
| 114 var branch; | |
| 115 switch (kind) { | |
| 116 case BranchKind.CONDITION: | |
| 117 branch = value ? 't' : 'f'; | |
| 118 break; | |
| 119 case BranchKind.LOOP: | |
| 120 branch = 'l'; | |
| 121 break; | |
| 122 case BranchKind.CATCH: | |
| 123 branch = 'c'; | |
| 124 break; | |
| 125 case BranchKind.FINALLY: | |
| 126 branch = 'F'; | |
| 127 break; | |
| 128 case BranchKind.CASE: | |
| 129 branch = '$value'; | |
| 130 break; | |
| 131 } | |
| 132 graph.pushBranch(branch); | |
| 133 } | |
| 134 | |
| 135 void popBranch() { | |
| 136 graph.popBranch(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 SourceLocation getSourceLocation( | |
|
Siggi Cherem (dart-lang)
2016/01/21 18:18:34
seems you already agreed with my other suggestion
Johnni Winther
2016/01/22 15:12:38
Done.
| |
| 141 SourceInformation sourceInformation, | |
| 142 [SourcePositionKind sourcePositionKind = SourcePositionKind.START]) { | |
| 143 if (sourceInformation == null) return null; | |
| 144 switch (sourcePositionKind) { | |
| 145 case SourcePositionKind.START: | |
| 146 return sourceInformation.startPosition; | |
| 147 case SourcePositionKind.CLOSING: | |
| 148 return sourceInformation.closingPosition; | |
| 149 case SourcePositionKind.END: | |
| 150 return sourceInformation.endPosition; | |
| 151 } | |
| 152 } | |
| OLD | NEW |