Chromium Code Reviews| 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 INITIALIZE_LOADED_HUNK, | 17 INITIALIZE_LOADED_HUNK, |
| 18 IS_HUNK_INITIALIZED, | 18 IS_HUNK_INITIALIZED, |
| 19 IS_HUNK_LOADED; | 19 IS_HUNK_LOADED, |
| 20 GET_TYPE_FROM_NAME; | |
| 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; |
| 27 final ConstantEmitter constantEmitter; | 28 final ConstantEmitter constantEmitter; |
| 28 | 29 |
| 29 JavaScriptBackend get backend => compiler.backend; | 30 JavaScriptBackend get backend => compiler.backend; |
| (...skipping 98 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 = {}') | |
| 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 var holder = holders[holdersMap[name]]; | |
| 226 return holder[name]; | |
| 227 }"""); | |
| 228 return new js.Property(js.string(GET_TYPE_FROM_NAME), function); | |
| 229 } | |
| 230 | |
| 219 js.Expression emitDeferredFragment(DeferredFragment fragment, | 231 js.Expression emitDeferredFragment(DeferredFragment fragment, |
| 220 List<Holder> holders) { | 232 List<Holder> holders) { |
| 221 // TODO(floitsch): initialize eager classes. | 233 // TODO(floitsch): initialize eager classes. |
| 222 // TODO(floitsch): the hash must depend on the output. | 234 // TODO(floitsch): the hash must depend on the output. |
| 223 int hash = this.hashCode; | 235 int hash = this.hashCode; |
| 224 if (fragment.constants.isNotEmpty) { | 236 if (fragment.constants.isNotEmpty) { |
| 225 throw new UnimplementedError("constants in deferred units"); | 237 throw new UnimplementedError("constants in deferred units"); |
| 226 } | 238 } |
| 227 js.ArrayInitializer content = | 239 js.ArrayInitializer content = |
| 228 new js.ArrayInitializer(fragment.libraries.map(emitLibrary) | 240 new js.ArrayInitializer(fragment.libraries.map(emitLibrary) |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 function setupLibrary(library) { | 420 function setupLibrary(library) { |
| 409 var statics = library[0]; | 421 var statics = library[0]; |
| 410 for (var i = 0; i < statics.length; i += 3) { | 422 for (var i = 0; i < statics.length; i += 3) { |
| 411 var holderIndex = statics[i + 1]; | 423 var holderIndex = statics[i + 1]; |
| 412 setupStatic(statics[i], holders[holderIndex], statics[i + 2]); | 424 setupStatic(statics[i], holders[holderIndex], statics[i + 2]); |
| 413 } | 425 } |
| 414 | 426 |
| 415 var classes = library[1]; | 427 var classes = library[1]; |
| 416 for (var i = 0; i < classes.length; i += 3) { | 428 for (var i = 0; i < classes.length; i += 3) { |
| 417 var holderIndex = classes[i + 1]; | 429 var holderIndex = classes[i + 1]; |
| 430 holdersMap[classes[i]] = holderIndex; | |
|
floitsch
2015/01/23 10:46:31
You can directly store the holder in the map.
That
zarah
2015/01/23 12:22:18
Done.
| |
| 418 setupClass(classes[i], holders[holderIndex], classes[i + 2]); | 431 setupClass(classes[i], holders[holderIndex], classes[i + 2]); |
| 419 } | 432 } |
| 420 } | 433 } |
| 421 | 434 |
| 422 function setupLazyStatics(statics) { | 435 function setupLazyStatics(statics) { |
| 423 for (var i = 0; i < statics.length; i += 4) { | 436 for (var i = 0; i < statics.length; i += 4) { |
| 424 var name = statics[i]; | 437 var name = statics[i]; |
| 425 var getterName = statics[i + 1]; | 438 var getterName = statics[i + 1]; |
| 426 var holderIndex = statics[i + 2]; | 439 var holderIndex = statics[i + 2]; |
| 427 var initializer = statics[i + 3]; | 440 var initializer = statics[i + 3]; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 | 587 |
| 575 var end = Date.now(); | 588 var end = Date.now(); |
| 576 print('Setup: ' + (end - start) + ' ms.'); | 589 print('Setup: ' + (end - start) + ' ms.'); |
| 577 | 590 |
| 578 #main(); // Start main. | 591 #main(); // Start main. |
| 579 | 592 |
| 580 }(Date.now(), #code) | 593 }(Date.now(), #code) |
| 581 }"""; | 594 }"""; |
| 582 | 595 |
| 583 } | 596 } |
| OLD | NEW |