| 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 17 matching lines...) Expand all Loading... |
| 28 NativeEmitter nativeEmitter; | 28 NativeEmitter nativeEmitter; |
| 29 CodeBuffer boundClosureBuffer; | 29 CodeBuffer boundClosureBuffer; |
| 30 CodeBuffer mainBuffer; | 30 CodeBuffer 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 final bool generateSourceMap; | 37 final bool generateSourceMap; |
| 38 final SourceMapBuilder sourceMapBuilder; | |
| 39 | 38 |
| 40 CodeEmitterTask(Compiler compiler, [bool generateSourceMap = false]) | 39 CodeEmitterTask(Compiler compiler, [bool generateSourceMap = false]) |
| 41 : namer = compiler.namer, | 40 : namer = compiler.namer, |
| 42 boundClosureBuffer = new CodeBuffer(), | 41 boundClosureBuffer = new CodeBuffer(), |
| 43 mainBuffer = new CodeBuffer(), | 42 mainBuffer = new CodeBuffer(), |
| 44 boundClosureCache = new Map<int, String>(), | 43 boundClosureCache = new Map<int, String>(), |
| 45 generateSourceMap = generateSourceMap, | 44 generateSourceMap = generateSourceMap, |
| 46 sourceMapBuilder = new SourceMapBuilder(), | |
| 47 super(compiler) { | 45 super(compiler) { |
| 48 nativeEmitter = new NativeEmitter(this); | 46 nativeEmitter = new NativeEmitter(this); |
| 49 } | 47 } |
| 50 | 48 |
| 51 String get name => 'CodeEmitter'; | 49 String get name => 'CodeEmitter'; |
| 52 | 50 |
| 53 String get defineClassName | 51 String get defineClassName |
| 54 => '${namer.ISOLATE}.\$defineClass'; | 52 => '${namer.ISOLATE}.\$defineClass'; |
| 55 String get finishClassesName | 53 String get finishClassesName |
| 56 => '${namer.ISOLATE}.\$finishClasses'; | 54 => '${namer.ISOLATE}.\$finishClasses'; |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 | 525 |
| 528 void emitInstanceMembers(ClassElement classElement, | 526 void emitInstanceMembers(ClassElement classElement, |
| 529 CodeBuffer buffer, | 527 CodeBuffer buffer, |
| 530 bool needsLeadingComma) { | 528 bool needsLeadingComma) { |
| 531 bool needsComma = needsLeadingComma; | 529 bool needsComma = needsLeadingComma; |
| 532 void defineInstanceMember(String name, CodeBuffer memberBuffer) { | 530 void defineInstanceMember(String name, CodeBuffer memberBuffer) { |
| 533 if (needsComma) buffer.add(','); | 531 if (needsComma) buffer.add(','); |
| 534 needsComma = true; | 532 needsComma = true; |
| 535 buffer.add('\n'); | 533 buffer.add('\n'); |
| 536 buffer.add(' $name: '); | 534 buffer.add(' $name: '); |
| 537 addMappings(memberBuffer, buffer.length); | |
| 538 buffer.add(memberBuffer); | 535 buffer.add(memberBuffer); |
| 539 } | 536 } |
| 540 | 537 |
| 541 classElement.forEachMember(includeBackendMembers: true, | 538 classElement.forEachMember(includeBackendMembers: true, |
| 542 f: (ClassElement enclosing, Element member) { | 539 f: (ClassElement enclosing, Element member) { |
| 543 if (member.isInstanceMember()) { | 540 if (member.isInstanceMember()) { |
| 544 addInstanceMember(member, defineInstanceMember); | 541 addInstanceMember(member, defineInstanceMember); |
| 545 } | 542 } |
| 546 }); | 543 }); |
| 547 | 544 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 } | 682 } |
| 686 } | 683 } |
| 687 | 684 |
| 688 void emitStaticFunctionsWithNamer(CodeBuffer buffer, | 685 void emitStaticFunctionsWithNamer(CodeBuffer buffer, |
| 689 Map<Element, CodeBuffer> generatedCode, | 686 Map<Element, CodeBuffer> generatedCode, |
| 690 String functionNamer(Element element)) { | 687 String functionNamer(Element element)) { |
| 691 generatedCode.forEach((Element element, CodeBuffer functionBuffer) { | 688 generatedCode.forEach((Element element, CodeBuffer functionBuffer) { |
| 692 if (!element.isInstanceMember()) { | 689 if (!element.isInstanceMember()) { |
| 693 String functionName = functionNamer(element); | 690 String functionName = functionNamer(element); |
| 694 buffer.add('$isolateProperties.$functionName = '); | 691 buffer.add('$isolateProperties.$functionName = '); |
| 695 addMappings(functionBuffer, buffer.length); | |
| 696 buffer.add(functionBuffer); | 692 buffer.add(functionBuffer); |
| 697 buffer.add(';\n\n'); | 693 buffer.add(';\n\n'); |
| 698 } | 694 } |
| 699 }); | 695 }); |
| 700 } | 696 } |
| 701 | 697 |
| 702 void emitStaticFunctions(CodeBuffer buffer) { | 698 void emitStaticFunctions(CodeBuffer buffer) { |
| 703 emitStaticFunctionsWithNamer(buffer, | 699 emitStaticFunctionsWithNamer(buffer, |
| 704 compiler.codegenWorld.generatedCode, | 700 compiler.codegenWorld.generatedCode, |
| 705 namer.getName); | 701 namer.getName); |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1173 emitMain(mainBuffer); | 1169 emitMain(mainBuffer); |
| 1174 mainBuffer.add('function init() {\n'); | 1170 mainBuffer.add('function init() {\n'); |
| 1175 mainBuffer.add('$isolateProperties = {};\n'); | 1171 mainBuffer.add('$isolateProperties = {};\n'); |
| 1176 addDefineClassAndFinishClassFunctionsIfNecessary(mainBuffer); | 1172 addDefineClassAndFinishClassFunctionsIfNecessary(mainBuffer); |
| 1177 emitFinishIsolateConstructor(mainBuffer); | 1173 emitFinishIsolateConstructor(mainBuffer); |
| 1178 mainBuffer.add('}\n'); | 1174 mainBuffer.add('}\n'); |
| 1179 compiler.assembledCode = mainBuffer.toString(); | 1175 compiler.assembledCode = mainBuffer.toString(); |
| 1180 | 1176 |
| 1181 if (generateSourceMap) { | 1177 if (generateSourceMap) { |
| 1182 SourceFile compiledFile = new SourceFile(null, compiler.assembledCode); | 1178 SourceFile compiledFile = new SourceFile(null, compiler.assembledCode); |
| 1183 String sourceMap = sourceMapBuilder.build(compiledFile); | 1179 String sourceMap = buildSourceMap(mainBuffer, compiledFile); |
| 1184 // TODO(podivilov): We should find a better way to return source maps to | 1180 // TODO(podivilov): We should find a better way to return source maps to |
| 1185 // compiler. Using diagnostic handler for that purpose is a temporary | 1181 // compiler. Using diagnostic handler for that purpose is a temporary |
| 1186 // hack. | 1182 // hack. |
| 1187 compiler.reportDiagnostic( | 1183 compiler.reportDiagnostic( |
| 1188 null, sourceMap, new api.Diagnostic(-1, 'source map')); | 1184 null, sourceMap, new api.Diagnostic(-1, 'source map')); |
| 1189 } | 1185 } |
| 1190 }); | 1186 }); |
| 1191 return compiler.assembledCode; | 1187 return compiler.assembledCode; |
| 1192 } | 1188 } |
| 1193 | 1189 |
| 1194 void addMappings(CodeBuffer buffer, int bufferOffset) { | 1190 String buildSourceMap(CodeBuffer buffer, SourceFile compiledFile) { |
| 1191 SourceMapBuilder sourceMapBuilder = new SourceMapBuilder(); |
| 1195 buffer.forEachSourceLocation((Element element, Token token, int offset) { | 1192 buffer.forEachSourceLocation((Element element, Token token, int offset) { |
| 1193 if (element == null) { |
| 1194 sourceMapBuilder.addMapping(null, null, null, offset); |
| 1195 return; |
| 1196 } |
| 1196 SourceFile sourceFile = element.getCompilationUnit().script.file; | 1197 SourceFile sourceFile = element.getCompilationUnit().script.file; |
| 1197 String sourceName = null; | 1198 String sourceName = null; |
| 1198 if (token.kind === IDENTIFIER_TOKEN) { | 1199 if (token.kind === IDENTIFIER_TOKEN) { |
| 1199 sourceName = token.slowToString(); | 1200 sourceName = token.slowToString(); |
| 1200 } | 1201 } |
| 1201 int totalOffset = bufferOffset + offset; | |
| 1202 sourceMapBuilder.addMapping( | 1202 sourceMapBuilder.addMapping( |
| 1203 sourceFile, token.charOffset, sourceName, totalOffset); | 1203 sourceFile, token.charOffset, sourceName, offset); |
| 1204 }); | 1204 }); |
| 1205 return sourceMapBuilder.build(compiledFile); |
| 1205 } | 1206 } |
| 1206 } | 1207 } |
| 1207 | 1208 |
| 1208 typedef void DefineMemberFunction(String invocationName, CodeBuffer definition); | 1209 typedef void DefineMemberFunction(String invocationName, CodeBuffer definition); |
| OLD | NEW |