OLD | NEW |
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 || {}; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 this._imports = imports; | 60 this._imports = imports; |
61 this._loader = loader; | 61 this._loader = loader; |
62 | 62 |
63 // Cyclic import detection | 63 // Cyclic import detection |
64 this._state = LibraryLoader.NOT_LOADED; | 64 this._state = LibraryLoader.NOT_LOADED; |
65 } | 65 } |
66 | 66 |
67 loadImports() { | 67 loadImports() { |
68 let results = []; | 68 let results = []; |
69 for (let name of this._imports) { | 69 for (let name of this._imports) { |
70 results.push(import_(name)); | 70 let lib = libraries.get(name); |
| 71 if (!lib) { |
| 72 throwLibraryError('Library not available: ' + name); |
| 73 } |
| 74 results.push(lib.load()); |
71 } | 75 } |
72 return results; | 76 return results; |
73 } | 77 } |
74 | 78 |
75 load() { | 79 load() { |
76 // Check for cycles | 80 // Check for cycles |
77 if (this._state == LibraryLoader.LOADING) { | 81 if (this._state == LibraryLoader.LOADING) { |
78 throwLibraryError('Circular dependence on library: ' | 82 throwLibraryError('Circular dependence on library: ' |
79 + this._name); | 83 + this._name); |
80 } else if (this._state >= LibraryLoader.READY) { | 84 } else if (this._state >= LibraryLoader.READY) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 if (result) { | 145 if (result) { |
142 console.warn('Already loaded ' + name); | 146 console.warn('Already loaded ' + name); |
143 return result; | 147 return result; |
144 } | 148 } |
145 result = new LibraryLoader(name, defaultValue, imports, loader); | 149 result = new LibraryLoader(name, defaultValue, imports, loader); |
146 libraries.set(name, result); | 150 libraries.set(name, result); |
147 return result; | 151 return result; |
148 } | 152 } |
149 dart_library.library = library; | 153 dart_library.library = library; |
150 | 154 |
151 // Maintain a stack of active imports. If a requested library/module is not | 155 function import_(libraryName) { |
152 // available, print the stack to show where/how it was requested. | 156 let loader = libraries.get(libraryName); |
153 let _stack = []; | 157 // TODO(vsm): A user might call this directly from JS (as we do in tests). |
154 function import_(name) { | 158 // We may want a different error type. |
155 let lib = libraries.get(name); | 159 if (!loader) throwLibraryError('Library not found: ' + libraryName); |
156 if (!lib) { | 160 return loader.load(); |
157 let message = 'Module ' + name + ' not loaded in the browser.'; | |
158 if (_stack != []) { | |
159 message += '\nDependency via:'; | |
160 let indent = ''; | |
161 for (let last = _stack.length - 1; last >= 0; last--) { | |
162 indent += ' '; | |
163 message += '\n' + indent + '- ' + _stack[last]; | |
164 } | |
165 } | |
166 throwLibraryError(message); | |
167 } | |
168 _stack.push(name); | |
169 let result = lib.load(); | |
170 _stack.pop(); | |
171 return result; | |
172 } | 161 } |
173 dart_library.import = import_; | 162 dart_library.import = import_; |
174 | 163 |
175 var _currentIsolate = false; | 164 var _currentIsolate = false; |
176 | 165 |
177 function start(moduleName, libraryName) { | 166 function start(moduleName, libraryName) { |
178 if (libraryName == null) libraryName = moduleName; | 167 if (libraryName == null) libraryName = moduleName; |
179 let library = import_(moduleName)[libraryName]; | 168 let library = import_(moduleName)[libraryName]; |
180 let dart_sdk = import_('dart_sdk'); | 169 let dart_sdk = import_('dart_sdk'); |
181 | 170 |
182 if (!_currentIsolate) { | 171 if (!_currentIsolate) { |
183 // This import is only needed for chrome debugging. We should provide an | 172 // This import is only needed for chrome debugging. We should provide an |
184 // option to compile without it. | 173 // option to compile without it. |
185 dart_sdk._debugger.registerDevtoolsFormatter(); | 174 dart_sdk._debugger.registerDevtoolsFormatter(); |
186 | 175 |
187 // Create isolate. | 176 // Create isolate. |
188 _currentIsolate = true; | 177 _currentIsolate = true; |
189 dart_sdk._isolate_helper.startRootIsolate(() => {}, []); | 178 dart_sdk._isolate_helper.startRootIsolate(() => {}, []); |
190 } | 179 } |
191 | 180 |
192 library.main(); | 181 library.main(); |
193 } | 182 } |
194 dart_library.start = start; | 183 dart_library.start = start; |
195 | 184 |
196 })(dart_library); | 185 })(dart_library); |
197 } | 186 } |
OLD | NEW |