OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /* This file defines the module loader for the dart runtime. |
| 6 */ |
| 7 |
| 8 var loader; |
| 9 (function (loader) { |
| 10 'use strict'; |
| 11 |
| 12 // Module support. This is a simplified module system for Dart. |
| 13 // Longer term, we can easily migrate to an existing JS module system: |
| 14 // ES6, AMD, RequireJS, .... |
| 15 |
| 16 class LibraryLoader { |
| 17 constructor(name, defaultValue, imports, lazyImports, loader) { |
| 18 this._name = name; |
| 19 this._library = defaultValue ? defaultValue : {}; |
| 20 this._imports = imports; |
| 21 this._lazyImports = lazyImports; |
| 22 this._loader = loader; |
| 23 |
| 24 // Cyclic import detection |
| 25 this._state = LibraryLoader.NOT_LOADED; |
| 26 } |
| 27 |
| 28 loadImports(pendingSet) { |
| 29 return this.handleImports(this._imports, (lib) => lib.load(pendingSet)); |
| 30 } |
| 31 |
| 32 deferLazyImports(pendingSet) { |
| 33 return this.handleImports(this._lazyImports, |
| 34 (lib) => { |
| 35 pendingSet.add(lib._name); |
| 36 return lib.stub(); |
| 37 }); |
| 38 } |
| 39 |
| 40 loadLazyImports(pendingSet) { |
| 41 return this.handleImports(pendingSet, (lib) => lib.load()); |
| 42 } |
| 43 |
| 44 handleImports(list, handler) { |
| 45 let results = []; |
| 46 for (let name of list) { |
| 47 let lib = libraries[name]; |
| 48 if (!lib) { |
| 49 js_utils.throwRuntimeError('Library not available: ' + name); |
| 50 } |
| 51 results.push(handler(lib)); |
| 52 } |
| 53 return results; |
| 54 } |
| 55 |
| 56 load(inheritedPendingSet) { |
| 57 // Check for cycles |
| 58 if (this._state == LibraryLoader.LOADING) { |
| 59 js_utils.throwRuntimeError('Circular dependence on library: ' + this._na
me); |
| 60 } else if (this._state >= LibraryLoader.LOADED) { |
| 61 return this._library; |
| 62 } |
| 63 this._state = LibraryLoader.LOADING; |
| 64 |
| 65 // Handle imports and record lazy imports |
| 66 let pendingSet = inheritedPendingSet ? inheritedPendingSet : new Set(); |
| 67 let args = this.loadImports(pendingSet); |
| 68 args = args.concat(this.deferLazyImports(pendingSet)); |
| 69 |
| 70 // Load the library |
| 71 args.unshift(this._library); |
| 72 this._loader.apply(null, args); |
| 73 this._state = LibraryLoader.LOADED; |
| 74 |
| 75 // Handle lazy imports |
| 76 if (inheritedPendingSet === void 0) { |
| 77 // Drain the queue |
| 78 this.loadLazyImports(pendingSet); |
| 79 } |
| 80 this._state = LibraryLoader.READY; |
| 81 return this._library; |
| 82 } |
| 83 |
| 84 stub() { |
| 85 return this._library; |
| 86 } |
| 87 } |
| 88 LibraryLoader.NOT_LOADED = 0; |
| 89 LibraryLoader.LOADING = 1; |
| 90 LibraryLoader.LOADED = 2; |
| 91 LibraryLoader.READY = 3; |
| 92 |
| 93 // Map from name to LibraryLoader |
| 94 let libraries = new Map(); |
| 95 |
| 96 function library(name, defaultValue, imports, lazyImports, loader) { |
| 97 return libraries[name] = |
| 98 new LibraryLoader(name, defaultValue, imports, lazyImports, loader); |
| 99 } |
| 100 loader.library = library; |
| 101 |
| 102 function import_(libraryName) { |
| 103 bootstrap(); |
| 104 let loader = libraries[libraryName]; |
| 105 return loader.load(); |
| 106 } |
| 107 loader.import = import_; |
| 108 |
| 109 function start(libraryName) { |
| 110 let library = import_(libraryName); |
| 111 let _isolate_helper = import_('dart/_isolate_helper'); |
| 112 _isolate_helper.startRootIsolate(library.main, []); |
| 113 } |
| 114 loader.start = start; |
| 115 |
| 116 let _bootstrapped = false; |
| 117 function bootstrap() { |
| 118 if (_bootstrapped) return; |
| 119 _bootstrapped = true; |
| 120 |
| 121 // Force import of core. |
| 122 import_('dart/core'); |
| 123 |
| 124 // TODO(vsm): DOM facades? |
| 125 // See: https://github.com/dart-lang/dev_compiler/issues/173 |
| 126 NodeList.prototype.get = function(i) { return this[i]; }; |
| 127 NamedNodeMap.prototype.get = function(i) { return this[i]; }; |
| 128 DOMTokenList.prototype.get = function(i) { return this[i]; }; |
| 129 HTMLCollection.prototype.get = function(i) { return this[i]; }; |
| 130 |
| 131 } |
| 132 |
| 133 })(loader || (loader = {})); |
OLD | NEW |