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 dart_library; |
| 9 (function (dart_library) { |
| 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 dart_utils.throwError('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 dart_utils.throwError('Circular dependence on library: ' |
| 60 + this._name); |
| 61 } else if (this._state >= LibraryLoader.LOADED) { |
| 62 return this._library; |
| 63 } |
| 64 this._state = LibraryLoader.LOADING; |
| 65 |
| 66 // Handle imports and record lazy imports |
| 67 let pendingSet = inheritedPendingSet ? inheritedPendingSet : new Set(); |
| 68 let args = this.loadImports(pendingSet); |
| 69 args = args.concat(this.deferLazyImports(pendingSet)); |
| 70 |
| 71 // Load the library |
| 72 args.unshift(this._library); |
| 73 this._loader.apply(null, args); |
| 74 this._state = LibraryLoader.LOADED; |
| 75 |
| 76 // Handle lazy imports |
| 77 if (inheritedPendingSet === void 0) { |
| 78 // Drain the queue |
| 79 this.loadLazyImports(pendingSet); |
| 80 } |
| 81 this._state = LibraryLoader.READY; |
| 82 return this._library; |
| 83 } |
| 84 |
| 85 stub() { |
| 86 return this._library; |
| 87 } |
| 88 } |
| 89 LibraryLoader.NOT_LOADED = 0; |
| 90 LibraryLoader.LOADING = 1; |
| 91 LibraryLoader.LOADED = 2; |
| 92 LibraryLoader.READY = 3; |
| 93 |
| 94 // Map from name to LibraryLoader |
| 95 let libraries = new Map(); |
| 96 |
| 97 function library(name, defaultValue, imports, lazyImports, loader) { |
| 98 return libraries[name] = |
| 99 new LibraryLoader(name, defaultValue, imports, lazyImports, loader); |
| 100 } |
| 101 dart_library.library = library; |
| 102 |
| 103 function import_(libraryName) { |
| 104 bootstrap(); |
| 105 let loader = libraries[libraryName]; |
| 106 return loader.load(); |
| 107 } |
| 108 dart_library.import = import_; |
| 109 |
| 110 function start(libraryName) { |
| 111 let library = import_(libraryName); |
| 112 let _isolate_helper = import_('dart/_isolate_helper'); |
| 113 _isolate_helper.startRootIsolate(library.main, []); |
| 114 } |
| 115 dart_library.start = start; |
| 116 |
| 117 let _bootstrapped = false; |
| 118 function bootstrap() { |
| 119 if (_bootstrapped) return; |
| 120 _bootstrapped = true; |
| 121 |
| 122 // Force import of core. |
| 123 import_('dart/core'); |
| 124 |
| 125 // TODO(vsm): DOM facades? |
| 126 // See: https://github.com/dart-lang/dev_compiler/issues/173 |
| 127 NodeList.prototype.get = function(i) { return this[i]; }; |
| 128 NamedNodeMap.prototype.get = function(i) { return this[i]; }; |
| 129 DOMTokenList.prototype.get = function(i) { return this[i]; }; |
| 130 HTMLCollection.prototype.get = function(i) { return this[i]; }; |
| 131 |
| 132 } |
| 133 |
| 134 })(dart_library || (dart_library = {})); |
OLD | NEW |