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