Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Unified Diff: lib/compiler/implementation/source_map_builder.dart

Issue 10579019: First shot at source maps generation in dart2js. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compiler/implementation/leg.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/source_map_builder.dart
diff --git a/lib/compiler/implementation/source_map_builder.dart b/lib/compiler/implementation/source_map_builder.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7e4086cef1f035aec207ed28132198faba176ff1
--- /dev/null
+++ b/lib/compiler/implementation/source_map_builder.dart
@@ -0,0 +1,147 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#library('source_map_builder');
+
+#import('dart:json');
+
+#import('source_file.dart');
+
+class SourceMappingEntry {
+ SourceFile sourceFile;
+ int sourceOffset;
+ int targetOffset;
+ String sourceName;
+
+ SourceMappingEntry(this.sourceFile,
+ this.sourceOffset,
+ this.targetOffset,
+ [this.sourceName]);
+}
+
+class SourceMapBuilder {
+ static final int VLQ_BASE_SHIFT = 5;
+ static final int VLQ_BASE_MASK = (1 << 5) - 1;
+ static final int VLQ_CONTINUATION_BIT = 1 << 5;
+ static final int VLQ_CONTINUATION_MASK = 1 << 5;
+ static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn'
+ 'opqrstuvwxyz0123456789+/';
+
+ Map<String, int> sourceURLMap;
ahe 2012/06/21 08:12:13 Forgot to mention this earlier: we try to use came
podivilov 2012/06/21 09:50:42 Done.
+ List<String> sourceURLList;
+ Map<String, int> sourceNameMap;
+ List<String> sourceNameList;
+
+ int previousTargetLine;
+ int previousTargetColumn;
+ int previousSourceURLIndex;
+ int previousSourceLine;
+ int previousSourceColumn;
+ int previousSourceNameIndex;
+ bool firstEntryInLine;
+
+ SourceMapBuilder() {
+ sourceURLMap = new Map<String, int>();
+ sourceURLList = new List<String>();
+ sourceNameMap = new Map<String, int>();
+ sourceNameList = new List<String>();
+
+ previousTargetLine = 0;
+ previousTargetColumn = 0;
+ previousSourceURLIndex = 0;
+ previousSourceLine = 0;
+ previousSourceColumn = 0;
+ previousSourceNameIndex = 0;
+ firstEntryInLine = true;
+ }
+
+ String build(List<SourceMappingEntry> mappingEntries, SourceFile targetFile) {
+ StringBuffer buffer = new StringBuffer();
+ buffer.add('{\n');
+ buffer.add(' "version": 3,\n');
+ buffer.add(' "mappings": "');
+ mappingEntries.forEach((SourceMappingEntry entry) {
+ writeEntry(entry, targetFile, buffer);
+ });
+ buffer.add('",\n');
+ buffer.add(' "sources": ${JSON.stringify(sourceURLList)},\n');
+ buffer.add(' "names": ${JSON.stringify(sourceNameList)}\n');
+ buffer.add('}\n');
+ return buffer.toString();
ahe 2012/06/21 08:12:13 Let's say sourceURLList contains the string "http:
podivilov 2012/06/21 09:50:42 Added TODO to migrate to fast version when corresp
+ }
+
+ void writeEntry(SourceMappingEntry entry,
+ SourceFile targetFile,
+ StringBuffer output) {
+ int targetLine = targetFile.getLine(entry.targetOffset);
+ int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset);
+ String sourceURL = entry.sourceFile.filename;
+ int sourceLine = entry.sourceFile.getLine(entry.sourceOffset);
+ int sourceColumn = entry.sourceFile.getColumn(sourceLine,
+ entry.sourceOffset);
+ String sourceName = entry.sourceName;
+
+ if (targetLine > previousTargetLine) {
+ for (int i = previousTargetLine; i < targetLine; ++i) {
+ output.add(';');
+ }
+ previousTargetLine = targetLine;
+ previousTargetColumn = 0;
+ firstEntryInLine = true;
+ }
+
+ if (!firstEntryInLine) {
+ output.add(',');
+ }
+ firstEntryInLine = false;
+
+ encodeVLQ(output, targetColumn - previousTargetColumn);
+ previousTargetColumn = targetColumn;
+
+ if (sourceURL === null)
+ return;
+
+ int sourceURLIndex = indexOf(sourceURLList, sourceURL, sourceURLMap);
+ encodeVLQ(output, sourceURLIndex - previousSourceURLIndex);
+ previousSourceURLIndex = sourceURLIndex;
+
+ encodeVLQ(output, sourceLine - previousSourceLine);
+ previousSourceLine = sourceLine;
+ encodeVLQ(output, sourceColumn - previousSourceColumn);
+ previousSourceColumn = sourceColumn;
+
+ if (sourceName === null)
+ return;
+
+ int sourceNameIndex = indexOf(sourceNameList, sourceName, sourceNameMap);
+ encodeVLQ(output, sourceNameIndex - previousSourceNameIndex);
+ previousSourceNameIndex = sourceNameIndex;
+ }
+
+ int indexOf(List<String> list, String value, Map<String, int> map) {
+ return map.putIfAbsent(value, () {
+ int index = list.length;
+ map[value] = index;
+ list.add(value);
+ return index;
+ });
+ }
+
+ static void encodeVLQ(StringBuffer output, int value) {
+ int signBit = 0;
+ if (value < 0) {
+ signBit = 1;
+ value = -value;
+ }
+ value = (value << 1) | signBit;
+ do {
+ int digit = value & VLQ_BASE_MASK;
+ value >>= VLQ_BASE_SHIFT;
+ if (value > 0) {
+ digit |= VLQ_CONTINUATION_BIT;
+ }
+ output.add(BASE64_DIGITS[digit]);
+ } while (value > 0);
+ }
+}
« no previous file with comments | « lib/compiler/implementation/leg.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698