Chromium Code Reviews| OLD | NEW |
|---|---|
| (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; | |
|
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.
| |
| 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 buffer.add(' "sources": ${JSON.stringify(sourceURLList)},\n'); | |
| 69 buffer.add(' "names": ${JSON.stringify(sourceNameList)}\n'); | |
| 70 buffer.add('}\n'); | |
| 71 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
| |
| 72 } | |
| 73 | |
| 74 void writeEntry(SourceMappingEntry entry, | |
| 75 SourceFile targetFile, | |
| 76 StringBuffer output) { | |
| 77 int targetLine = targetFile.getLine(entry.targetOffset); | |
| 78 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset); | |
| 79 String sourceURL = entry.sourceFile.filename; | |
| 80 int sourceLine = entry.sourceFile.getLine(entry.sourceOffset); | |
| 81 int sourceColumn = entry.sourceFile.getColumn(sourceLine, | |
| 82 entry.sourceOffset); | |
| 83 String sourceName = entry.sourceName; | |
| 84 | |
| 85 if (targetLine > previousTargetLine) { | |
| 86 for (int i = previousTargetLine; i < targetLine; ++i) { | |
| 87 output.add(';'); | |
| 88 } | |
| 89 previousTargetLine = targetLine; | |
| 90 previousTargetColumn = 0; | |
| 91 firstEntryInLine = true; | |
| 92 } | |
| 93 | |
| 94 if (!firstEntryInLine) { | |
| 95 output.add(','); | |
| 96 } | |
| 97 firstEntryInLine = false; | |
| 98 | |
| 99 encodeVLQ(output, targetColumn - previousTargetColumn); | |
| 100 previousTargetColumn = targetColumn; | |
| 101 | |
| 102 if (sourceURL === null) | |
| 103 return; | |
| 104 | |
| 105 int sourceURLIndex = indexOf(sourceURLList, sourceURL, sourceURLMap); | |
| 106 encodeVLQ(output, sourceURLIndex - previousSourceURLIndex); | |
| 107 previousSourceURLIndex = sourceURLIndex; | |
| 108 | |
| 109 encodeVLQ(output, sourceLine - previousSourceLine); | |
| 110 previousSourceLine = sourceLine; | |
| 111 encodeVLQ(output, sourceColumn - previousSourceColumn); | |
| 112 previousSourceColumn = sourceColumn; | |
| 113 | |
| 114 if (sourceName === null) | |
| 115 return; | |
| 116 | |
| 117 int sourceNameIndex = indexOf(sourceNameList, sourceName, sourceNameMap); | |
| 118 encodeVLQ(output, sourceNameIndex - previousSourceNameIndex); | |
| 119 previousSourceNameIndex = sourceNameIndex; | |
| 120 } | |
| 121 | |
| 122 int indexOf(List<String> list, String value, Map<String, int> map) { | |
| 123 return map.putIfAbsent(value, () { | |
| 124 int index = list.length; | |
| 125 map[value] = index; | |
| 126 list.add(value); | |
| 127 return index; | |
| 128 }); | |
| 129 } | |
| 130 | |
| 131 static void encodeVLQ(StringBuffer output, int value) { | |
| 132 int signBit = 0; | |
| 133 if (value < 0) { | |
| 134 signBit = 1; | |
| 135 value = -value; | |
| 136 } | |
| 137 value = (value << 1) | signBit; | |
| 138 do { | |
| 139 int digit = value & VLQ_BASE_MASK; | |
| 140 value >>= VLQ_BASE_SHIFT; | |
| 141 if (value > 0) { | |
| 142 digit |= VLQ_CONTINUATION_BIT; | |
| 143 } | |
| 144 output.add(BASE64_DIGITS[digit]); | |
| 145 } while (value > 0); | |
| 146 } | |
| 147 } | |
| OLD | NEW |