OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.source_map_builder; | 5 library dart2js.source_map_builder; |
6 | 6 |
7 import '../util/util.dart'; | 7 import '../util/util.dart'; |
8 import '../scanner/scannerlib.dart' show Token; | 8 import '../scanner/scannerlib.dart' show Token; |
9 import '../source_file.dart'; | |
10 import '../util/uri_extras.dart' show relativize; | 9 import '../util/uri_extras.dart' show relativize; |
| 10 import 'line_column_provider.dart'; |
| 11 import 'source_file.dart'; |
11 | 12 |
12 class SourceMapBuilder { | 13 class SourceMapBuilder { |
13 static const int VLQ_BASE_SHIFT = 5; | 14 static const int VLQ_BASE_SHIFT = 5; |
14 static const int VLQ_BASE_MASK = (1 << 5) - 1; | 15 static const int VLQ_BASE_MASK = (1 << 5) - 1; |
15 static const int VLQ_CONTINUATION_BIT = 1 << 5; | 16 static const int VLQ_CONTINUATION_BIT = 1 << 5; |
16 static const int VLQ_CONTINUATION_MASK = 1 << 5; | 17 static const int VLQ_CONTINUATION_MASK = 1 << 5; |
17 static const String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn' | 18 static const String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn' |
18 'opqrstuvwxyz0123456789+/'; | 19 'opqrstuvwxyz0123456789+/'; |
19 | 20 |
20 final Uri uri; | 21 final Uri uri; |
21 final Uri fileUri; | 22 final Uri fileUri; |
22 | 23 |
23 SourceFile targetFile; | 24 LineColumnProvider lineColumnProvider; |
24 List<SourceMapEntry> entries; | 25 List<SourceMapEntry> entries; |
25 | 26 |
26 Map<String, int> sourceUrlMap; | 27 Map<String, int> sourceUrlMap; |
27 List<String> sourceUrlList; | 28 List<String> sourceUrlList; |
28 Map<String, int> sourceNameMap; | 29 Map<String, int> sourceNameMap; |
29 List<String> sourceNameList; | 30 List<String> sourceNameList; |
30 | 31 |
31 int previousTargetLine; | 32 int previousTargetLine; |
32 int previousTargetColumn; | 33 int previousTargetColumn; |
33 int previousSourceUrlIndex; | 34 int previousSourceUrlIndex; |
34 int previousSourceLine; | 35 int previousSourceLine; |
35 int previousSourceColumn; | 36 int previousSourceColumn; |
36 int previousSourceNameIndex; | 37 int previousSourceNameIndex; |
37 bool firstEntryInLine; | 38 bool firstEntryInLine; |
38 | 39 |
39 SourceMapBuilder(this.uri, this.fileUri, this.targetFile) { | 40 SourceMapBuilder(this.uri, this.fileUri, this.lineColumnProvider) { |
40 entries = new List<SourceMapEntry>(); | 41 entries = new List<SourceMapEntry>(); |
41 | 42 |
42 sourceUrlMap = new Map<String, int>(); | 43 sourceUrlMap = new Map<String, int>(); |
43 sourceUrlList = new List<String>(); | 44 sourceUrlList = new List<String>(); |
44 sourceNameMap = new Map<String, int>(); | 45 sourceNameMap = new Map<String, int>(); |
45 sourceNameList = new List<String>(); | 46 sourceNameList = new List<String>(); |
46 | 47 |
47 previousTargetLine = 0; | 48 previousTargetLine = 0; |
48 previousTargetColumn = 0; | 49 previousTargetColumn = 0; |
49 previousSourceUrlIndex = 0; | 50 previousSourceUrlIndex = 0; |
(...skipping 30 matching lines...) Expand all Loading... |
80 indexOf(sourceUrlList, sourceLocation.getSourceUrl(), sourceUrlMap); | 81 indexOf(sourceUrlList, sourceLocation.getSourceUrl(), sourceUrlMap); |
81 return | 82 return |
82 sourceUrlIndex == previousSourceUrlIndex && | 83 sourceUrlIndex == previousSourceUrlIndex && |
83 sourceLocation.getLine() == previousSourceLine && | 84 sourceLocation.getLine() == previousSourceLine && |
84 sourceLocation.getColumn() == previousSourceColumn; | 85 sourceLocation.getColumn() == previousSourceColumn; |
85 } | 86 } |
86 | 87 |
87 void addMapping(int targetOffset, SourceFileLocation sourceLocation) { | 88 void addMapping(int targetOffset, SourceFileLocation sourceLocation) { |
88 | 89 |
89 bool sameLine(int position, otherPosition) { | 90 bool sameLine(int position, otherPosition) { |
90 return targetFile.getLine(position) == targetFile.getLine(otherPosition); | 91 return lineColumnProvider.getLine(position) == |
| 92 lineColumnProvider.getLine(otherPosition); |
91 } | 93 } |
92 | 94 |
93 if (!entries.isEmpty && sameLine(targetOffset, entries.last.targetOffset)) { | 95 if (!entries.isEmpty && sameLine(targetOffset, entries.last.targetOffset)) { |
94 if (sameAsPreviousLocation(sourceLocation)) { | 96 if (sameAsPreviousLocation(sourceLocation)) { |
95 // The entry points to the same source location as the previous entry in | 97 // The entry points to the same source location as the previous entry in |
96 // the same line, hence it is not needed for the source map. | 98 // the same line, hence it is not needed for the source map. |
97 // | 99 // |
98 // TODO(zarah): Remove this check and make sure that [addMapping] is not | 100 // TODO(zarah): Remove this check and make sure that [addMapping] is not |
99 // called for this position. Instead, when consecutive lines in the | 101 // called for this position. Instead, when consecutive lines in the |
100 // generated code point to the same source location, record this and use | 102 // generated code point to the same source location, record this and use |
(...skipping 17 matching lines...) Expand all Loading... |
118 writeJsonEscapedCharsOn(string, buffer); | 120 writeJsonEscapedCharsOn(string, buffer); |
119 buffer.write('"'); | 121 buffer.write('"'); |
120 first = false; | 122 first = false; |
121 } | 123 } |
122 buffer.write(']'); | 124 buffer.write(']'); |
123 } | 125 } |
124 | 126 |
125 String build() { | 127 String build() { |
126 resetPreviousSourceLocation(); | 128 resetPreviousSourceLocation(); |
127 StringBuffer mappingsBuffer = new StringBuffer(); | 129 StringBuffer mappingsBuffer = new StringBuffer(); |
128 entries.forEach((SourceMapEntry entry) => writeEntry(entry, targetFile, | 130 entries.forEach((SourceMapEntry entry) { |
129 mappingsBuffer)); | 131 writeEntry(entry, mappingsBuffer); |
| 132 }); |
130 StringBuffer buffer = new StringBuffer(); | 133 StringBuffer buffer = new StringBuffer(); |
131 buffer.write('{\n'); | 134 buffer.write('{\n'); |
132 buffer.write(' "version": 3,\n'); | 135 buffer.write(' "version": 3,\n'); |
133 if (uri != null && fileUri != null) { | 136 if (uri != null && fileUri != null) { |
134 buffer.write(' "file": "${relativize(uri, fileUri, false)}",\n'); | 137 buffer.write(' "file": "${relativize(uri, fileUri, false)}",\n'); |
135 } | 138 } |
136 buffer.write(' "sourceRoot": "",\n'); | 139 buffer.write(' "sourceRoot": "",\n'); |
137 buffer.write(' "sources": '); | 140 buffer.write(' "sources": '); |
138 if (uri != null) { | 141 if (uri != null) { |
139 sourceUrlList = | 142 sourceUrlList = |
140 sourceUrlList.map((url) => relativize(uri, Uri.parse(url), false)) | 143 sourceUrlList.map((url) => relativize(uri, Uri.parse(url), false)) |
141 .toList(); | 144 .toList(); |
142 } | 145 } |
143 printStringListOn(sourceUrlList, buffer); | 146 printStringListOn(sourceUrlList, buffer); |
144 buffer.write(',\n'); | 147 buffer.write(',\n'); |
145 buffer.write(' "names": '); | 148 buffer.write(' "names": '); |
146 printStringListOn(sourceNameList, buffer); | 149 printStringListOn(sourceNameList, buffer); |
147 buffer.write(',\n'); | 150 buffer.write(',\n'); |
148 buffer.write(' "mappings": "'); | 151 buffer.write(' "mappings": "'); |
149 buffer.write(mappingsBuffer); | 152 buffer.write(mappingsBuffer); |
150 buffer.write('"\n}\n'); | 153 buffer.write('"\n}\n'); |
151 return buffer.toString(); | 154 return buffer.toString(); |
152 } | 155 } |
153 | 156 |
154 void writeEntry(SourceMapEntry entry, SourceFile targetFile, StringBuffer outp
ut) { | 157 void writeEntry(SourceMapEntry entry, StringBuffer output) { |
155 int targetLine = targetFile.getLine(entry.targetOffset); | 158 int targetLine = lineColumnProvider.getLine(entry.targetOffset); |
156 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset); | 159 int targetColumn = |
| 160 lineColumnProvider.getColumn(targetLine, entry.targetOffset); |
157 | 161 |
158 if (targetLine > previousTargetLine) { | 162 if (targetLine > previousTargetLine) { |
159 for (int i = previousTargetLine; i < targetLine; ++i) { | 163 for (int i = previousTargetLine; i < targetLine; ++i) { |
160 output.write(';'); | 164 output.write(';'); |
161 } | 165 } |
162 previousTargetLine = targetLine; | 166 previousTargetLine = targetLine; |
163 previousTargetColumn = 0; | 167 previousTargetColumn = 0; |
164 firstEntryInLine = true; | 168 firstEntryInLine = true; |
165 } | 169 } |
166 | 170 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 262 |
259 TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name) | 263 TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name) |
260 : super(sourceFile); | 264 : super(sourceFile); |
261 | 265 |
262 int get offset => token.charOffset; | 266 int get offset => token.charOffset; |
263 | 267 |
264 String getSourceName() { | 268 String getSourceName() { |
265 return name; | 269 return name; |
266 } | 270 } |
267 } | 271 } |
OLD | NEW |