| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart2js.js_emitter.full_emitter; | 5 library dart2js.js_emitter.full_emitter; |
| 6 | 6 |
| 7 import 'dart:collection' show HashMap; | 7 import 'dart:collection' show HashMap; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; | 10 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 TypeVariableCodegenAnalysis; | 46 TypeVariableCodegenAnalysis; |
| 47 import '../../js_backend/native_data.dart'; | 47 import '../../js_backend/native_data.dart'; |
| 48 import '../../universe/call_structure.dart' show CallStructure; | 48 import '../../universe/call_structure.dart' show CallStructure; |
| 49 import '../../universe/selector.dart' show Selector; | 49 import '../../universe/selector.dart' show Selector; |
| 50 import '../../universe/world_builder.dart' show CodegenWorldBuilder; | 50 import '../../universe/world_builder.dart' show CodegenWorldBuilder; |
| 51 import '../../util/uri_extras.dart' show relativize; | 51 import '../../util/uri_extras.dart' show relativize; |
| 52 import '../../world.dart' show ClosedWorld; | 52 import '../../world.dart' show ClosedWorld; |
| 53 import '../constant_ordering.dart' show deepCompareConstants; | 53 import '../constant_ordering.dart' show deepCompareConstants; |
| 54 import '../headers.dart'; | 54 import '../headers.dart'; |
| 55 import '../js_emitter.dart' hide Emitter, EmitterFactory; | 55 import '../js_emitter.dart' hide Emitter, EmitterFactory; |
| 56 import '../js_emitter.dart' as js_emitter show Emitter, EmitterFactory; | 56 import '../js_emitter.dart' as js_emitter show EmitterBase, EmitterFactory; |
| 57 import '../model.dart'; | 57 import '../model.dart'; |
| 58 import '../program_builder/program_builder.dart'; | 58 import '../program_builder/program_builder.dart'; |
| 59 import '../sorter.dart'; | 59 import '../sorter.dart'; |
| 60 | 60 |
| 61 import 'class_builder.dart'; | 61 import 'class_builder.dart'; |
| 62 import 'class_emitter.dart'; | 62 import 'class_emitter.dart'; |
| 63 import 'container_builder.dart'; | 63 import 'container_builder.dart'; |
| 64 import 'interceptor_emitter.dart'; | 64 import 'interceptor_emitter.dart'; |
| 65 import 'nsm_emitter.dart'; | 65 import 'nsm_emitter.dart'; |
| 66 | 66 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 84 bool get supportsReflection => true; | 84 bool get supportsReflection => true; |
| 85 | 85 |
| 86 @override | 86 @override |
| 87 Emitter createEmitter( | 87 Emitter createEmitter( |
| 88 CodeEmitterTask task, Namer namer, ClosedWorld closedWorld) { | 88 CodeEmitterTask task, Namer namer, ClosedWorld closedWorld) { |
| 89 return new Emitter(task.compiler, namer, closedWorld, generateSourceMap, | 89 return new Emitter(task.compiler, namer, closedWorld, generateSourceMap, |
| 90 task, task.sorter); | 90 task, task.sorter); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 class Emitter implements js_emitter.Emitter { | 94 class Emitter extends js_emitter.EmitterBase { |
| 95 final Compiler compiler; | 95 final Compiler compiler; |
| 96 final CodeEmitterTask task; | 96 final CodeEmitterTask task; |
| 97 final ClosedWorld _closedWorld; | 97 final ClosedWorld _closedWorld; |
| 98 | 98 |
| 99 // The following fields will be set to copies of the program-builder's | 99 // The following fields will be set to copies of the program-builder's |
| 100 // collector. | 100 // collector. |
| 101 Map<OutputUnit, List<FieldEntity>> outputStaticNonFinalFieldLists; | 101 Map<OutputUnit, List<FieldEntity>> outputStaticNonFinalFieldLists; |
| 102 Map<OutputUnit, Set<LibraryEntity>> outputLibraryLists; | 102 Map<OutputUnit, Set<LibraryEntity>> outputLibraryLists; |
| 103 List<TypedefElement> typedefsNeededForReflection; | 103 List<TypedefElement> typedefsNeededForReflection; |
| 104 | 104 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 @override | 292 @override |
| 293 jsAst.Expression generateEmbeddedGlobalAccess(String global) { | 293 jsAst.Expression generateEmbeddedGlobalAccess(String global) { |
| 294 return js(generateEmbeddedGlobalAccessString(global)); | 294 return js(generateEmbeddedGlobalAccessString(global)); |
| 295 } | 295 } |
| 296 | 296 |
| 297 String generateEmbeddedGlobalAccessString(String global) { | 297 String generateEmbeddedGlobalAccessString(String global) { |
| 298 // TODO(floitsch): don't use 'init' as global embedder storage. | 298 // TODO(floitsch): don't use 'init' as global embedder storage. |
| 299 return '$initName.$global'; | 299 return '$initName.$global'; |
| 300 } | 300 } |
| 301 | 301 |
| 302 jsAst.PropertyAccess globalPropertyAccess(Element element) { | |
| 303 jsAst.Name name = namer.globalPropertyName(element); | |
| 304 jsAst.PropertyAccess pa = new jsAst.PropertyAccess( | |
| 305 new jsAst.VariableUse(namer.globalObjectFor(element)), name); | |
| 306 return pa; | |
| 307 } | |
| 308 | |
| 309 @override | 302 @override |
| 310 jsAst.Expression isolateLazyInitializerAccess(FieldElement element) { | 303 jsAst.Expression isolateLazyInitializerAccess(FieldElement element) { |
| 311 return jsAst.js('#.#', | 304 return jsAst.js('#.#', [ |
| 312 [namer.globalObjectFor(element), namer.lazyInitializerName(element)]); | 305 namer.globalObjectForMember(element), |
| 306 namer.lazyInitializerName(element) |
| 307 ]); |
| 313 } | 308 } |
| 314 | 309 |
| 315 @override | 310 @override |
| 316 jsAst.Expression isolateStaticClosureAccess(MethodElement element) { | 311 jsAst.Expression isolateStaticClosureAccess(MethodElement element) { |
| 317 return jsAst.js('#.#()', | 312 return jsAst.js('#.#()', [ |
| 318 [namer.globalObjectFor(element), namer.staticClosureName(element)]); | 313 namer.globalObjectForMember(element), |
| 314 namer.staticClosureName(element) |
| 315 ]); |
| 319 } | 316 } |
| 320 | 317 |
| 321 @override | 318 @override |
| 322 jsAst.PropertyAccess staticFieldAccess(FieldElement element) { | |
| 323 return globalPropertyAccess(element); | |
| 324 } | |
| 325 | |
| 326 @override | |
| 327 jsAst.PropertyAccess staticFunctionAccess(MethodElement element) { | |
| 328 return globalPropertyAccess(element); | |
| 329 } | |
| 330 | |
| 331 @override | |
| 332 jsAst.PropertyAccess constructorAccess(ClassElement element) { | |
| 333 return globalPropertyAccess(element); | |
| 334 } | |
| 335 | |
| 336 @override | |
| 337 jsAst.PropertyAccess prototypeAccess( | 319 jsAst.PropertyAccess prototypeAccess( |
| 338 ClassElement element, bool hasBeenInstantiated) { | 320 ClassElement element, bool hasBeenInstantiated) { |
| 339 return jsAst.js('#.prototype', constructorAccess(element)); | 321 return jsAst.js('#.prototype', constructorAccess(element)); |
| 340 } | 322 } |
| 341 | 323 |
| 342 @override | 324 @override |
| 343 jsAst.PropertyAccess interceptorClassAccess(ClassElement element) { | |
| 344 return globalPropertyAccess(element); | |
| 345 } | |
| 346 | |
| 347 @override | |
| 348 jsAst.PropertyAccess typeAccess(Entity element) { | |
| 349 return globalPropertyAccess(element); | |
| 350 } | |
| 351 | |
| 352 @override | |
| 353 jsAst.Template templateForBuiltin(JsBuiltin builtin) { | 325 jsAst.Template templateForBuiltin(JsBuiltin builtin) { |
| 354 switch (builtin) { | 326 switch (builtin) { |
| 355 case JsBuiltin.dartObjectConstructor: | 327 case JsBuiltin.dartObjectConstructor: |
| 356 return jsAst.js | 328 return jsAst.js |
| 357 .expressionTemplateYielding(typeAccess(commonElements.objectClass)); | 329 .expressionTemplateYielding(typeAccess(commonElements.objectClass)); |
| 358 | 330 |
| 359 case JsBuiltin.isCheckPropertyToJsConstructorName: | 331 case JsBuiltin.isCheckPropertyToJsConstructorName: |
| 360 int isPrefixLength = namer.operatorIsPrefix.length; | 332 int isPrefixLength = namer.operatorIsPrefix.length; |
| 361 return jsAst.js.expressionTemplateFor('#.substring($isPrefixLength)'); | 333 return jsAst.js.expressionTemplateFor('#.substring($isPrefixLength)'); |
| 362 | 334 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 .properties | 574 .properties |
| 603 .addAll(builder.properties); | 575 .addAll(builder.properties); |
| 604 } | 576 } |
| 605 } | 577 } |
| 606 | 578 |
| 607 jsAst.Statement buildStaticNonFinalFieldInitializations( | 579 jsAst.Statement buildStaticNonFinalFieldInitializations( |
| 608 OutputUnit outputUnit) { | 580 OutputUnit outputUnit) { |
| 609 jsAst.Statement buildInitialization( | 581 jsAst.Statement buildInitialization( |
| 610 FieldElement element, jsAst.Expression initialValue) { | 582 FieldElement element, jsAst.Expression initialValue) { |
| 611 return js.statement('${namer.staticStateHolder}.# = #', | 583 return js.statement('${namer.staticStateHolder}.# = #', |
| 612 [namer.globalPropertyName(element), initialValue]); | 584 [namer.globalPropertyNameForMember(element), initialValue]); |
| 613 } | 585 } |
| 614 | 586 |
| 615 bool inMainUnit = (outputUnit == compiler.deferredLoadTask.mainOutputUnit); | 587 bool inMainUnit = (outputUnit == compiler.deferredLoadTask.mainOutputUnit); |
| 616 JavaScriptConstantCompiler handler = backend.constants; | 588 JavaScriptConstantCompiler handler = backend.constants; |
| 617 List<jsAst.Statement> parts = <jsAst.Statement>[]; | 589 List<jsAst.Statement> parts = <jsAst.Statement>[]; |
| 618 | 590 |
| 619 Iterable<FieldEntity> fields = outputStaticNonFinalFieldLists[outputUnit]; | 591 Iterable<FieldEntity> fields = outputStaticNonFinalFieldLists[outputUnit]; |
| 620 // If the outputUnit does not contain any static non-final fields, then | 592 // If the outputUnit does not contain any static non-final fields, then |
| 621 // [fields] is `null`. | 593 // [fields] is `null`. |
| 622 if (fields != null) { | 594 if (fields != null) { |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1071 } | 1043 } |
| 1072 | 1044 |
| 1073 compiler.dumpInfoTask.registerElementAst(library, metadata); | 1045 compiler.dumpInfoTask.registerElementAst(library, metadata); |
| 1074 compiler.dumpInfoTask.registerElementAst(library, initializer); | 1046 compiler.dumpInfoTask.registerElementAst(library, initializer); |
| 1075 | 1047 |
| 1076 List<jsAst.Expression> parts = <jsAst.Expression>[]; | 1048 List<jsAst.Expression> parts = <jsAst.Expression>[]; |
| 1077 parts | 1049 parts |
| 1078 ..add(js.string(libraryName)) | 1050 ..add(js.string(libraryName)) |
| 1079 ..add(js.string(uri.toString())) | 1051 ..add(js.string(uri.toString())) |
| 1080 ..add(metadata == null ? new jsAst.ArrayHole() : metadata) | 1052 ..add(metadata == null ? new jsAst.ArrayHole() : metadata) |
| 1081 ..add(js('#', namer.globalObjectFor(library))) | 1053 ..add(js('#', namer.globalObjectForLibrary(library))) |
| 1082 ..add(initializer); | 1054 ..add(initializer); |
| 1083 if (library == compiler.mainApp) { | 1055 if (library == compiler.mainApp) { |
| 1084 parts.add(js.number(1)); | 1056 parts.add(js.number(1)); |
| 1085 } | 1057 } |
| 1086 | 1058 |
| 1087 return new jsAst.ArrayInitializer(parts); | 1059 return new jsAst.ArrayInitializer(parts); |
| 1088 } | 1060 } |
| 1089 | 1061 |
| 1090 void assemblePrecompiledConstructor( | 1062 void assemblePrecompiledConstructor( |
| 1091 OutputUnit outputUnit, | 1063 OutputUnit outputUnit, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1150 embeddedNames.TYPEDEF_TYPE_PROPERTY_NAME, typeIndex); | 1122 embeddedNames.TYPEDEF_TYPE_PROPERTY_NAME, typeIndex); |
| 1151 builder.addPropertyByName( | 1123 builder.addPropertyByName( |
| 1152 embeddedNames.TYPEDEF_PREDICATE_PROPERTY_NAME, js.boolean(true)); | 1124 embeddedNames.TYPEDEF_PREDICATE_PROPERTY_NAME, js.boolean(true)); |
| 1153 | 1125 |
| 1154 // We can be pretty sure that the objectClass is initialized, since | 1126 // We can be pretty sure that the objectClass is initialized, since |
| 1155 // typedefs are only emitted with reflection, which requires lots of | 1127 // typedefs are only emitted with reflection, which requires lots of |
| 1156 // classes. | 1128 // classes. |
| 1157 assert(commonElements.objectClass != null); | 1129 assert(commonElements.objectClass != null); |
| 1158 builder.superName = namer.className(commonElements.objectClass); | 1130 builder.superName = namer.className(commonElements.objectClass); |
| 1159 jsAst.Node declaration = builder.toObjectInitializer(); | 1131 jsAst.Node declaration = builder.toObjectInitializer(); |
| 1160 jsAst.Name mangledName = namer.globalPropertyName(typedef); | 1132 jsAst.Name mangledName = namer.globalPropertyNameForType(typedef); |
| 1161 String reflectionName = getReflectionName(typedef, mangledName); | 1133 String reflectionName = getReflectionName(typedef, mangledName); |
| 1162 getLibraryDescriptor(library, mainFragment) | 1134 getLibraryDescriptor(library, mainFragment) |
| 1163 ..addProperty(mangledName, declaration) | 1135 ..addProperty(mangledName, declaration) |
| 1164 ..addPropertyByName("+$reflectionName", js.string('')); | 1136 ..addPropertyByName("+$reflectionName", js.string('')); |
| 1165 // Also emit a trivial constructor for CSP mode. | 1137 // Also emit a trivial constructor for CSP mode. |
| 1166 jsAst.Name constructorName = mangledName; | 1138 jsAst.Name constructorName = mangledName; |
| 1167 jsAst.Expression constructorAst = js('function() {}'); | 1139 jsAst.Expression constructorAst = js('function() {}'); |
| 1168 List<jsAst.Name> fieldNames = []; | 1140 List<jsAst.Name> fieldNames = []; |
| 1169 assemblePrecompiledConstructor( | 1141 assemblePrecompiledConstructor( |
| 1170 mainOutputUnit, constructorName, constructorAst, fieldNames); | 1142 mainOutputUnit, constructorName, constructorAst, fieldNames); |
| (...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1939 // data. | 1911 // data. |
| 1940 mapping["_comment"] = "This mapping shows which compiled `.js` files are " | 1912 mapping["_comment"] = "This mapping shows which compiled `.js` files are " |
| 1941 "needed for a given deferred library import."; | 1913 "needed for a given deferred library import."; |
| 1942 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); | 1914 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); |
| 1943 compiler.outputProvider( | 1915 compiler.outputProvider( |
| 1944 compiler.options.deferredMapUri.path, '', OutputType.info) | 1916 compiler.options.deferredMapUri.path, '', OutputType.info) |
| 1945 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) | 1917 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) |
| 1946 ..close(); | 1918 ..close(); |
| 1947 } | 1919 } |
| 1948 } | 1920 } |
| OLD | NEW |