| 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 /** | 5 /** |
| 6 * A function element that represents a closure call. The signature is copied | 6 * A function element that represents a closure call. The signature is copied |
| 7 * from the given element. | 7 * from the given element. |
| 8 */ | 8 */ |
| 9 class ClosureInvocationElement extends FunctionElement { | 9 class ClosureInvocationElement extends FunctionElement { |
| 10 ClosureInvocationElement(SourceString name, | 10 ClosureInvocationElement(SourceString name, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 final Namer namer; | 27 final Namer namer; |
| 28 NativeEmitter nativeEmitter; | 28 NativeEmitter nativeEmitter; |
| 29 StringBuffer boundClosureBuffer; | 29 StringBuffer boundClosureBuffer; |
| 30 StringBuffer mainBuffer; | 30 StringBuffer mainBuffer; |
| 31 /** Shorter access to [isolatePropertiesName]. Both here in the code, as | 31 /** Shorter access to [isolatePropertiesName]. Both here in the code, as |
| 32 well as in the generated code. */ | 32 well as in the generated code. */ |
| 33 String isolateProperties; | 33 String isolateProperties; |
| 34 String classesCollector; | 34 String classesCollector; |
| 35 final Map<int, String> boundClosureCache; | 35 final Map<int, String> boundClosureCache; |
| 36 | 36 |
| 37 CodeEmitterTask(Compiler compiler) | 37 final bool generateSourceMap; |
| 38 final List<SourceMappingEntry> sourceMappings; |
| 39 |
| 40 CodeEmitterTask(Compiler compiler, [bool generateSourceMap = false]) |
| 38 : namer = compiler.namer, | 41 : namer = compiler.namer, |
| 39 boundClosureBuffer = new StringBuffer(), | 42 boundClosureBuffer = new StringBuffer(), |
| 40 mainBuffer = new StringBuffer(), | 43 mainBuffer = new StringBuffer(), |
| 41 boundClosureCache = new Map<int, String>(), | 44 boundClosureCache = new Map<int, String>(), |
| 45 generateSourceMap = generateSourceMap, |
| 46 sourceMappings = new List<SourceMappingEntry>(), |
| 42 super(compiler) { | 47 super(compiler) { |
| 43 nativeEmitter = new NativeEmitter(this); | 48 nativeEmitter = new NativeEmitter(this); |
| 44 } | 49 } |
| 45 | 50 |
| 46 String get name() => 'CodeEmitter'; | 51 String get name() => 'CodeEmitter'; |
| 47 | 52 |
| 48 String get defineClassName() | 53 String get defineClassName() |
| 49 => '${namer.ISOLATE}.\$defineClass'; | 54 => '${namer.ISOLATE}.\$defineClass'; |
| 50 String get finishClassesName() | 55 String get finishClassesName() |
| 51 => '${namer.ISOLATE}.\$finishClasses'; | 56 => '${namer.ISOLATE}.\$finishClasses'; |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 buffer.add("$classesCollector = {};\n"); | 614 buffer.add("$classesCollector = {};\n"); |
| 610 } | 615 } |
| 611 } | 616 } |
| 612 | 617 |
| 613 void emitStaticFunctionsWithNamer(StringBuffer buffer, | 618 void emitStaticFunctionsWithNamer(StringBuffer buffer, |
| 614 Map<Element, String> generatedCode, | 619 Map<Element, String> generatedCode, |
| 615 String functionNamer(Element element)) { | 620 String functionNamer(Element element)) { |
| 616 generatedCode.forEach((Element element, String codeBlock) { | 621 generatedCode.forEach((Element element, String codeBlock) { |
| 617 if (!element.isInstanceMember()) { | 622 if (!element.isInstanceMember()) { |
| 618 String functionName = functionNamer(element); | 623 String functionName = functionNamer(element); |
| 619 buffer.add('$isolateProperties.$functionName = $codeBlock;\n\n'); | 624 buffer.add('$isolateProperties.$functionName = '); |
| 625 int beginPosition = buffer.length; |
| 626 buffer.add(codeBlock); |
| 627 int endPosition = buffer.length; |
| 628 buffer.add(';\n\n'); |
| 629 if (generateSourceMap) { |
| 630 addSourceMapping(element, beginPosition, endPosition); |
| 631 } |
| 620 } | 632 } |
| 621 }); | 633 }); |
| 622 } | 634 } |
| 623 | 635 |
| 624 void emitStaticFunctions(StringBuffer buffer) { | 636 void emitStaticFunctions(StringBuffer buffer) { |
| 625 emitStaticFunctionsWithNamer(buffer, | 637 emitStaticFunctionsWithNamer(buffer, |
| 626 compiler.codegenWorld.generatedCode, | 638 compiler.codegenWorld.generatedCode, |
| 627 namer.getName); | 639 namer.getName); |
| 628 emitStaticFunctionsWithNamer(buffer, | 640 emitStaticFunctionsWithNamer(buffer, |
| 629 compiler.codegenWorld.generatedBailoutCode, | 641 compiler.codegenWorld.generatedBailoutCode, |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 'var ${namer.CURRENT_ISOLATE} = new ${namer.ISOLATE}();\n'); | 1037 'var ${namer.CURRENT_ISOLATE} = new ${namer.ISOLATE}();\n'); |
| 1026 | 1038 |
| 1027 nativeEmitter.assembleCode(mainBuffer); | 1039 nativeEmitter.assembleCode(mainBuffer); |
| 1028 emitMain(mainBuffer); | 1040 emitMain(mainBuffer); |
| 1029 mainBuffer.add('function init() {\n'); | 1041 mainBuffer.add('function init() {\n'); |
| 1030 mainBuffer.add(' $isolateProperties = {};\n'); | 1042 mainBuffer.add(' $isolateProperties = {};\n'); |
| 1031 addDefineClassAndFinishClassFunctionsIfNecessary(mainBuffer); | 1043 addDefineClassAndFinishClassFunctionsIfNecessary(mainBuffer); |
| 1032 emitFinishIsolateConstructor(mainBuffer); | 1044 emitFinishIsolateConstructor(mainBuffer); |
| 1033 mainBuffer.add('}\n'); | 1045 mainBuffer.add('}\n'); |
| 1034 compiler.assembledCode = mainBuffer.toString(); | 1046 compiler.assembledCode = mainBuffer.toString(); |
| 1047 |
| 1048 if (generateSourceMap) { |
| 1049 SourceFile compiledFile = new SourceFile(null, compiler.assembledCode); |
| 1050 String sourceMap = new SourceMapBuilder().build(sourceMappings, |
| 1051 compiledFile); |
| 1052 // TODO(podivilov): We should find a better way to return source maps to |
| 1053 // compiler. Using diagnostic handler for that purpose is a temporary |
| 1054 // hack. |
| 1055 compiler.reportDiagnostic( |
| 1056 null, sourceMap, new api.Diagnostic(-1, 'source map')); |
| 1057 } |
| 1035 }); | 1058 }); |
| 1036 return compiler.assembledCode; | 1059 return compiler.assembledCode; |
| 1037 } | 1060 } |
| 1061 |
| 1062 void addSourceMapping(FunctionElement element, |
| 1063 int beginPosition, |
| 1064 int endPosition) { |
| 1065 SourceFile sourceFile = element.getCompilationUnit().script.file; |
| 1066 FunctionExpression expression = element.cachedNode; |
| 1067 sourceMappings.add(new SourceMappingEntry( |
| 1068 sourceFile, expression.getBeginToken().charOffset, beginPosition)); |
| 1069 sourceMappings.add(new SourceMappingEntry( |
| 1070 sourceFile, expression.getEndToken().charOffset, endPosition)); |
| 1071 } |
| 1038 } | 1072 } |
| OLD | NEW |