| 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 typeof module != "undefined" && module.exports || {}; | |
| 10 | |
| 11 (function (dart_library) { | |
| 12 'use strict'; | |
| 13 | |
| 14 /** Note that we cannot use dart_utils.throwInternalError from here. */ | |
| 15 function throwLibraryError(message) { | |
| 16 throw Error(message); | |
| 17 } | |
| 18 | |
| 19 const dartLibraryName = Symbol('dartLibraryName'); | |
| 20 dart_library.dartLibraryName = dartLibraryName; | |
| 21 | |
| 22 const libraryImports = Symbol('libraryImports'); | |
| 23 dart_library.libraryImports = libraryImports; | |
| 24 | |
| 25 // Module support. This is a simplified module system for Dart. | |
| 26 // Longer term, we can easily migrate to an existing JS module system: | |
| 27 // ES6, AMD, RequireJS, .... | |
| 28 | |
| 29 class LibraryLoader { | |
| 30 | |
| 31 constructor(name, defaultValue, imports, loader) { | |
| 32 this._name = name; | |
| 33 this._library = defaultValue ? defaultValue : {}; | |
| 34 this._imports = imports; | |
| 35 this._loader = loader; | |
| 36 | |
| 37 // Cyclic import detection | |
| 38 this._state = LibraryLoader.NOT_LOADED; | |
| 39 } | |
| 40 | |
| 41 loadImports() { | |
| 42 let results = []; | |
| 43 for (let name of this._imports) { | |
| 44 let lib = libraries.get(name); | |
| 45 if (!lib) { | |
| 46 throwLibraryError('Library not available: ' + name); | |
| 47 } | |
| 48 results.push(lib.load()); | |
| 49 } | |
| 50 return results; | |
| 51 } | |
| 52 | |
| 53 load() { | |
| 54 // Check for cycles | |
| 55 if (this._state == LibraryLoader.LOADING) { | |
| 56 throwLibraryError('Circular dependence on library: ' | |
| 57 + this._name); | |
| 58 } else if (this._state >= LibraryLoader.READY) { | |
| 59 return this._library; | |
| 60 } | |
| 61 this._state = LibraryLoader.LOADING; | |
| 62 | |
| 63 // Handle imports | |
| 64 let args = this.loadImports(); | |
| 65 | |
| 66 // Load the library | |
| 67 args.unshift(this._library); | |
| 68 this._loader.apply(null, args); | |
| 69 this._state = LibraryLoader.READY; | |
| 70 this._library[dartLibraryName] = this._name; | |
| 71 this._library[libraryImports] = this._imports; | |
| 72 return this._library; | |
| 73 } | |
| 74 | |
| 75 stub() { | |
| 76 return this._library; | |
| 77 } | |
| 78 } | |
| 79 LibraryLoader.NOT_LOADED = 0; | |
| 80 LibraryLoader.LOADING = 1; | |
| 81 LibraryLoader.READY = 2; | |
| 82 | |
| 83 // Map from name to LibraryLoader | |
| 84 let libraries = new Map(); | |
| 85 dart_library.libraries = function() { return libraries.keys(); }; | |
| 86 dart_library.debuggerLibraries = function() { | |
| 87 var debuggerLibraries = []; | |
| 88 libraries.forEach(function (value, key, map) { | |
| 89 debuggerLibraries.push(value.load()); | |
| 90 }); | |
| 91 debuggerLibraries.__proto__ = null; | |
| 92 return debuggerLibraries; | |
| 93 }; | |
| 94 | |
| 95 function library(name, defaultValue, imports, loader) { | |
| 96 let result = new LibraryLoader(name, defaultValue, imports, loader); | |
| 97 libraries.set(name, result); | |
| 98 return result; | |
| 99 } | |
| 100 dart_library.library = library; | |
| 101 | |
| 102 function import_(libraryName) { | |
| 103 bootstrap(); | |
| 104 let loader = libraries.get(libraryName); | |
| 105 // TODO(vsm): A user might call this directly from JS (as we do in tests). | |
| 106 // We may want a different error type. | |
| 107 if (!loader) throwLibraryError('Library not found: ' + libraryName); | |
| 108 return loader.load(); | |
| 109 } | |
| 110 dart_library.import = import_; | |
| 111 | |
| 112 function start(moduleName, libraryName) { | |
| 113 if (libraryName == null) libraryName = moduleName; | |
| 114 let library = import_(moduleName)[libraryName]; | |
| 115 let dart_sdk = import_('dart_sdk'); | |
| 116 dart_sdk._isolate_helper.startRootIsolate(library.main, []); | |
| 117 } | |
| 118 dart_library.start = start; | |
| 119 | |
| 120 let _bootstrapped = false; | |
| 121 function bootstrap() { | |
| 122 if (_bootstrapped) return; | |
| 123 _bootstrapped = true; | |
| 124 | |
| 125 // Force import of core. | |
| 126 var dart_sdk = import_('dart_sdk'); | |
| 127 | |
| 128 // TODO(vsm): DOM facades? | |
| 129 // See: https://github.com/dart-lang/dev_compiler/issues/173 | |
| 130 if (typeof NodeList !== "undefined") { | |
| 131 NodeList.prototype.get = function(i) { return this[i]; }; | |
| 132 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | |
| 133 DOMTokenList.prototype.get = function(i) { return this[i]; }; | |
| 134 HTMLCollection.prototype.get = function(i) { return this[i]; }; | |
| 135 // Expose constructors for DOM types dart:html needs to assume are | |
| 136 // available on window. | |
| 137 if (typeof PannerNode == "undefined") { | |
| 138 let audioContext = new AudioContext(); | |
| 139 window.PannerNode = audioContext.createPanner().constructor; | |
| 140 window.StereoPannerNode = audioContext.createStereoPanner().constructor; | |
| 141 } | |
| 142 if (typeof AudioSourceNode == "undefined") { | |
| 143 window.AudioSourceNode = MediaElementAudioSourceNode.constructor; | |
| 144 } | |
| 145 if (typeof FontFaceSet == "undefined") { | |
| 146 window.FontFaceSet = document.fonts.__proto__.constructor; | |
| 147 } | |
| 148 if (typeof MemoryInfo == "undefined") { | |
| 149 window.MemoryInfo = window.performance.memory.constructor; | |
| 150 } | |
| 151 if (typeof Geolocation == "undefined") { | |
| 152 navigator.geolocation.constructor; | |
| 153 } | |
| 154 if (typeof Animation == "undefined") { | |
| 155 let d = document.createElement('div'); | |
| 156 window.Animation = d.animate(d).constructor; | |
| 157 } | |
| 158 if (typeof SourceBufferList == "undefined") { | |
| 159 window.SourceBufferList = new MediaSource().sourceBuffers.constructor; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 // This import is only needed for chrome debugging. We should provide an | |
| 164 // option to compile without it. | |
| 165 dart_sdk._debugger.registerDevtoolsFormatter(); | |
| 166 } | |
| 167 | |
| 168 })(dart_library); | |
| OLD | NEW |