Chromium Code Reviews| 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) == lineColumnProvider.getLine( otherPosition); |
|
floitsch
2015/01/09 16:35:19
long line.
Johnni Winther
2015/01/13 08:53:05
Done.
| |
| 91 } | 92 } |
| 92 | 93 |
| 93 if (!entries.isEmpty && sameLine(targetOffset, entries.last.targetOffset)) { | 94 if (!entries.isEmpty && sameLine(targetOffset, entries.last.targetOffset)) { |
| 94 if (sameAsPreviousLocation(sourceLocation)) { | 95 if (sameAsPreviousLocation(sourceLocation)) { |
| 95 // The entry points to the same source location as the previous entry in | 96 // 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. | 97 // the same line, hence it is not needed for the source map. |
| 97 // | 98 // |
| 98 // TODO(zarah): Remove this check and make sure that [addMapping] is not | 99 // TODO(zarah): Remove this check and make sure that [addMapping] is not |
| 99 // called for this position. Instead, when consecutive lines in the | 100 // called for this position. Instead, when consecutive lines in the |
| 100 // generated code point to the same source location, record this and use | 101 // generated code point to the same source location, record this and use |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 118 writeJsonEscapedCharsOn(string, buffer); | 119 writeJsonEscapedCharsOn(string, buffer); |
| 119 buffer.write('"'); | 120 buffer.write('"'); |
| 120 first = false; | 121 first = false; |
| 121 } | 122 } |
| 122 buffer.write(']'); | 123 buffer.write(']'); |
| 123 } | 124 } |
| 124 | 125 |
| 125 String build() { | 126 String build() { |
| 126 resetPreviousSourceLocation(); | 127 resetPreviousSourceLocation(); |
| 127 StringBuffer mappingsBuffer = new StringBuffer(); | 128 StringBuffer mappingsBuffer = new StringBuffer(); |
| 128 entries.forEach((SourceMapEntry entry) => writeEntry(entry, targetFile, | 129 entries.forEach((SourceMapEntry entry) { |
| 129 mappingsBuffer)); | 130 writeEntry(entry, mappingsBuffer); |
| 131 }); | |
| 130 StringBuffer buffer = new StringBuffer(); | 132 StringBuffer buffer = new StringBuffer(); |
| 131 buffer.write('{\n'); | 133 buffer.write('{\n'); |
| 132 buffer.write(' "version": 3,\n'); | 134 buffer.write(' "version": 3,\n'); |
| 133 if (uri != null && fileUri != null) { | 135 if (uri != null && fileUri != null) { |
| 134 buffer.write(' "file": "${relativize(uri, fileUri, false)}",\n'); | 136 buffer.write(' "file": "${relativize(uri, fileUri, false)}",\n'); |
| 135 } | 137 } |
| 136 buffer.write(' "sourceRoot": "",\n'); | 138 buffer.write(' "sourceRoot": "",\n'); |
| 137 buffer.write(' "sources": '); | 139 buffer.write(' "sources": '); |
| 138 if (uri != null) { | 140 if (uri != null) { |
| 139 sourceUrlList = | 141 sourceUrlList = |
| 140 sourceUrlList.map((url) => relativize(uri, Uri.parse(url), false)) | 142 sourceUrlList.map((url) => relativize(uri, Uri.parse(url), false)) |
| 141 .toList(); | 143 .toList(); |
| 142 } | 144 } |
| 143 printStringListOn(sourceUrlList, buffer); | 145 printStringListOn(sourceUrlList, buffer); |
| 144 buffer.write(',\n'); | 146 buffer.write(',\n'); |
| 145 buffer.write(' "names": '); | 147 buffer.write(' "names": '); |
| 146 printStringListOn(sourceNameList, buffer); | 148 printStringListOn(sourceNameList, buffer); |
| 147 buffer.write(',\n'); | 149 buffer.write(',\n'); |
| 148 buffer.write(' "mappings": "'); | 150 buffer.write(' "mappings": "'); |
| 149 buffer.write(mappingsBuffer); | 151 buffer.write(mappingsBuffer); |
| 150 buffer.write('"\n}\n'); | 152 buffer.write('"\n}\n'); |
| 151 return buffer.toString(); | 153 return buffer.toString(); |
| 152 } | 154 } |
| 153 | 155 |
| 154 void writeEntry(SourceMapEntry entry, SourceFile targetFile, StringBuffer outp ut) { | 156 void writeEntry(SourceMapEntry entry, StringBuffer output) { |
| 155 int targetLine = targetFile.getLine(entry.targetOffset); | 157 int targetLine = lineColumnProvider.getLine(entry.targetOffset); |
| 156 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset); | 158 int targetColumn = |
| 159 lineColumnProvider.getColumn(targetLine, entry.targetOffset); | |
| 157 | 160 |
| 158 if (targetLine > previousTargetLine) { | 161 if (targetLine > previousTargetLine) { |
| 159 for (int i = previousTargetLine; i < targetLine; ++i) { | 162 for (int i = previousTargetLine; i < targetLine; ++i) { |
| 160 output.write(';'); | 163 output.write(';'); |
| 161 } | 164 } |
| 162 previousTargetLine = targetLine; | 165 previousTargetLine = targetLine; |
| 163 previousTargetColumn = 0; | 166 previousTargetColumn = 0; |
| 164 firstEntryInLine = true; | 167 firstEntryInLine = true; |
| 165 } | 168 } |
| 166 | 169 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 | 261 |
| 259 TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name) | 262 TokenSourceFileLocation(SourceFile sourceFile, this.token, this.name) |
| 260 : super(sourceFile); | 263 : super(sourceFile); |
| 261 | 264 |
| 262 int get offset => token.charOffset; | 265 int get offset => token.charOffset; |
| 263 | 266 |
| 264 String getSourceName() { | 267 String getSourceName() { |
| 265 return name; | 268 return name; |
| 266 } | 269 } |
| 267 } | 270 } |
| OLD | NEW |