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 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
6 | 6 |
7 class MainCallStubGenerator { | 7 class MainCallStubGenerator { |
8 final Compiler compiler; | 8 final Compiler compiler; |
9 final JavaScriptBackend backend; | 9 final JavaScriptBackend backend; |
10 final CodeEmitterTask emitterTask; | 10 final CodeEmitterTask emitterTask; |
11 | 11 |
12 MainCallStubGenerator(this.compiler, this.backend, this.emitterTask); | 12 MainCallStubGenerator(this.compiler, this.backend, this.emitterTask); |
13 | 13 |
14 BackendHelpers get helpers => backend.helpers; | 14 BackendHelpers get helpers => backend.helpers; |
15 | 15 |
16 /// Returns the code equivalent to: | 16 /// Returns the code equivalent to: |
17 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` | 17 /// `function(args) { $.startRootIsolate(X.main$closure(), args); }` |
18 jsAst.Expression _buildIsolateSetupClosure(Element appMain, | 18 jsAst.Expression _buildIsolateSetupClosure( |
19 Element isolateMain) { | 19 Element appMain, Element isolateMain) { |
20 jsAst.Expression mainAccess = | 20 jsAst.Expression mainAccess = |
21 emitterTask.isolateStaticClosureAccess(appMain); | 21 emitterTask.isolateStaticClosureAccess(appMain); |
22 // Since we pass the closurized version of the main method to | 22 // Since we pass the closurized version of the main method to |
23 // the isolate method, we must make sure that it exists. | 23 // the isolate method, we must make sure that it exists. |
24 return js('function(a){ #(#, a); }', | 24 return js('function(a){ #(#, a); }', |
25 [emitterTask.staticFunctionAccess(isolateMain), mainAccess]); | 25 [emitterTask.staticFunctionAccess(isolateMain), mainAccess]); |
26 } | 26 } |
27 | 27 |
28 jsAst.Statement generateInvokeMain() { | 28 jsAst.Statement generateInvokeMain() { |
29 Element main = compiler.mainFunction; | 29 Element main = compiler.mainFunction; |
30 jsAst.Expression mainCallClosure = null; | 30 jsAst.Expression mainCallClosure = null; |
31 if (compiler.hasIsolateSupport) { | 31 if (compiler.hasIsolateSupport) { |
32 Element isolateMain = | 32 Element isolateMain = |
33 helpers.isolateHelperLibrary.find(BackendHelpers.START_ROOT_ISOLATE); | 33 helpers.isolateHelperLibrary.find(BackendHelpers.START_ROOT_ISOLATE); |
34 mainCallClosure = _buildIsolateSetupClosure(main, isolateMain); | 34 mainCallClosure = _buildIsolateSetupClosure(main, isolateMain); |
35 } else if (compiler.options.hasIncrementalSupport) { | 35 } else if (compiler.options.hasIncrementalSupport) { |
36 mainCallClosure = js( | 36 mainCallClosure = js( |
37 'function() { return #(); }', | 37 'function() { return #(); }', emitterTask.staticFunctionAccess(main)); |
38 emitterTask.staticFunctionAccess(main)); | |
39 } else { | 38 } else { |
40 mainCallClosure = emitterTask.staticFunctionAccess(main); | 39 mainCallClosure = emitterTask.staticFunctionAccess(main); |
41 } | 40 } |
42 | 41 |
43 jsAst.Expression currentScriptAccess = | 42 jsAst.Expression currentScriptAccess = |
44 emitterTask.generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); | 43 emitterTask.generateEmbeddedGlobalAccess(embeddedNames.CURRENT_SCRIPT); |
45 | 44 |
46 // This code finds the currently executing script by listening to the | 45 // This code finds the currently executing script by listening to the |
47 // onload event of all script tags and getting the first script which | 46 // onload event of all script tags and getting the first script which |
48 // finishes. Since onload is called immediately after execution this should | 47 // finishes. Since onload is called immediately after execution this should |
49 // not substantially change execution order. | 48 // not substantially change execution order. |
50 return js.statement(''' | 49 return js.statement( |
| 50 ''' |
51 (function (callback) { | 51 (function (callback) { |
52 if (typeof document === "undefined") { | 52 if (typeof document === "undefined") { |
53 callback(null); | 53 callback(null); |
54 return; | 54 return; |
55 } | 55 } |
56 // When running as a content-script of a chrome-extension the | 56 // When running as a content-script of a chrome-extension the |
57 // 'currentScript' is `null` (but not undefined). | 57 // 'currentScript' is `null` (but not undefined). |
58 if (typeof document.currentScript != 'undefined') { | 58 if (typeof document.currentScript != 'undefined') { |
59 callback(document.currentScript); | 59 callback(document.currentScript); |
60 return; | 60 return; |
(...skipping 11 matching lines...) Expand all Loading... |
72 } | 72 } |
73 })(function(currentScript) { | 73 })(function(currentScript) { |
74 #currentScript = currentScript; | 74 #currentScript = currentScript; |
75 | 75 |
76 if (typeof dartMainRunner === "function") { | 76 if (typeof dartMainRunner === "function") { |
77 dartMainRunner(#mainCallClosure, []); | 77 dartMainRunner(#mainCallClosure, []); |
78 } else { | 78 } else { |
79 #mainCallClosure([]); | 79 #mainCallClosure([]); |
80 } | 80 } |
81 })''', | 81 })''', |
82 {'currentScript': currentScriptAccess, | 82 { |
83 'mainCallClosure': mainCallClosure}); | 83 'currentScript': currentScriptAccess, |
| 84 'mainCallClosure': mainCallClosure |
| 85 }); |
84 } | 86 } |
85 } | 87 } |
OLD | NEW |