| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart2js.js_emitter; |
| 6 |
| 7 class MainCallStubGenerator { |
| 8 final Compiler compiler; |
| 9 final JavaScriptBackend backend; |
| 10 final CodeEmitterTask emitterTask; |
| 11 |
| 12 MainCallStubGenerator(this.compiler, this.backend, this.emitterTask); |
| 13 |
| 14 /// Returns the code equivalent to: |
| 15 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` |
| 16 jsAst.Expression _buildIsolateSetupClosure(Element appMain, |
| 17 Element isolateMain) { |
| 18 jsAst.Expression mainAccess = |
| 19 emitterTask.isolateStaticClosureAccess(appMain); |
| 20 // Since we pass the closurized version of the main method to |
| 21 // the isolate method, we must make sure that it exists. |
| 22 return js('function(a){ #(#, a); }', |
| 23 [emitterTask.staticFunctionAccess(isolateMain), mainAccess]); |
| 24 } |
| 25 |
| 26 |
| 27 jsAst.Statement generateInvokeMain() { |
| 28 Element main = compiler.mainFunction; |
| 29 jsAst.Expression mainCallClosure = null; |
| 30 if (compiler.hasIsolateSupport) { |
| 31 Element isolateMain = |
| 32 backend.isolateHelperLibrary.find(JavaScriptBackend.START_ROOT_ISOLATE); |
| 33 mainCallClosure = _buildIsolateSetupClosure(main, isolateMain); |
| 34 } else if (compiler.hasIncrementalSupport) { |
| 35 mainCallClosure = js( |
| 36 'function() { return #(); }', |
| 37 emitterTask.staticFunctionAccess(main)); |
| 38 } else { |
| 39 mainCallClosure = emitterTask.staticFunctionAccess(main); |
| 40 } |
| 41 |
| 42 jsAst.Expression currentScriptAccess = |
| 43 emitterTask.generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); |
| 44 |
| 45 // This code finds the currently executing script by listening to the |
| 46 // onload event of all script tags and getting the first script which |
| 47 // finishes. Since onload is called immediately after execution this should |
| 48 // not substantially change execution order. |
| 49 return js.statement(''' |
| 50 (function (callback) { |
| 51 if (typeof document === "undefined") { |
| 52 callback(null); |
| 53 return; |
| 54 } |
| 55 if (document.currentScript) { |
| 56 callback(document.currentScript); |
| 57 return; |
| 58 } |
| 59 |
| 60 var scripts = document.scripts; |
| 61 function onLoad(event) { |
| 62 for (var i = 0; i < scripts.length; ++i) { |
| 63 scripts[i].removeEventListener("load", onLoad, false); |
| 64 } |
| 65 callback(event.target); |
| 66 } |
| 67 for (var i = 0; i < scripts.length; ++i) { |
| 68 scripts[i].addEventListener("load", onLoad, false); |
| 69 } |
| 70 })(function(currentScript) { |
| 71 #currentScript = currentScript; |
| 72 |
| 73 if (typeof dartMainRunner === "function") { |
| 74 dartMainRunner(#mainCallClosure, []); |
| 75 } else { |
| 76 #mainCallClosure([]); |
| 77 } |
| 78 })''', |
| 79 {'currentScript': currentScriptAccess, |
| 80 'mainCallClosure': mainCallClosure}); |
| 81 } |
| 82 } |
| OLD | NEW |