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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 411 |
411 js.Property emitGetTypeFromName() { | 412 js.Property emitGetTypeFromName() { |
412 js.Expression function = | 413 js.Expression function = |
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 List<js.Property> metadataGlobals = <js.Property>[]; | 421 List<js.Property> metadataGlobals = <js.Property>[]; |
422 | 422 metadataGlobals.add(new js.Property( |
423 js.Property createGlobal(List<String> list, String global) { | 423 js.string(METADATA), new js.ArrayInitializer(program.metadata))); |
424 String listAsString = "[${list.join(",")}]"; | 424 List<js.Expression> types = |
425 js.Expression metadata = | |
426 js.js.uncachedExpressionTemplate(listAsString).instantiate([]); | |
427 return new js.Property(js.string(global), metadata); | |
428 } | |
429 | |
430 metadataGlobals.add(createGlobal(program.metadata, METADATA)); | |
431 List<String> types = | |
432 program.metadataTypes[program.fragments.first.outputUnit]; | 425 program.metadataTypes[program.fragments.first.outputUnit]; |
433 if (types == null) types = <String>[]; | 426 if (types == null) types = <js.Expression>[]; |
434 metadataGlobals.add(createGlobal(types, TYPES)); | 427 metadataGlobals.add(new js.Property( |
| 428 js.string(TYPES), new js.ArrayInitializer(types))); |
435 | 429 |
436 return metadataGlobals; | 430 return metadataGlobals; |
437 } | 431 } |
438 | 432 |
439 js.Expression emitDeferredFragment(List<String> types, | 433 js.Expression emitDeferredFragment(List<js.Expression> types, |
440 DeferredFragment fragment, | 434 DeferredFragment fragment, |
441 List<Holder> holders) { | 435 List<Holder> holders) { |
442 // TODO(floitsch): initialize eager classes. | 436 // TODO(floitsch): initialize eager classes. |
443 // TODO(floitsch): the hash must depend on the output. | 437 // TODO(floitsch): the hash must depend on the output. |
444 int hash = fragment.hashCode; | 438 int hash = fragment.hashCode; |
445 | 439 |
446 List<js.Expression> deferredCode = | 440 List<js.Expression> deferredCode = |
447 fragment.libraries.map(emitLibrary).toList(); | 441 fragment.libraries.map(emitLibrary).toList(); |
448 | 442 |
449 deferredCode.add( | 443 deferredCode.add( |
450 emitLazilyInitializedStatics(fragment.staticLazilyInitializedFields)); | 444 emitLazilyInitializedStatics(fragment.staticLazilyInitializedFields)); |
451 | 445 |
452 deferredCode.add(emitConstants(fragment.constants)); | 446 deferredCode.add(emitConstants(fragment.constants)); |
453 | 447 |
454 js.ArrayInitializer deferredArray = new js.ArrayInitializer(deferredCode); | 448 js.ArrayInitializer deferredArray = new js.ArrayInitializer(deferredCode); |
455 | 449 |
456 // This is the code that must be evaluated after all deferred classes have | 450 // This is the code that must be evaluated after all deferred classes have |
457 // been setup. | 451 // been setup. |
458 js.Statement immediateCode = | 452 js.Statement immediateCode = |
459 emitEagerClassInitializations(fragment.libraries); | 453 emitEagerClassInitializations(fragment.libraries); |
460 | 454 |
461 js.LiteralString immediateString = unparse(compiler, immediateCode); | 455 js.LiteralString immediateString = unparse(compiler, immediateCode); |
462 | 456 |
463 js.Expression deferredTypes = types == null | 457 js.Expression deferredTypes = types == null |
464 ? js.string("[]") | 458 ? js.string("[]") |
465 : js.string("[${types.join(",")}]"); | 459 : unparse(compiler, new js.ArrayInitializer(types)); |
466 | 460 |
467 js.ArrayInitializer hunk = | 461 js.ArrayInitializer hunk = |
468 new js.ArrayInitializer([deferredArray, immediateString, | 462 new js.ArrayInitializer([deferredArray, immediateString, |
469 deferredTypes]); | 463 deferredTypes]); |
470 | 464 |
471 return js.js("$deferredInitializersGlobal[$hash] = #", hunk); | 465 return js.js("$deferredInitializersGlobal[$hash] = #", hunk); |
472 } | 466 } |
473 | 467 |
474 // This string should be referenced wherever JavaScript code makes assumptions | 468 // This string should be referenced wherever JavaScript code makes assumptions |
475 // on the constants format. | 469 // on the constants format. |
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1176 | 1170 |
1177 var end = Date.now(); | 1171 var end = Date.now(); |
1178 // print('Setup: ' + (end - start) + ' ms.'); | 1172 // print('Setup: ' + (end - start) + ' ms.'); |
1179 | 1173 |
1180 #invokeMain; // Start main. | 1174 #invokeMain; // Start main. |
1181 | 1175 |
1182 }(Date.now(), #code) | 1176 }(Date.now(), #code) |
1183 }"""; | 1177 }"""; |
1184 | 1178 |
1185 } | 1179 } |
OLD | NEW |