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, this.sourceOffset, this.targetOffset, [thi s.sourceName]); | |
|
ahe
2012/06/19 17:00:41
Lines should be limited to 80 columns.
podivilov
2012/06/20 09:37:14
Done.
| |
| 18 } | |
| 19 | |
| 20 class SourceMapBuilder { | |
| 21 static final int VLQ_BASE_SHIFT = 5; | |
| 22 static final int VLQ_BASE_MASK = (1 << 5) - 1; | |
| 23 static final int VLQ_CONTINUATION_BIT = 1 << 5; | |
| 24 static final int VLQ_CONTINUATION_MASK = 1 << 5; | |
| 25 static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno pqrstuvwxyz0123456789+/'; | |
| 26 | |
| 27 Map<String, int> sourceURLMap; | |
| 28 List<String> sourceURLList; | |
| 29 Map<String, int> sourceNameMap; | |
| 30 List<String> sourceNameList; | |
| 31 | |
| 32 int previousTargetLine; | |
| 33 int previousTargetColumn; | |
| 34 int previousSourceURLIndex; | |
| 35 int previousSourceLine; | |
| 36 int previousSourceColumn; | |
| 37 int previousSourceNameIndex; | |
| 38 bool firstEntryInLine; | |
| 39 | |
| 40 SourceMapBuilder() { | |
| 41 sourceURLMap = new Map<String, int>(); | |
| 42 sourceURLList = new List<String>(); | |
| 43 sourceNameMap = new Map<String, int>(); | |
| 44 sourceNameList = new List<String>(); | |
| 45 | |
| 46 previousTargetLine = 0; | |
| 47 previousTargetColumn = 0; | |
| 48 previousSourceURLIndex = 0; | |
| 49 previousSourceLine = 0; | |
| 50 previousSourceColumn = 0; | |
| 51 previousSourceNameIndex = 0; | |
| 52 firstEntryInLine = true; | |
| 53 } | |
| 54 | |
| 55 String build(List<SourceMappingEntry> mappingEntries, SourceFile targetFile) { | |
| 56 StringBuffer mappingsBuffer = new StringBuffer(); | |
| 57 mappingEntries.forEach((SourceMappingEntry entry) { | |
| 58 writeEntry(entry, targetFile, mappingsBuffer); | |
| 59 }); | |
| 60 | |
| 61 Map<String, Object> sourceMap = new Map<String, Object>(); | |
| 62 sourceMap['version'] = 3; | |
| 63 sourceMap['mappings'] = mappingsBuffer.toString(); | |
| 64 sourceMap['sources'] = sourceURLList; | |
| 65 sourceMap['names'] = sourceNameList; | |
| 66 return JSON.stringify(sourceMap); | |
|
ahe
2012/06/19 17:00:41
Seriously?
| |
| 67 } | |
| 68 | |
| 69 void writeEntry(SourceMappingEntry entry, SourceFile targetFile, StringBuffer output) { | |
| 70 int targetLine = targetFile.getLine(entry.targetOffset); | |
| 71 int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset); | |
| 72 String sourceURL = entry.sourceFile.filename; | |
| 73 int sourceLine = entry.sourceFile.getLine(entry.sourceOffset); | |
| 74 int sourceColumn = entry.sourceFile.getColumn(sourceLine, entry.sourceOffset ); | |
| 75 String sourceName = entry.sourceName; | |
| 76 | |
| 77 if (targetLine > previousTargetLine) { | |
| 78 for (int i = previousTargetLine; i < targetLine; ++i) { | |
| 79 output.add(';'); | |
| 80 } | |
| 81 previousTargetLine = targetLine; | |
| 82 previousTargetColumn = 0; | |
| 83 firstEntryInLine = true; | |
| 84 } | |
| 85 | |
| 86 if (!firstEntryInLine) { | |
| 87 output.add(','); | |
| 88 } | |
| 89 firstEntryInLine = false; | |
| 90 | |
| 91 encodeVLQ(output, targetColumn - previousTargetColumn); | |
| 92 previousTargetColumn = targetColumn; | |
| 93 | |
| 94 if (sourceURL === null) | |
| 95 return; | |
| 96 | |
| 97 int sourceURLIndex = indexOf(sourceURLList, sourceURL, sourceURLMap); | |
| 98 encodeVLQ(output, sourceURLIndex - previousSourceURLIndex); | |
| 99 previousSourceURLIndex = sourceURLIndex; | |
| 100 | |
| 101 encodeVLQ(output, sourceLine - previousSourceLine); | |
| 102 previousSourceLine = sourceLine; | |
| 103 encodeVLQ(output, sourceColumn - previousSourceColumn); | |
| 104 previousSourceColumn = sourceColumn; | |
| 105 | |
| 106 if (sourceName === null) | |
| 107 return; | |
| 108 | |
| 109 int sourceNameIndex = indexOf(sourceNameList, sourceName, sourceNameMap); | |
| 110 encodeVLQ(output, sourceNameIndex - previousSourceNameIndex); | |
| 111 previousSourceNameIndex = sourceNameIndex; | |
| 112 } | |
| 113 | |
| 114 int indexOf(List<String> list, String value, Map<String, int> map) { | |
| 115 return map.putIfAbsent(value, () { | |
| 116 int index = list.length; | |
| 117 map[value] = index; | |
| 118 list.add(value); | |
| 119 return index; | |
| 120 }); | |
| 121 } | |
| 122 | |
| 123 static void encodeVLQ(StringBuffer output, int value) { | |
| 124 int signBit = 0; | |
| 125 if (value < 0) { | |
| 126 signBit = 1; | |
| 127 value = -value; | |
| 128 } | |
| 129 value = (value << 1) | signBit; | |
| 130 do { | |
| 131 int digit = value & VLQ_BASE_MASK; | |
| 132 value >>= VLQ_BASE_SHIFT; | |
| 133 if (value > 0) { | |
| 134 digit |= VLQ_CONTINUATION_BIT; | |
| 135 } | |
| 136 output.add(BASE64_DIGITS[digit]); | |
| 137 } while (value > 0); | |
| 138 } | |
| 139 } | |
| OLD | NEW |