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 '../../constants/values.dart' show ConstantValue, FunctionConstantValue; | 7 import '../../constants/values.dart' show ConstantValue, FunctionConstantValue; |
| 8 import '../../dart2jslib.dart' show Compiler; | 8 import '../../dart2jslib.dart' show Compiler; |
| 9 import '../../dart_types.dart' show DartType; | 9 import '../../dart_types.dart' show DartType; |
| 10 import '../../elements/elements.dart' show ClassElement, FunctionElement; | 10 import '../../elements/elements.dart' show ClassElement, FunctionElement; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 int emitProgram(Program program) { | 127 int emitProgram(Program program) { |
| 128 List<Fragment> fragments = program.fragments; | 128 List<Fragment> fragments = program.fragments; |
| 129 MainFragment mainFragment = fragments.first; | 129 MainFragment mainFragment = fragments.first; |
| 130 | 130 |
| 131 int totalSize = 0; | 131 int totalSize = 0; |
| 132 | 132 |
| 133 // We have to emit the deferred fragments first, since we need their | 133 // We have to emit the deferred fragments first, since we need their |
| 134 // deferred hash (which depends on the output) when emitting the main | 134 // deferred hash (which depends on the output) when emitting the main |
| 135 // fragment. | 135 // fragment. |
| 136 fragments.skip(1).forEach((DeferredFragment deferredUnit) { | 136 fragments.skip(1).forEach((DeferredFragment deferredUnit) { |
| 137 List<String> types = program.metadataTypes[deferredUnit.outputUnit]; | 137 List<js.Expression> types = |
| 138 program.metadataTypes[deferredUnit.outputUnit]; | |
| 138 js.Expression ast = emitDeferredFragment(types, deferredUnit, | 139 js.Expression ast = emitDeferredFragment(types, deferredUnit, |
| 139 program.holders); | 140 program.holders); |
| 140 String code = js.prettyPrint(ast, compiler).getText(); | 141 String code = js.prettyPrint(ast, compiler).getText(); |
| 141 totalSize += code.length; | 142 totalSize += code.length; |
| 142 compiler.outputProvider(deferredUnit.outputFileName, deferredExtension) | 143 compiler.outputProvider(deferredUnit.outputFileName, deferredExtension) |
| 143 ..add(code) | 144 ..add(code) |
| 144 ..close(); | 145 ..close(); |
| 145 }); | 146 }); |
| 146 | 147 |
| 147 js.Statement mainAst = emitMainFragment(program); | 148 js.Statement mainAst = emitMainFragment(program); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 413 js.js( """function(name) { | 414 js.js( """function(name) { |
| 414 return holdersMap[name][name].ensureResolved(); | 415 return holdersMap[name][name].ensureResolved(); |
| 415 }"""); | 416 }"""); |
| 416 return new js.Property(js.string(GET_TYPE_FROM_NAME), function); | 417 return new js.Property(js.string(GET_TYPE_FROM_NAME), function); |
| 417 } | 418 } |
| 418 | 419 |
| 419 List<js.Property> emitMetadata(Program program) { | 420 List<js.Property> emitMetadata(Program program) { |
| 420 | 421 |
| 421 List<js.Property> metadataGlobals = <js.Property>[]; | 422 List<js.Property> metadataGlobals = <js.Property>[]; |
| 422 | 423 |
| 423 js.Property createGlobal(List<String> list, String global) { | 424 js.Property createGlobal(List<js.Expression> list, String global) { |
|
floitsch
2015/05/07 01:14:29
It's simple enough now, that I would inline it. Bu
Siggi Cherem (dart-lang)
2015/05/07 16:29:23
Done.
| |
| 424 String listAsString = "[${list.join(",")}]"; | 425 return new js.Property(js.string(global), new js.ArrayInitializer(list)); |
| 425 js.Expression metadata = | |
| 426 js.js.uncachedExpressionTemplate(listAsString).instantiate([]); | |
| 427 return new js.Property(js.string(global), metadata); | |
| 428 } | 426 } |
| 429 | 427 |
| 430 metadataGlobals.add(createGlobal(program.metadata, METADATA)); | 428 metadataGlobals.add(createGlobal(program.metadata, METADATA)); |
| 431 List<String> types = | 429 List<js.Expression> types = |
| 432 program.metadataTypes[program.fragments.first.outputUnit]; | 430 program.metadataTypes[program.fragments.first.outputUnit]; |
| 433 if (types == null) types = <String>[]; | 431 if (types == null) types = <js.Expression>[]; |
| 434 metadataGlobals.add(createGlobal(types, TYPES)); | 432 metadataGlobals.add(createGlobal(types, TYPES)); |
| 435 | 433 |
| 436 return metadataGlobals; | 434 return metadataGlobals; |
| 437 } | 435 } |
| 438 | 436 |
| 439 js.Expression emitDeferredFragment(List<String> types, | 437 js.Expression emitDeferredFragment(List<js.Expression> types, |
| 440 DeferredFragment fragment, | 438 DeferredFragment fragment, |
| 441 List<Holder> holders) { | 439 List<Holder> holders) { |
| 442 // TODO(floitsch): initialize eager classes. | 440 // TODO(floitsch): initialize eager classes. |
| 443 // TODO(floitsch): the hash must depend on the output. | 441 // TODO(floitsch): the hash must depend on the output. |
| 444 int hash = fragment.hashCode; | 442 int hash = fragment.hashCode; |
| 445 | 443 |
| 446 List<js.Expression> deferredCode = | 444 List<js.Expression> deferredCode = |
| 447 fragment.libraries.map(emitLibrary).toList(); | 445 fragment.libraries.map(emitLibrary).toList(); |
| 448 | 446 |
| 449 deferredCode.add( | 447 deferredCode.add( |
| 450 emitLazilyInitializedStatics(fragment.staticLazilyInitializedFields)); | 448 emitLazilyInitializedStatics(fragment.staticLazilyInitializedFields)); |
| 451 | 449 |
| 452 deferredCode.add(emitConstants(fragment.constants)); | 450 deferredCode.add(emitConstants(fragment.constants)); |
| 453 | 451 |
| 454 js.ArrayInitializer deferredArray = new js.ArrayInitializer(deferredCode); | 452 js.ArrayInitializer deferredArray = new js.ArrayInitializer(deferredCode); |
| 455 | 453 |
| 456 // This is the code that must be evaluated after all deferred classes have | 454 // This is the code that must be evaluated after all deferred classes have |
| 457 // been setup. | 455 // been setup. |
| 458 js.Statement immediateCode = | 456 js.Statement immediateCode = |
| 459 emitEagerClassInitializations(fragment.libraries); | 457 emitEagerClassInitializations(fragment.libraries); |
| 460 | 458 |
| 461 js.LiteralString immediateString = unparse(compiler, immediateCode); | 459 js.LiteralString immediateString = unparse(compiler, immediateCode); |
| 462 | 460 |
| 463 js.Expression deferredTypes = types == null | 461 js.Expression deferredTypes = js.string(types == null |
| 464 ? js.string("[]") | 462 ? "[]" |
| 465 : js.string("[${types.join(",")}]"); | 463 : js.prettyPrint(new js.ArrayInitializer(types), compiler).getText()); |
|
floitsch
2015/05/07 01:14:29
Use `unparse`. It does the js.string and pretty-pr
Siggi Cherem (dart-lang)
2015/05/07 16:29:23
Done.
| |
| 466 | 464 |
| 467 js.ArrayInitializer hunk = | 465 js.ArrayInitializer hunk = |
| 468 new js.ArrayInitializer([deferredArray, immediateString, | 466 new js.ArrayInitializer([deferredArray, immediateString, |
| 469 deferredTypes]); | 467 deferredTypes]); |
| 470 | 468 |
| 471 return js.js("$deferredInitializersGlobal[$hash] = #", hunk); | 469 return js.js("$deferredInitializersGlobal[$hash] = #", hunk); |
| 472 } | 470 } |
| 473 | 471 |
| 474 // This string should be referenced wherever JavaScript code makes assumptions | 472 // This string should be referenced wherever JavaScript code makes assumptions |
| 475 // on the constants format. | 473 // on the constants format. |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1176 | 1174 |
| 1177 var end = Date.now(); | 1175 var end = Date.now(); |
| 1178 // print('Setup: ' + (end - start) + ' ms.'); | 1176 // print('Setup: ' + (end - start) + ' ms.'); |
| 1179 | 1177 |
| 1180 #invokeMain; // Start main. | 1178 #invokeMain; // Start main. |
| 1181 | 1179 |
| 1182 }(Date.now(), #code) | 1180 }(Date.now(), #code) |
| 1183 }"""; | 1181 }"""; |
| 1184 | 1182 |
| 1185 } | 1183 } |
| OLD | NEW |