| 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.new_js_emitter.model_emitter; | 5 library dart2js.new_js_emitter.model_emitter; |
| 6 | 6 |
| 7 import '../../dart2jslib.dart' show Compiler; | 7 import '../../dart2jslib.dart' show Compiler; |
| 8 import '../../js/js.dart' as js; | 8 import '../../js/js.dart' as js; |
| 9 import '../../js_backend/js_backend.dart' show | 9 import '../../js_backend/js_backend.dart' show |
| 10 JavaScriptBackend, | 10 JavaScriptBackend, |
| 11 Namer, | 11 Namer, |
| 12 ConstantEmitter; | 12 ConstantEmitter; |
| 13 | 13 |
| 14 import 'package:_internal/compiler/js_lib/shared/embedded_names.dart' show | 14 import 'package:_internal/compiler/js_lib/shared/embedded_names.dart' show |
| 15 DEFERRED_LIBRARY_URIS, | 15 DEFERRED_LIBRARY_URIS, |
| 16 DEFERRED_LIBRARY_HASHES, | 16 DEFERRED_LIBRARY_HASHES, |
| 17 GET_TYPE_FROM_NAME, |
| 17 INITIALIZE_LOADED_HUNK, | 18 INITIALIZE_LOADED_HUNK, |
| 18 IS_HUNK_INITIALIZED, | 19 IS_HUNK_INITIALIZED, |
| 19 IS_HUNK_LOADED; | 20 IS_HUNK_LOADED; |
| 20 | 21 |
| 21 import '../js_emitter.dart' show NativeGenerator; | 22 import '../js_emitter.dart' show NativeGenerator; |
| 22 import '../model.dart'; | 23 import '../model.dart'; |
| 23 | 24 |
| 24 class ModelEmitter { | 25 class ModelEmitter { |
| 25 final Compiler compiler; | 26 final Compiler compiler; |
| 26 final Namer namer; | 27 final Namer namer; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 // that covers the entire program. | 129 // that covers the entire program. |
| 129 | 130 |
| 130 List<js.Statement> statements = [ | 131 List<js.Statement> statements = [ |
| 131 new js.ExpressionStatement( | 132 new js.ExpressionStatement( |
| 132 new js.VariableDeclarationList(holders.map((e) => | 133 new js.VariableDeclarationList(holders.map((e) => |
| 133 new js.VariableInitialization( | 134 new js.VariableInitialization( |
| 134 new js.VariableDeclaration(e.name, allowRename: false), | 135 new js.VariableDeclaration(e.name, allowRename: false), |
| 135 new js.ObjectInitializer(const []))).toList())), | 136 new js.ObjectInitializer(const []))).toList())), |
| 136 js.js.statement('var holders = #', new js.ArrayInitializer( | 137 js.js.statement('var holders = #', new js.ArrayInitializer( |
| 137 holders.map((e) => new js.VariableUse(e.name)) | 138 holders.map((e) => new js.VariableUse(e.name)) |
| 138 .toList(growable: false))) | 139 .toList(growable: false))), |
| 140 js.js.statement('var holdersMap = Object.create(null)') |
| 139 ]; | 141 ]; |
| 140 return new js.Block(statements); | 142 return new js.Block(statements); |
| 141 } | 143 } |
| 142 | 144 |
| 143 static js.Template get makeConstantListTemplate { | 145 static js.Template get makeConstantListTemplate { |
| 144 // TODO(floitsch): remove hard-coded name. | 146 // TODO(floitsch): remove hard-coded name. |
| 145 // TODO(floitsch): there is no harm in caching the template. | 147 // TODO(floitsch): there is no harm in caching the template. |
| 146 return js.js.uncachedExpressionTemplate('makeConstList(#)'); | 148 return js.js.uncachedExpressionTemplate('makeConstList(#)'); |
| 147 } | 149 } |
| 148 | 150 |
| 149 js.Block emitEmbeddedGlobals(Map<String, List<Fragment>> loadMap) { | 151 js.Block emitEmbeddedGlobals(Map<String, List<Fragment>> loadMap) { |
| 150 List<js.Property> globals = <js.Property>[]; | 152 List<js.Property> globals = <js.Property>[]; |
| 151 | 153 |
| 152 if (loadMap.isNotEmpty) { | 154 if (loadMap.isNotEmpty) { |
| 153 globals.addAll(emitLoadUrisAndHashes(loadMap)); | 155 globals.addAll(emitLoadUrisAndHashes(loadMap)); |
| 154 globals.add(emitIsHunkLoadedFunction()); | 156 globals.add(emitIsHunkLoadedFunction()); |
| 155 globals.add(emitInitializeLoadedHunk()); | 157 globals.add(emitInitializeLoadedHunk()); |
| 156 } | 158 } |
| 157 | 159 |
| 160 globals.add(emitGetTypeFromName()); |
| 158 js.ObjectInitializer globalsObject = new js.ObjectInitializer(globals); | 161 js.ObjectInitializer globalsObject = new js.ObjectInitializer(globals); |
| 159 | 162 |
| 160 List<js.Statement> statements = | 163 List<js.Statement> statements = |
| 161 [new js.ExpressionStatement( | 164 [new js.ExpressionStatement( |
| 162 new js.VariableDeclarationList( | 165 new js.VariableDeclarationList( |
| 163 [new js.VariableInitialization( | 166 [new js.VariableInitialization( |
| 164 new js.VariableDeclaration("init", allowRename: false), | 167 new js.VariableDeclaration("init", allowRename: false), |
| 165 globalsObject)]))]; | 168 globalsObject)]))]; |
| 166 return new js.Block(statements); | 169 return new js.Block(statements); |
| 167 } | 170 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 js.js("function(hash) { return !!$deferredInitializersGlobal[hash]; }"); | 212 js.js("function(hash) { return !!$deferredInitializersGlobal[hash]; }"); |
| 210 return new js.Property(js.string(IS_HUNK_LOADED), function); | 213 return new js.Property(js.string(IS_HUNK_LOADED), function); |
| 211 } | 214 } |
| 212 | 215 |
| 213 js.Property emitInitializeLoadedHunk() { | 216 js.Property emitInitializeLoadedHunk() { |
| 214 js.Expression function = | 217 js.Expression function = |
| 215 js.js("function(hash) { eval($deferredInitializersGlobal[hash]); }"); | 218 js.js("function(hash) { eval($deferredInitializersGlobal[hash]); }"); |
| 216 return new js.Property(js.string(INITIALIZE_LOADED_HUNK), function); | 219 return new js.Property(js.string(INITIALIZE_LOADED_HUNK), function); |
| 217 } | 220 } |
| 218 | 221 |
| 222 js.Property emitGetTypeFromName() { |
| 223 js.Expression function = |
| 224 js.js( """function(name) { |
| 225 return holdersMap[name][name].ensureResolved(); |
| 226 }"""); |
| 227 return new js.Property(js.string(GET_TYPE_FROM_NAME), function); |
| 228 } |
| 229 |
| 219 js.Expression emitDeferredFragment(DeferredFragment fragment, | 230 js.Expression emitDeferredFragment(DeferredFragment fragment, |
| 220 List<Holder> holders) { | 231 List<Holder> holders) { |
| 221 // TODO(floitsch): initialize eager classes. | 232 // TODO(floitsch): initialize eager classes. |
| 222 // TODO(floitsch): the hash must depend on the output. | 233 // TODO(floitsch): the hash must depend on the output. |
| 223 int hash = this.hashCode; | 234 int hash = this.hashCode; |
| 224 if (fragment.constants.isNotEmpty) { | 235 if (fragment.constants.isNotEmpty) { |
| 225 throw new UnimplementedError("constants in deferred units"); | 236 throw new UnimplementedError("constants in deferred units"); |
| 226 } | 237 } |
| 227 js.ArrayInitializer content = | 238 js.ArrayInitializer content = |
| 228 new js.ArrayInitializer(fragment.libraries.map(emitLibrary) | 239 new js.ArrayInitializer(fragment.libraries.map(emitLibrary) |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 function setupLibrary(library) { | 410 function setupLibrary(library) { |
| 400 var statics = library[0]; | 411 var statics = library[0]; |
| 401 for (var i = 0; i < statics.length; i += 3) { | 412 for (var i = 0; i < statics.length; i += 3) { |
| 402 var holderIndex = statics[i + 1]; | 413 var holderIndex = statics[i + 1]; |
| 403 setupStatic(statics[i], holders[holderIndex], statics[i + 2]); | 414 setupStatic(statics[i], holders[holderIndex], statics[i + 2]); |
| 404 } | 415 } |
| 405 | 416 |
| 406 var classes = library[1]; | 417 var classes = library[1]; |
| 407 for (var i = 0; i < classes.length; i += 3) { | 418 for (var i = 0; i < classes.length; i += 3) { |
| 408 var holderIndex = classes[i + 1]; | 419 var holderIndex = classes[i + 1]; |
| 420 holdersMap[classes[i]] = holders[holderIndex]; |
| 409 setupClass(classes[i], holders[holderIndex], classes[i + 2]); | 421 setupClass(classes[i], holders[holderIndex], classes[i + 2]); |
| 410 } | 422 } |
| 411 } | 423 } |
| 412 | 424 |
| 413 function setupLazyStatics(statics) { | 425 function setupLazyStatics(statics) { |
| 414 for (var i = 0; i < statics.length; i += 4) { | 426 for (var i = 0; i < statics.length; i += 4) { |
| 415 var name = statics[i]; | 427 var name = statics[i]; |
| 416 var getterName = statics[i + 1]; | 428 var getterName = statics[i + 1]; |
| 417 var holderIndex = statics[i + 2]; | 429 var holderIndex = statics[i + 2]; |
| 418 var initializer = statics[i + 3]; | 430 var initializer = statics[i + 3]; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 | 577 |
| 566 var end = Date.now(); | 578 var end = Date.now(); |
| 567 print('Setup: ' + (end - start) + ' ms.'); | 579 print('Setup: ' + (end - start) + ' ms.'); |
| 568 | 580 |
| 569 #main(); // Start main. | 581 #main(); // Start main. |
| 570 | 582 |
| 571 }(Date.now(), #code) | 583 }(Date.now(), #code) |
| 572 }"""; | 584 }"""; |
| 573 | 585 |
| 574 } | 586 } |
| OLD | NEW |