| 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.main_call_stub_generator; | 5 library dart2js.js_emitter.main_call_stub_generator; |
| 6 | 6 |
| 7 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; | 7 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; |
| 8 | 8 |
| 9 import '../common_elements.dart'; |
| 9 import '../elements/entities.dart'; | 10 import '../elements/entities.dart'; |
| 10 import '../js/js.dart' as jsAst; | 11 import '../js/js.dart' as jsAst; |
| 11 import '../js/js.dart' show js; | 12 import '../js/js.dart' show js; |
| 12 import '../js_backend/js_backend.dart' show JavaScriptBackend; | 13 import '../js_backend/backend_usage.dart' show BackendUsage; |
| 13 | 14 |
| 14 import 'code_emitter_task.dart' show CodeEmitterTask; | 15 import 'code_emitter_task.dart' show Emitter; |
| 15 | 16 |
| 16 class MainCallStubGenerator { | 17 class MainCallStubGenerator { |
| 17 final JavaScriptBackend backend; | 18 final CommonElements _commonElements; |
| 18 final CodeEmitterTask emitterTask; | 19 final Emitter _emitter; |
| 20 final BackendUsage _backendUsage; |
| 19 | 21 |
| 20 MainCallStubGenerator(this.backend, this.emitterTask); | 22 MainCallStubGenerator( |
| 23 this._commonElements, this._emitter, this._backendUsage); |
| 21 | 24 |
| 22 /// Returns the code equivalent to: | 25 /// Returns the code equivalent to: |
| 23 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` | 26 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` |
| 24 jsAst.Expression _buildIsolateSetupClosure( | 27 jsAst.Expression _buildIsolateSetupClosure( |
| 25 FunctionEntity appMain, FunctionEntity isolateMain) { | 28 FunctionEntity appMain, FunctionEntity isolateMain) { |
| 26 jsAst.Expression mainAccess = | 29 jsAst.Expression mainAccess = _emitter.isolateStaticClosureAccess(appMain); |
| 27 emitterTask.isolateStaticClosureAccess(appMain); | |
| 28 // Since we pass the closurized version of the main method to | 30 // Since we pass the closurized version of the main method to |
| 29 // the isolate method, we must make sure that it exists. | 31 // the isolate method, we must make sure that it exists. |
| 30 return js('function(a){ #(#, a); }', | 32 return js('function(a){ #(#, a); }', |
| 31 [emitterTask.staticFunctionAccess(isolateMain), mainAccess]); | 33 [_emitter.staticFunctionAccess(isolateMain), mainAccess]); |
| 32 } | 34 } |
| 33 | 35 |
| 34 jsAst.Statement generateInvokeMain(FunctionEntity main) { | 36 jsAst.Statement generateInvokeMain(FunctionEntity main) { |
| 35 jsAst.Expression mainCallClosure = null; | 37 jsAst.Expression mainCallClosure = null; |
| 36 if (backend.backendUsage.isIsolateInUse) { | 38 if (_backendUsage.isIsolateInUse) { |
| 37 FunctionEntity isolateMain = backend.commonElements.startRootIsolate; | 39 FunctionEntity isolateMain = _commonElements.startRootIsolate; |
| 38 mainCallClosure = _buildIsolateSetupClosure(main, isolateMain); | 40 mainCallClosure = _buildIsolateSetupClosure(main, isolateMain); |
| 39 } else { | 41 } else { |
| 40 mainCallClosure = emitterTask.staticFunctionAccess(main); | 42 mainCallClosure = _emitter.staticFunctionAccess(main); |
| 41 } | 43 } |
| 42 | 44 |
| 43 jsAst.Expression currentScriptAccess = | 45 jsAst.Expression currentScriptAccess = |
| 44 emitterTask.generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); | 46 _emitter.generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); |
| 45 | 47 |
| 46 // This code finds the currently executing script by listening to the | 48 // This code finds the currently executing script by listening to the |
| 47 // onload event of all script tags and getting the first script which | 49 // onload event of all script tags and getting the first script which |
| 48 // finishes. Since onload is called immediately after execution this should | 50 // finishes. Since onload is called immediately after execution this should |
| 49 // not substantially change execution order. | 51 // not substantially change execution order. |
| 50 return js.statement( | 52 return js.statement( |
| 51 ''' | 53 ''' |
| 52 (function (callback) { | 54 (function (callback) { |
| 53 if (typeof document === "undefined") { | 55 if (typeof document === "undefined") { |
| 54 callback(null); | 56 callback(null); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 79 } else { | 81 } else { |
| 80 #mainCallClosure([]); | 82 #mainCallClosure([]); |
| 81 } | 83 } |
| 82 })''', | 84 })''', |
| 83 { | 85 { |
| 84 'currentScript': currentScriptAccess, | 86 'currentScript': currentScriptAccess, |
| 85 'mainCallClosure': mainCallClosure | 87 'mainCallClosure': mainCallClosure |
| 86 }); | 88 }); |
| 87 } | 89 } |
| 88 } | 90 } |
| OLD | NEW |