| 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 '../../compiler_new.dart' show OutputSink, OutputType; |
| 7 import '../util/uri_extras.dart' show relativize; | 8 import '../util/uri_extras.dart' show relativize; |
| 8 import '../util/util.dart'; | 9 import '../util/util.dart'; |
| 9 import 'line_column_provider.dart'; | 10 import 'line_column_provider.dart'; |
| 11 import 'code_output.dart' show SourceLocationsProvider, SourceLocations; |
| 10 import 'source_information.dart' show SourceLocation; | 12 import 'source_information.dart' show SourceLocation; |
| 11 | 13 |
| 12 class SourceMapBuilder { | 14 class SourceMapBuilder { |
| 15 final String version; |
| 16 |
| 13 /// The URI of the source map file. | 17 /// The URI of the source map file. |
| 14 final Uri sourceMapUri; | 18 final Uri sourceMapUri; |
| 15 | 19 |
| 16 /// The URI of the target language file. | 20 /// The URI of the target language file. |
| 17 final Uri targetFileUri; | 21 final Uri targetFileUri; |
| 18 | 22 |
| 19 final LineColumnProvider lineColumnProvider; | 23 final LineColumnProvider lineColumnProvider; |
| 20 final List<SourceMapEntry> entries = new List<SourceMapEntry>(); | 24 final List<SourceMapEntry> entries = new List<SourceMapEntry>(); |
| 21 | 25 |
| 22 SourceMapBuilder( | 26 SourceMapBuilder(this.version, this.sourceMapUri, this.targetFileUri, |
| 23 this.sourceMapUri, this.targetFileUri, this.lineColumnProvider); | 27 this.lineColumnProvider); |
| 24 | 28 |
| 25 void addMapping(int targetOffset, SourceLocation sourceLocation) { | 29 void addMapping(int targetOffset, SourceLocation sourceLocation) { |
| 26 entries.add(new SourceMapEntry(sourceLocation, targetOffset)); | 30 entries.add(new SourceMapEntry(sourceLocation, targetOffset)); |
| 27 } | 31 } |
| 28 | 32 |
| 29 void printStringListOn(Iterable<String> strings, StringBuffer buffer) { | 33 void printStringListOn(Iterable<String> strings, StringBuffer buffer) { |
| 30 bool first = true; | 34 bool first = true; |
| 31 buffer.write('['); | 35 buffer.write('['); |
| 32 for (String string in strings) { | 36 for (String string in strings) { |
| 33 if (!first) buffer.write(','); | 37 if (!first) buffer.write(','); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } | 84 } |
| 81 } | 85 } |
| 82 }); | 86 }); |
| 83 | 87 |
| 84 StringBuffer mappingsBuffer = new StringBuffer(); | 88 StringBuffer mappingsBuffer = new StringBuffer(); |
| 85 writeEntries(lineColumnMap, uriMap, nameMap, mappingsBuffer); | 89 writeEntries(lineColumnMap, uriMap, nameMap, mappingsBuffer); |
| 86 | 90 |
| 87 StringBuffer buffer = new StringBuffer(); | 91 StringBuffer buffer = new StringBuffer(); |
| 88 buffer.write('{\n'); | 92 buffer.write('{\n'); |
| 89 buffer.write(' "version": 3,\n'); | 93 buffer.write(' "version": 3,\n'); |
| 94 buffer.write(' "engine": "$version",\n'); |
| 90 if (sourceMapUri != null && targetFileUri != null) { | 95 if (sourceMapUri != null && targetFileUri != null) { |
| 91 buffer.write( | 96 buffer.write( |
| 92 ' "file": "${relativize(sourceMapUri, targetFileUri, false)}",\n'); | 97 ' "file": "${relativize(sourceMapUri, targetFileUri, false)}",\n'); |
| 93 } | 98 } |
| 94 buffer.write(' "sourceRoot": "",\n'); | 99 buffer.write(' "sourceRoot": "",\n'); |
| 95 buffer.write(' "sources": '); | 100 buffer.write(' "sources": '); |
| 96 Iterable<String> relativeSourceUriList = const <String>[]; | 101 Iterable<String> relativeSourceUriList = const <String>[]; |
| 97 if (sourceMapUri != null) { | 102 if (sourceMapUri != null) { |
| 98 relativeSourceUriList = | 103 relativeSourceUriList = |
| 99 uriMap.elements.map((u) => relativize(sourceMapUri, u, false)); | 104 uriMap.elements.map((u) => relativize(sourceMapUri, u, false)); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 160 } |
| 156 | 161 |
| 157 String sourceName = sourceLocation.sourceName; | 162 String sourceName = sourceLocation.sourceName; |
| 158 if (sourceName != null) { | 163 if (sourceName != null) { |
| 159 sourceNameIndexEncoder.encode(output, nameMap[sourceName]); | 164 sourceNameIndexEncoder.encode(output, nameMap[sourceName]); |
| 160 } | 165 } |
| 161 | 166 |
| 162 previousSourceLocation = sourceLocation; | 167 previousSourceLocation = sourceLocation; |
| 163 }); | 168 }); |
| 164 } | 169 } |
| 170 |
| 171 /// Returns the source map tag to put at the end a .js file in [fileUri] to |
| 172 /// make it point to the source map file in [sourceMapUri]. |
| 173 static String generateSourceMapTag(Uri sourceMapUri, Uri fileUri) { |
| 174 if (sourceMapUri != null && fileUri != null) { |
| 175 String sourceMapFileName = relativize(fileUri, sourceMapUri, false); |
| 176 return ''' |
| 177 |
| 178 //# sourceMappingURL=$sourceMapFileName |
| 179 '''; |
| 180 } |
| 181 return ''; |
| 182 } |
| 183 |
| 184 /// Generates source map files for all [SourceLocations] in |
| 185 /// [sourceLocationsProvider] for the .js code in [lineColumnProvider] |
| 186 /// [sourceMapUri] is used to relativizes the URIs of the referenced source |
| 187 /// files and the target [fileUri]. [name] and [outputProvider] are used to |
| 188 /// create the [OutputSink] for the source map text. |
| 189 static void outputSourceMap( |
| 190 SourceLocationsProvider sourceLocationsProvider, |
| 191 LineColumnProvider lineColumnProvider, |
| 192 String name, |
| 193 Uri sourceMapUri, |
| 194 Uri fileUri, |
| 195 OutputSink outputProvider( |
| 196 String name, String extension, OutputType type)) { |
| 197 // Create a source file for the compilation output. This allows using |
| 198 // [:getLine:] to transform offsets to line numbers in [SourceMapBuilder]. |
| 199 int index = 0; |
| 200 sourceLocationsProvider.sourceLocations |
| 201 .forEach((SourceLocations sourceLocations) { |
| 202 SourceMapBuilder sourceMapBuilder = new SourceMapBuilder( |
| 203 sourceLocations.name, sourceMapUri, fileUri, lineColumnProvider); |
| 204 sourceLocations.forEachSourceLocation(sourceMapBuilder.addMapping); |
| 205 String sourceMap = sourceMapBuilder.build(); |
| 206 String extension = 'js.map'; |
| 207 if (index > 0) { |
| 208 if (name == '') { |
| 209 name = fileUri != null ? fileUri.pathSegments.last : 'out.js'; |
| 210 extension = 'map.${sourceLocations.name}'; |
| 211 } else { |
| 212 extension = 'js.map.${sourceLocations.name}'; |
| 213 } |
| 214 } |
| 215 outputProvider(name, extension, OutputType.js_map) |
| 216 ..add(sourceMap) |
| 217 ..close(); |
| 218 index++; |
| 219 }); |
| 220 } |
| 165 } | 221 } |
| 166 | 222 |
| 167 /// Encoder for value deltas in VLQ format. | 223 /// Encoder for value deltas in VLQ format. |
| 168 class DeltaEncoder { | 224 class DeltaEncoder { |
| 169 /// The last emitted value of the encoder. | 225 /// The last emitted value of the encoder. |
| 170 int _value = 0; | 226 int _value = 0; |
| 171 | 227 |
| 172 /// Reset the encoder to its initial state. | 228 /// Reset the encoder to its initial state. |
| 173 void reset() { | 229 void reset() { |
| 174 _value = 0; | 230 _value = 0; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 int register(T element) { | 354 int register(T element) { |
| 299 return map.putIfAbsent(element, () => map.length); | 355 return map.putIfAbsent(element, () => map.length); |
| 300 } | 356 } |
| 301 | 357 |
| 302 /// Returns the index of [element]. | 358 /// Returns the index of [element]. |
| 303 int operator [](T element) => map[element]; | 359 int operator [](T element) => map[element]; |
| 304 | 360 |
| 305 /// Returns the indexed elements. | 361 /// Returns the indexed elements. |
| 306 Iterable<T> get elements => map.keys; | 362 Iterable<T> get elements => map.keys; |
| 307 } | 363 } |
| OLD | NEW |