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('source_map_builder'); | 5 #library('source_map_builder'); |
6 | 6 |
7 #import('dart:json'); | 7 #import('dart:json'); |
8 | 8 |
9 #import('source_file.dart'); | 9 #import('source_file.dart'); |
10 #import('ssa/ssa.dart'); | |
11 | 10 |
12 class SourceMapBuilder { | 11 class SourceMapBuilder { |
13 static final int VLQ_BASE_SHIFT = 5; | 12 static final int VLQ_BASE_SHIFT = 5; |
14 static final int VLQ_BASE_MASK = (1 << 5) - 1; | 13 static final int VLQ_BASE_MASK = (1 << 5) - 1; |
15 static final int VLQ_CONTINUATION_BIT = 1 << 5; | 14 static final int VLQ_CONTINUATION_BIT = 1 << 5; |
16 static final int VLQ_CONTINUATION_MASK = 1 << 5; | 15 static final int VLQ_CONTINUATION_MASK = 1 << 5; |
17 static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn' | 16 static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn' |
18 'opqrstuvwxyz0123456789+/'; | 17 'opqrstuvwxyz0123456789+/'; |
19 | 18 |
20 List<_Block> blocks; | 19 List<_Entry> entries; |
21 | 20 |
22 Map<String, int> sourceUrlMap; | 21 Map<String, int> sourceUrlMap; |
23 List<String> sourceUrlList; | 22 List<String> sourceUrlList; |
24 Map<String, int> sourceNameMap; | 23 Map<String, int> sourceNameMap; |
25 List<String> sourceNameList; | 24 List<String> sourceNameList; |
26 | 25 |
27 int previousTargetLine; | 26 int previousTargetLine; |
28 int previousTargetColumn; | 27 int previousTargetColumn; |
29 int previousSourceUrlIndex; | 28 int previousSourceUrlIndex; |
30 int previousSourceLine; | 29 int previousSourceLine; |
31 int previousSourceColumn; | 30 int previousSourceColumn; |
32 int previousSourceNameIndex; | 31 int previousSourceNameIndex; |
33 bool firstEntryInLine; | 32 bool firstEntryInLine; |
34 | 33 |
35 SourceMapBuilder() { | 34 SourceMapBuilder() { |
36 blocks = new List<_Block>(); | 35 entries = new List<_Entry>(); |
37 | 36 |
38 sourceUrlMap = new Map<String, int>(); | 37 sourceUrlMap = new Map<String, int>(); |
39 sourceUrlList = new List<String>(); | 38 sourceUrlList = new List<String>(); |
40 sourceNameMap = new Map<String, int>(); | 39 sourceNameMap = new Map<String, int>(); |
41 sourceNameList = new List<String>(); | 40 sourceNameList = new List<String>(); |
42 | 41 |
43 previousTargetLine = 0; | 42 previousTargetLine = 0; |
44 previousTargetColumn = 0; | 43 previousTargetColumn = 0; |
45 previousSourceUrlIndex = 0; | 44 previousSourceUrlIndex = 0; |
46 previousSourceLine = 0; | 45 previousSourceLine = 0; |
47 previousSourceColumn = 0; | 46 previousSourceColumn = 0; |
48 previousSourceNameIndex = 0; | 47 previousSourceNameIndex = 0; |
49 firstEntryInLine = true; | 48 firstEntryInLine = true; |
50 } | 49 } |
51 | 50 |
52 void addCodeBlock(List<SourceMappingEntry> sourceMappings, int offset) { | 51 void addMapping(SourceFile sourceFile, int sourceOffset, String sourceName, |
53 if (sourceMappings !== null) { | 52 int targetOffset) { |
54 blocks.add(new _Block(sourceMappings, offset)); | 53 entries.add(new _Entry(sourceFile, sourceOffset, sourceName, targetOffset)); |
55 } | |
56 } | 54 } |
57 | 55 |
58 String build(SourceFile targetFile) { | 56 String build(SourceFile targetFile) { |
59 StringBuffer buffer = new StringBuffer(); | 57 StringBuffer buffer = new StringBuffer(); |
60 buffer.add('{\n'); | 58 buffer.add('{\n'); |
61 buffer.add(' "version": 3,\n'); | 59 buffer.add(' "version": 3,\n'); |
62 buffer.add(' "mappings": "'); | 60 buffer.add(' "mappings": "'); |
63 blocks.forEach((_Block block) { | 61 entries.forEach((_Entry entry) => writeEntry(entry, targetFile, buffer)); |
64 block.sourceMapings.forEach((SourceMappingEntry entry) { | |
65 writeEntry(entry, targetFile, block.offset, buffer); | |
66 }); | |
67 }); | |
68 buffer.add('",\n'); | 62 buffer.add('",\n'); |
69 buffer.add(' "sources": '); | 63 buffer.add(' "sources": '); |
70 JSON.printOn(sourceUrlList, buffer); | 64 JSON.printOn(sourceUrlList, buffer); |
71 buffer.add(',\n'); | 65 buffer.add(',\n'); |
72 buffer.add(' "names": '); | 66 buffer.add(' "names": '); |
73 JSON.printOn(sourceNameList, buffer); | 67 JSON.printOn(sourceNameList, buffer); |
74 buffer.add('\n}\n'); | 68 buffer.add('\n}\n'); |
75 return buffer.toString(); | 69 return buffer.toString(); |
76 } | 70 } |
77 | 71 |
78 void writeEntry(SourceMappingEntry entry, | 72 void writeEntry(_Entry entry, SourceFile targetFile, StringBuffer output) { |
79 SourceFile targetFile, | |
80 int targetOffset, | |
81 StringBuffer output) { | |
82 if (entry.sourceFile === null) { | 73 if (entry.sourceFile === null) { |
83 return; | 74 return; |
84 } | 75 } |
85 int totalTargetOffset = targetOffset + entry.targetOffset; | 76 |
86 int targetLine = targetFile.getLine(totalTargetOffset); | 77 int targetLine = targetFile.getLine(entry.targetOffset); |
87 int targetColumn = targetFile.getColumn(targetLine, totalTargetOffset); | 78 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset); |
88 String sourceUrl = entry.sourceFile.filename; | 79 String sourceUrl = entry.sourceFile.filename; |
89 int sourceLine = entry.sourceFile.getLine(entry.sourceOffset); | 80 int sourceLine = entry.sourceFile.getLine(entry.sourceOffset); |
90 int sourceColumn = entry.sourceFile.getColumn(sourceLine, | 81 int sourceColumn = entry.sourceFile.getColumn(sourceLine, |
91 entry.sourceOffset); | 82 entry.sourceOffset); |
92 String sourceName = entry.sourceName; | 83 String sourceName = entry.sourceName; |
93 | 84 |
94 if (targetLine > previousTargetLine) { | 85 if (targetLine > previousTargetLine) { |
95 for (int i = previousTargetLine; i < targetLine; ++i) { | 86 for (int i = previousTargetLine; i < targetLine; ++i) { |
96 output.add(';'); | 87 output.add(';'); |
97 } | 88 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 int digit = value & VLQ_BASE_MASK; | 141 int digit = value & VLQ_BASE_MASK; |
151 value >>= VLQ_BASE_SHIFT; | 142 value >>= VLQ_BASE_SHIFT; |
152 if (value > 0) { | 143 if (value > 0) { |
153 digit |= VLQ_CONTINUATION_BIT; | 144 digit |= VLQ_CONTINUATION_BIT; |
154 } | 145 } |
155 output.add(BASE64_DIGITS[digit]); | 146 output.add(BASE64_DIGITS[digit]); |
156 } while (value > 0); | 147 } while (value > 0); |
157 } | 148 } |
158 } | 149 } |
159 | 150 |
160 class _Block { | 151 class _Entry { |
161 List<SourceMappingEntry> sourceMapings; | |
162 int offset; | |
163 _Block(this.sourceMapings, this.offset); | |
164 } | |
165 | |
166 class SourceMappingEntry { | |
167 SourceFile sourceFile; | 152 SourceFile sourceFile; |
168 int sourceOffset; | 153 int sourceOffset; |
| 154 String sourceName; |
169 int targetOffset; | 155 int targetOffset; |
170 String sourceName; | 156 _Entry(this.sourceFile, this.sourceOffset, this.sourceName, |
171 | 157 this.targetOffset); |
172 SourceMappingEntry(this.sourceFile, | |
173 this.sourceOffset, | |
174 this.targetOffset, | |
175 [this.sourceName]); | |
176 } | 158 } |
OLD | NEW |