Revert "First shot at source maps generation in dart2js."

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@8970 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
podivilov@google.com 2012-06-21 10:47:00 +00:00
parent 85a0c9635c
commit a15a5c72a4
5 changed files with 6 additions and 201 deletions

View file

@ -63,8 +63,8 @@ class JavaScriptBackend extends Backend {
return <CompilerTask>[builder, optimizer, generator, emitter];
}
JavaScriptBackend(Compiler compiler, bool generateSourceMap)
: emitter = new CodeEmitterTask(compiler, generateSourceMap),
JavaScriptBackend(Compiler compiler)
: emitter = new CodeEmitterTask(compiler),
fieldInitializers = new Map<Element, Map<Element, HType>>(),
fieldIntegerSetters = new Map<Element, Map<Element, bool>>(),
super(compiler) {
@ -260,8 +260,7 @@ class Compiler implements DiagnosticListener {
this.enableTypeAssertions = false,
this.enableUserAssertions = false,
bool emitJavascript = true,
validateUnparse = false,
generateSourceMap = true])
validateUnparse = false])
: libraries = new Map<String, LibraryElement>(),
world = new World(),
progress = new Stopwatch.start() {
@ -275,8 +274,7 @@ class Compiler implements DiagnosticListener {
resolver = new ResolverTask(this);
checker = new TypeCheckerTask(this);
backend = emitJavascript ?
new JavaScriptBackend(this, generateSourceMap) :
new dart_backend.DartBackend(this);
new JavaScriptBackend(this) : new dart_backend.DartBackend(this);
enqueuer = new EnqueueTask(this);
tasks = [scanner, dietParser, parser, resolver, checker,
unparseValidator, constantHandler, enqueuer];

View file

@ -71,7 +71,6 @@ void compile(List<String> argv) {
bool verbose = false;
Uri libraryRoot = cwd;
Uri out = cwd.resolve('out.js');
Uri sourceMapOut = cwd.resolve('out.js.map');
Uri packageRoot = null;
List<String> options = new List<String>();
bool explicitOut = false;
@ -91,7 +90,6 @@ void compile(List<String> argv) {
setOutput(String argument) {
explicitOut = true;
out = cwd.resolve(nativeToUriPath(extractParameter(argument)));
sourceMapOut = new Uri.fromString('$out.map');
}
handleShortOptions(String argument) {
@ -183,13 +181,6 @@ void compile(List<String> argv) {
void handler(Uri uri, int begin, int end, String message,
api.Diagnostic kind) {
if (kind.name === 'source map') {
// TODO(podivilov): We should find a better way to return source maps from
// emitter. Using diagnostic handler for that purpose is a temporary hack.
writeString(sourceMapOut, message);
return;
}
if (isAborting) return;
isAborting = kind === api.Diagnostic.CRASH;
bool fatal = (kind.ordinal & FATAL) != 0;
@ -242,7 +233,6 @@ void compile(List<String> argv) {
if (code === null) {
fail('Error: Compilation failed.');
}
code = '$code\n//@ sourceMappingURL=${relativize(out, sourceMapOut)}';
writeString(out, code);
int jsBytesWritten = code.length;
info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS '

View file

@ -34,16 +34,11 @@ class CodeEmitterTask extends CompilerTask {
String classesCollector;
final Map<int, String> boundClosureCache;
final bool generateSourceMap;
final List<SourceMappingEntry> sourceMappings;
CodeEmitterTask(Compiler compiler, [bool generateSourceMap = false])
CodeEmitterTask(Compiler compiler)
: namer = compiler.namer,
boundClosureBuffer = new StringBuffer(),
mainBuffer = new StringBuffer(),
boundClosureCache = new Map<int, String>(),
generateSourceMap = generateSourceMap,
sourceMappings = new List<SourceMappingEntry>(),
super(compiler) {
nativeEmitter = new NativeEmitter(this);
}
@ -621,14 +616,7 @@ function(collectedClasses) {
generatedCode.forEach((Element element, String codeBlock) {
if (!element.isInstanceMember()) {
String functionName = functionNamer(element);
buffer.add('$isolateProperties.$functionName = ');
int beginPosition = buffer.length;
buffer.add(codeBlock);
int endPosition = buffer.length;
buffer.add(';\n\n');
if (generateSourceMap) {
addSourceMapping(element, beginPosition, endPosition);
}
buffer.add('$isolateProperties.$functionName = $codeBlock;\n\n');
}
});
}
@ -1044,29 +1032,7 @@ if (typeof window != 'undefined' && typeof document != 'undefined' &&
emitFinishIsolateConstructor(mainBuffer);
mainBuffer.add('}\n');
compiler.assembledCode = mainBuffer.toString();
if (generateSourceMap) {
SourceFile compiledFile = new SourceFile(null, compiler.assembledCode);
String sourceMap = new SourceMapBuilder().build(sourceMappings,
compiledFile);
// TODO(podivilov): We should find a better way to return source maps to
// compiler. Using diagnostic handler for that purpose is a temporary
// hack.
compiler.reportDiagnostic(
null, sourceMap, new api.Diagnostic(-1, 'source map'));
}
});
return compiler.assembledCode;
}
void addSourceMapping(FunctionElement element,
int beginPosition,
int endPosition) {
SourceFile sourceFile = element.getCompilationUnit().script.file;
FunctionExpression expression = element.cachedNode;
sourceMappings.add(new SourceMappingEntry(
sourceFile, expression.getBeginToken().charOffset, beginPosition));
sourceMappings.add(new SourceMappingEntry(
sourceFile, expression.getEndToken().charOffset, endPosition));
}
}

View file

@ -14,7 +14,6 @@
#import('ssa/ssa.dart');
#import('string_validator.dart');
#import('source_file.dart');
#import('source_map_builder.dart');
#import('tree/tree.dart');
#import('util/characters.dart');
#import('util/util.dart');

View file

@ -1,148 +0,0 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#library('source_map_builder');
#import('dart:json');
#import('source_file.dart');
class SourceMappingEntry {
SourceFile sourceFile;
int sourceOffset;
int targetOffset;
String sourceName;
SourceMappingEntry(this.sourceFile,
this.sourceOffset,
this.targetOffset,
[this.sourceName]);
}
class SourceMapBuilder {
static final int VLQ_BASE_SHIFT = 5;
static final int VLQ_BASE_MASK = (1 << 5) - 1;
static final int VLQ_CONTINUATION_BIT = 1 << 5;
static final int VLQ_CONTINUATION_MASK = 1 << 5;
static final String BASE64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn'
'opqrstuvwxyz0123456789+/';
Map<String, int> sourceUrlMap;
List<String> sourceUrlList;
Map<String, int> sourceNameMap;
List<String> sourceNameList;
int previousTargetLine;
int previousTargetColumn;
int previousSourceUrlIndex;
int previousSourceLine;
int previousSourceColumn;
int previousSourceNameIndex;
bool firstEntryInLine;
SourceMapBuilder() {
sourceUrlMap = new Map<String, int>();
sourceUrlList = new List<String>();
sourceNameMap = new Map<String, int>();
sourceNameList = new List<String>();
previousTargetLine = 0;
previousTargetColumn = 0;
previousSourceUrlIndex = 0;
previousSourceLine = 0;
previousSourceColumn = 0;
previousSourceNameIndex = 0;
firstEntryInLine = true;
}
String build(List<SourceMappingEntry> mappingEntries, SourceFile targetFile) {
StringBuffer buffer = new StringBuffer();
buffer.add('{\n');
buffer.add(' "version": 3,\n');
buffer.add(' "mappings": "');
mappingEntries.forEach((SourceMappingEntry entry) {
writeEntry(entry, targetFile, buffer);
});
buffer.add('",\n');
// TODO(podivilov): serialize lists directly to buffer.
buffer.add(' "sources": ${JSON.stringify(sourceUrlList)},\n');
buffer.add(' "names": ${JSON.stringify(sourceNameList)}\n');
buffer.add('}\n');
return buffer.toString();
}
void writeEntry(SourceMappingEntry entry,
SourceFile targetFile,
StringBuffer output) {
int targetLine = targetFile.getLine(entry.targetOffset);
int targetColumn = targetFile.getColumn(targetLine, entry.targetOffset);
String sourceUrl = entry.sourceFile.filename;
int sourceLine = entry.sourceFile.getLine(entry.sourceOffset);
int sourceColumn = entry.sourceFile.getColumn(sourceLine,
entry.sourceOffset);
String sourceName = entry.sourceName;
if (targetLine > previousTargetLine) {
for (int i = previousTargetLine; i < targetLine; ++i) {
output.add(';');
}
previousTargetLine = targetLine;
previousTargetColumn = 0;
firstEntryInLine = true;
}
if (!firstEntryInLine) {
output.add(',');
}
firstEntryInLine = false;
encodeVLQ(output, targetColumn - previousTargetColumn);
previousTargetColumn = targetColumn;
if (sourceUrl === null)
return;
int sourceUrlIndex = indexOf(sourceUrlList, sourceUrl, sourceUrlMap);
encodeVLQ(output, sourceUrlIndex - previousSourceUrlIndex);
previousSourceUrlIndex = sourceUrlIndex;
encodeVLQ(output, sourceLine - previousSourceLine);
previousSourceLine = sourceLine;
encodeVLQ(output, sourceColumn - previousSourceColumn);
previousSourceColumn = sourceColumn;
if (sourceName === null)
return;
int sourceNameIndex = indexOf(sourceNameList, sourceName, sourceNameMap);
encodeVLQ(output, sourceNameIndex - previousSourceNameIndex);
previousSourceNameIndex = sourceNameIndex;
}
int indexOf(List<String> list, String value, Map<String, int> map) {
return map.putIfAbsent(value, () {
int index = list.length;
map[value] = index;
list.add(value);
return index;
});
}
static void encodeVLQ(StringBuffer output, int value) {
int signBit = 0;
if (value < 0) {
signBit = 1;
value = -value;
}
value = (value << 1) | signBit;
do {
int digit = value & VLQ_BASE_MASK;
value >>= VLQ_BASE_SHIFT;
if (value > 0) {
digit |= VLQ_CONTINUATION_BIT;
}
output.add(BASE64_DIGITS[digit]);
} while (value > 0);
}
}