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

Side by Side 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: Fix the tests. 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/compiler/implementation/leg.dart ('k') | tests/compiler/dart2js/parser_helper.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library('source_map_builder');
6
7 #import('dart:json');
8
9 #import('source_file.dart');
10
11 class SourceMappingEntry {
12 SourceFile sourceFile;
13 int sourceOffset;
14 int targetOffset;
15 String sourceName;
16
17 SourceMappingEntry(this.sourceFile,
18 this.sourceOffset,
19 this.targetOffset,
20 [this.sourceName]);
21 }
22
23 class SourceMapBuilder {
24 static final int VLQ_BASE_SHIFT = 5;
25 static final int VLQ_BASE_MASK = (1 << 5) - 1;
26 static final int VLQ_CONTINUATION_BIT = 1 << 5;
27 static final int VLQ_CONTINUATION_MASK = 1 << 5;
28 static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn'
29 'opqrstuvwxyz0123456789+/';
30
31 Map<String, int> sourceUrlMap;
32 List<String> sourceUrlList;
33 Map<String, int> sourceNameMap;
34 List<String> sourceNameList;
35
36 int previousTargetLine;
37 int previousTargetColumn;
38 int previousSourceUrlIndex;
39 int previousSourceLine;
40 int previousSourceColumn;
41 int previousSourceNameIndex;
42 bool firstEntryInLine;
43
44 SourceMapBuilder() {
45 sourceUrlMap = new Map<String, int>();
46 sourceUrlList = new List<String>();
47 sourceNameMap = new Map<String, int>();
48 sourceNameList = new List<String>();
49
50 previousTargetLine = 0;
51 previousTargetColumn = 0;
52 previousSourceUrlIndex = 0;
53 previousSourceLine = 0;
54 previousSourceColumn = 0;
55 previousSourceNameIndex = 0;
56 firstEntryInLine = true;
57 }
58
59 String build(List<SourceMappingEntry> mappingEntries, SourceFile targetFile) {
60 StringBuffer buffer = new StringBuffer();
61 buffer.add('{\n');
62 buffer.add(' "version": 3,\n');
63 buffer.add(' "mappings": "');
64 mappingEntries.forEach((SourceMappingEntry entry) {
65 writeEntry(entry, targetFile, buffer);
66 });
67 buffer.add('",\n');
68 // TODO(podivilov): serialize lists directly to buffer.
69 buffer.add(' "sources": ${JSON.stringify(sourceUrlList)},\n');
70 buffer.add(' "names": ${JSON.stringify(sourceNameList)}\n');
71 buffer.add('}\n');
72 return buffer.toString();
73 }
74
75 void writeEntry(SourceMappingEntry entry,
76 SourceFile targetFile,
77 StringBuffer output) {
78 if (entry.sourceFile === null) {
79 return;
80 }
81 int targetLine = targetFile.getLine(entry.targetOffset);
82 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset);
83 String sourceUrl = entry.sourceFile.filename;
84 int sourceLine = entry.sourceFile.getLine(entry.sourceOffset);
85 int sourceColumn = entry.sourceFile.getColumn(sourceLine,
86 entry.sourceOffset);
87 String sourceName = entry.sourceName;
88
89 if (targetLine > previousTargetLine) {
90 for (int i = previousTargetLine; i < targetLine; ++i) {
91 output.add(';');
92 }
93 previousTargetLine = targetLine;
94 previousTargetColumn = 0;
95 firstEntryInLine = true;
96 }
97
98 if (!firstEntryInLine) {
99 output.add(',');
100 }
101 firstEntryInLine = false;
102
103 encodeVLQ(output, targetColumn - previousTargetColumn);
104 previousTargetColumn = targetColumn;
105
106 if (sourceUrl === null)
107 return;
108
109 int sourceUrlIndex = indexOf(sourceUrlList, sourceUrl, sourceUrlMap);
110 encodeVLQ(output, sourceUrlIndex - previousSourceUrlIndex);
111 previousSourceUrlIndex = sourceUrlIndex;
112
113 encodeVLQ(output, sourceLine - previousSourceLine);
114 previousSourceLine = sourceLine;
115 encodeVLQ(output, sourceColumn - previousSourceColumn);
116 previousSourceColumn = sourceColumn;
117
118 if (sourceName === null)
119 return;
120
121 int sourceNameIndex = indexOf(sourceNameList, sourceName, sourceNameMap);
122 encodeVLQ(output, sourceNameIndex - previousSourceNameIndex);
123 previousSourceNameIndex = sourceNameIndex;
124 }
125
126 int indexOf(List<String> list, String value, Map<String, int> map) {
127 return map.putIfAbsent(value, () {
128 int index = list.length;
129 map[value] = index;
130 list.add(value);
131 return index;
132 });
133 }
134
135 static void encodeVLQ(StringBuffer output, int value) {
136 int signBit = 0;
137 if (value < 0) {
138 signBit = 1;
139 value = -value;
140 }
141 value = (value << 1) | signBit;
142 do {
143 int digit = value & VLQ_BASE_MASK;
144 value >>= VLQ_BASE_SHIFT;
145 if (value > 0) {
146 digit |= VLQ_CONTINUATION_BIT;
147 }
148 output.add(BASE64_DIGITS[digit]);
149 } while (value > 0);
150 }
151 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/leg.dart ('k') | tests/compiler/dart2js/parser_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698