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