Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 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. | 30 // Returns a proxy that delegates to the underlying loader. |
| 31 // This defers loading of a module until a library is actually used. | 31 // This defers loading of a module until a library is actually used. |
| 32 const loadedModule = Symbol('loadedModule'); | 32 const loadedModule = Symbol('loadedModule'); |
| 33 dart_library.defer = function(module, name, patch) { | 33 dart_library.defer = function(module, name, patch) { |
| 34 var revocable = Proxy.revocable(module, { | 34 let done = false; |
| 35 function loadDeferred() { | |
| 36 done = true; | |
| 37 var mod = module[loadedModule]; | |
| 38 var lib = mod[name]; | |
| 39 // Install unproxied module and library in caller's context. | |
| 40 patch(mod, lib); | |
| 41 } | |
| 42 // The deferred library object. Note, the only legal operations on a Dart | |
| 43 // library object should be get (to read a top-level variable, method, or | |
| 44 // Class) or set (to write a top-level variable). | |
|
vsm
2017/02/17 15:16:17
A couple notes:
(1) I'm no longer revoking the pro
| |
| 45 return new Proxy({}, { | |
| 35 get: function(o, p) { | 46 get: function(o, p) { |
| 36 var mod = o[loadedModule]; | 47 if (!done) loadDeferred(); |
| 37 var lib = mod[name]; | 48 return module[name][p]; |
| 38 // Install unproxied module and library in caller's context. | 49 }, |
| 39 patch(mod, lib); | 50 set: function(o, p, value) { |
| 40 // Ensure proxy is only used on first access. | 51 if (!done) loadDeferred(); |
| 41 revocable.revoke(); | 52 module[name][p] = value; |
| 42 return lib[p]; | 53 return true; |
| 43 } | 54 }, |
| 44 }); | 55 }); |
| 45 return revocable.proxy; | |
| 46 }; | 56 }; |
| 47 | 57 |
| 48 class LibraryLoader { | 58 class LibraryLoader { |
| 49 | 59 |
| 50 constructor(name, defaultValue, imports, loader) { | 60 constructor(name, defaultValue, imports, loader) { |
| 51 this._name = name; | 61 this._name = name; |
| 52 this._library = defaultValue ? defaultValue : {}; | 62 this._library = defaultValue ? defaultValue : {}; |
| 53 this._imports = imports; | 63 this._imports = imports; |
| 54 this._loader = loader; | 64 this._loader = loader; |
| 55 | 65 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 78 return this._library; | 88 return this._library; |
| 79 } | 89 } |
| 80 this._state = LibraryLoader.LOADING; | 90 this._state = LibraryLoader.LOADING; |
| 81 | 91 |
| 82 // Handle imports | 92 // Handle imports |
| 83 let args = this.loadImports(); | 93 let args = this.loadImports(); |
| 84 | 94 |
| 85 // Load the library | 95 // Load the library |
| 86 let loader = this; | 96 let loader = this; |
| 87 let library = this._library; | 97 let library = this._library; |
| 98 library[dartLibraryName] = this._name; | |
| 99 library[libraryImports] = this._imports; | |
|
vsm
2017/02/17 15:16:17
Note: these were getting set on the proxy instead
| |
| 88 library[loadedModule] = library; | 100 library[loadedModule] = library; |
| 89 args.unshift(library); | 101 args.unshift(library); |
| 90 | 102 |
| 91 if (this._name == 'dart_sdk') { | 103 if (this._name == 'dart_sdk') { |
| 92 // Eagerly load the SDK. | 104 // Eagerly load the SDK. |
| 93 this._loader.apply(null, args); | 105 this._loader.apply(null, args); |
| 94 loader._loader = null; | 106 loader._loader = null; |
| 95 } else { | 107 } else { |
| 96 // Load / parse other modules on demand. | 108 // Load / parse other modules on demand. |
| 97 let done = false; | 109 let done = false; |
| 98 this._library = new Proxy(args, { | 110 this._library = new Proxy(library, { |
|
vsm
2017/02/17 15:16:17
Note, this is the module object, not the library o
| |
| 99 get: function(o, name) { | 111 get: function(o, name) { |
| 100 if (done) { | 112 if (!done) { |
| 101 return library[name]; | 113 done = true; |
| 114 loader._loader.apply(null, args); | |
| 115 loader._loader = null; | |
| 102 } | 116 } |
| 103 done = true; | 117 return o[name]; |
| 104 loader._loader.apply(null, o); | |
| 105 loader._loader = null; | |
| 106 return library[name]; | |
| 107 } | 118 } |
| 108 }); | 119 }); |
| 109 } | 120 } |
| 110 | 121 |
| 111 this._state = LibraryLoader.READY; | 122 this._state = LibraryLoader.READY; |
| 112 this._library[dartLibraryName] = this._name; | |
| 113 this._library[libraryImports] = this._imports; | |
| 114 return this._library; | 123 return this._library; |
| 115 } | 124 } |
| 116 | 125 |
| 117 stub() { | 126 stub() { |
| 118 return this._library; | 127 return this._library; |
| 119 } | 128 } |
| 120 } | 129 } |
| 121 LibraryLoader.NOT_LOADED = 0; | 130 LibraryLoader.NOT_LOADED = 0; |
| 122 LibraryLoader.LOADING = 1; | 131 LibraryLoader.LOADING = 1; |
| 123 LibraryLoader.READY = 2; | 132 LibraryLoader.READY = 2; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 // Force import of core. | 190 // Force import of core. |
| 182 var dart_sdk = import_('dart_sdk'); | 191 var dart_sdk = import_('dart_sdk'); |
| 183 | 192 |
| 184 // This import is only needed for chrome debugging. We should provide an | 193 // This import is only needed for chrome debugging. We should provide an |
| 185 // option to compile without it. | 194 // option to compile without it. |
| 186 dart_sdk._debugger.registerDevtoolsFormatter(); | 195 dart_sdk._debugger.registerDevtoolsFormatter(); |
| 187 } | 196 } |
| 188 | 197 |
| 189 })(dart_library); | 198 })(dart_library); |
| 190 } | 199 } |
| OLD | NEW |