Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: pkg/dev_compiler/lib/js/legacy/dart_library.js

Issue 2697093002: Deferred parsing for legacy modules (Closed)
Patch Set: Removing pending Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 /* This file defines the module loader for the dart runtime. 5 /* This file defines the module loader for the dart runtime.
6 */ 6 */
7 var dart_library; 7 var dart_library;
8 if (!dart_library) { 8 if (!dart_library) {
9 dart_library = 9 dart_library =
10 typeof module != "undefined" && module.exports || {}; 10 typeof module != "undefined" && module.exports || {};
11 11
12 (function (dart_library) { 12 (function (dart_library) {
13 'use strict'; 13 'use strict';
14 14
15 /** Note that we cannot use dart_utils.throwInternalError from here. */ 15 /** Note that we cannot use dart_utils.throwInternalError from here. */
16 function throwLibraryError(message) { 16 function throwLibraryError(message) {
17 throw Error(message); 17 throw Error(message);
18 } 18 }
19 19
20 const dartLibraryName = Symbol('dartLibraryName'); 20 const dartLibraryName = Symbol('dartLibraryName');
21 dart_library.dartLibraryName = dartLibraryName; 21 dart_library.dartLibraryName = dartLibraryName;
22 22
23 const libraryImports = Symbol('libraryImports'); 23 const libraryImports = Symbol('libraryImports');
24 dart_library.libraryImports = libraryImports; 24 dart_library.libraryImports = libraryImports;
25 25
26 // Module support. This is a simplified module system for Dart. 26 // Module support. This is a simplified module system for Dart.
27 // Longer term, we can easily migrate to an existing JS module system: 27 // Longer term, we can easily migrate to an existing JS module system:
28 // ES6, AMD, RequireJS, .... 28 // ES6, AMD, RequireJS, ....
29 29
30 // Returns a proxy that delegates to the underlying loader.
31 // This defers loading of a module until a library is actually used.
32 const loadedModule = Symbol('loadedModule');
33 dart_library.defer = function(module, name, patch) {
34 var revocable = Proxy.revocable(module, {
35 get: function(o, p) {
36 var mod = o[loadedModule];
37 var lib = mod[name];
38 // Install unproxied module and library in caller's context.
39 patch(mod, lib);
40 // Ensure proxy is only used on first access.
41 revocable.revoke();
42 return lib[p];
43 }
44 });
45 return revocable.proxy;
46 };
47
30 class LibraryLoader { 48 class LibraryLoader {
31 49
32 constructor(name, defaultValue, imports, loader) { 50 constructor(name, defaultValue, imports, loader) {
33 this._name = name; 51 this._name = name;
34 this._library = defaultValue ? defaultValue : {}; 52 this._library = defaultValue ? defaultValue : {};
35 this._imports = imports; 53 this._imports = imports;
36 this._loader = loader; 54 this._loader = loader;
37 55
38 // Cyclic import detection 56 // Cyclic import detection
39 this._state = LibraryLoader.NOT_LOADED; 57 this._state = LibraryLoader.NOT_LOADED;
(...skipping 18 matching lines...) Expand all
58 + this._name); 76 + this._name);
59 } else if (this._state >= LibraryLoader.READY) { 77 } else if (this._state >= LibraryLoader.READY) {
60 return this._library; 78 return this._library;
61 } 79 }
62 this._state = LibraryLoader.LOADING; 80 this._state = LibraryLoader.LOADING;
63 81
64 // Handle imports 82 // Handle imports
65 let args = this.loadImports(); 83 let args = this.loadImports();
66 84
67 // Load the library 85 // Load the library
68 args.unshift(this._library); 86 let loader = this;
69 this._loader.apply(null, args); 87 let library = this._library;
88 library[loadedModule] = library;
89 args.unshift(library);
90
91 if (this._name == 'dart_sdk') {
92 // Eagerly load the SDK.
93 this._loader.apply(null, args);
94 loader._loader = null;
95 } else {
96 // Load / parse other modules on demand.
97 let done = false;
98 this._library = new Proxy(args, {
99 get: function(o, name) {
100 if (done) {
101 return library[name];
102 }
103 done = true;
104 loader._loader.apply(null, o);
105 loader._loader = null;
106 return library[name];
107 }
108 });
109 }
110
70 this._state = LibraryLoader.READY; 111 this._state = LibraryLoader.READY;
71 this._library[dartLibraryName] = this._name; 112 this._library[dartLibraryName] = this._name;
72 this._library[libraryImports] = this._imports; 113 this._library[libraryImports] = this._imports;
73 return this._library; 114 return this._library;
74 } 115 }
75 116
76 stub() { 117 stub() {
77 return this._library; 118 return this._library;
78 } 119 }
79 } 120 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 return loader.load(); 155 return loader.load();
115 } 156 }
116 dart_library.import = import_; 157 dart_library.import = import_;
117 158
118 var _currentIsolate = false; 159 var _currentIsolate = false;
119 160
120 function start(moduleName, libraryName) { 161 function start(moduleName, libraryName) {
121 if (libraryName == null) libraryName = moduleName; 162 if (libraryName == null) libraryName = moduleName;
122 let library = import_(moduleName)[libraryName]; 163 let library = import_(moduleName)[libraryName];
123 let dart_sdk = import_('dart_sdk'); 164 let dart_sdk = import_('dart_sdk');
165
124 if (!_currentIsolate) { 166 if (!_currentIsolate) {
125 // Create isolate and run main. 167 // Create isolate.
126 _currentIsolate = true; 168 _currentIsolate = true;
127 dart_sdk._isolate_helper.startRootIsolate(library.main, []); 169 dart_sdk._isolate_helper.startRootIsolate(() => {}, []);
128 } else {
129 // Main isolate is already initialized - just run main.
130 library.main();
131 } 170 }
171
172 library.main();
132 } 173 }
133 dart_library.start = start; 174 dart_library.start = start;
134 175
135 let _bootstrapped = false; 176 let _bootstrapped = false;
136 function bootstrap() { 177 function bootstrap() {
137 if (_bootstrapped) return; 178 if (_bootstrapped) return;
138 _bootstrapped = true; 179 _bootstrapped = true;
139 180
140 // Force import of core. 181 // Force import of core.
141 var dart_sdk = import_('dart_sdk'); 182 var dart_sdk = import_('dart_sdk');
142 183
143 // This import is only needed for chrome debugging. We should provide an 184 // This import is only needed for chrome debugging. We should provide an
144 // option to compile without it. 185 // option to compile without it.
145 dart_sdk._debugger.registerDevtoolsFormatter(); 186 dart_sdk._debugger.registerDevtoolsFormatter();
146 } 187 }
147 188
148 })(dart_library); 189 })(dart_library);
149 } 190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698