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 var dart, _js_helper, _js_primitives, dartx; | 5 var dart, _js_helper, _js_primitives, dartx; |
|
Jennifer Messerly
2015/06/09 14:51:52
_js_helper, _js_primitives could be removed here?
| |
| 6 (function (dart) { | 6 (function (dart) { |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 // TODO(vsm): This is referenced (as init.globalState) from | 9 // TODO(vsm): This is referenced (as init.globalState) from |
| 10 // isolate_helper.dart. Where should it go? | 10 // isolate_helper.dart. Where should it go? |
| 11 // See: https://github.com/dart-lang/dev_compiler/issues/164 | 11 // See: https://github.com/dart-lang/dev_compiler/issues/164 |
| 12 dart.globalState = null; | 12 dart.globalState = null; |
|
Jennifer Messerly
2015/06/09 14:51:52
initialize this down in bootstrap?
| |
| 13 | 13 |
| 14 const defineProperty = Object.defineProperty; | 14 const defineProperty = Object.defineProperty; |
| 15 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | 15 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; |
| 16 const getOwnPropertyNames = Object.getOwnPropertyNames; | 16 const getOwnPropertyNames = Object.getOwnPropertyNames; |
| 17 const getOwnPropertySymbols = Object.getOwnPropertySymbols; | 17 const getOwnPropertySymbols = Object.getOwnPropertySymbols; |
| 18 const hasOwnProperty = Object.prototype.hasOwnProperty; | 18 const hasOwnProperty = Object.prototype.hasOwnProperty; |
| 19 const slice = [].slice; | 19 const slice = [].slice; |
| 20 | 20 |
| 21 let _constructorSig = Symbol('sigCtor'); | 21 let _constructorSig = Symbol('sigCtor'); |
| 22 let _methodSig = Symbol("sig"); | 22 let _methodSig = Symbol("sig"); |
| (...skipping 1268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1291 | 1291 |
| 1292 // TODO(jmesserly): right now this is a sentinel. It should be a type object | 1292 // TODO(jmesserly): right now this is a sentinel. It should be a type object |
| 1293 // of some sort, assuming we keep around `dynamic` at runtime. | 1293 // of some sort, assuming we keep around `dynamic` at runtime. |
| 1294 dart.dynamic = { toString() { return 'dynamic'; }, get name() {return toString ();}}; | 1294 dart.dynamic = { toString() { return 'dynamic'; }, get name() {return toString ();}}; |
| 1295 dart.void = { toString() { return 'void'; }, get name() {return toString();}}; | 1295 dart.void = { toString() { return 'void'; }, get name() {return toString();}}; |
| 1296 dart.bottom = { toString() { return 'bottom'; }, get name() {return toString() ;}}; | 1296 dart.bottom = { toString() { return 'bottom'; }, get name() {return toString() ;}}; |
| 1297 | 1297 |
| 1298 dart.global = window || global; | 1298 dart.global = window || global; |
| 1299 dart.JsSymbol = Symbol; | 1299 dart.JsSymbol = Symbol; |
| 1300 | 1300 |
| 1301 // All libraries, including those that have referenced (lazyImport), | 1301 // Module support. This is a simplified module system for Dart. |
| 1302 // but not yet loaded. | 1302 // Longer term, we can easily migrate to an existing JS module system: |
| 1303 // ES6, AMD, RequireJS, .... | |
| 1304 | |
| 1305 class LibraryLoader { | |
| 1306 constructor(name, defaultValue, imports, lazyImports, loader) { | |
| 1307 this._name = name; | |
| 1308 this._library = defaultValue ? defaultValue : {}; | |
| 1309 this._imports = imports; | |
| 1310 this._lazyImports = lazyImports; | |
| 1311 this._loader = loader; | |
| 1312 | |
| 1313 // Cyclic import detection | |
| 1314 this._state = LibraryLoader.NOT_LOADED; | |
| 1315 } | |
| 1316 | |
| 1317 loadImports(pendingSet) { | |
| 1318 return this.handleImports(this._imports, (lib) => lib.load(pendingSet)); | |
| 1319 } | |
| 1320 | |
| 1321 deferLazyImports(pendingSet) { | |
| 1322 return this.handleImports(this._lazyImports, | |
| 1323 (lib) => { | |
| 1324 pendingSet.add(lib._name); | |
| 1325 return lib.stub(); | |
| 1326 }); | |
| 1327 } | |
| 1328 | |
| 1329 loadLazyImports(pendingSet) { | |
| 1330 return this.handleImports(pendingSet, (lib) => lib.load()); | |
| 1331 } | |
| 1332 | |
| 1333 handleImports(list, handler) { | |
| 1334 var results = []; | |
| 1335 for (let name of list) { | |
| 1336 var lib = libraries[name]; | |
|
Jennifer Messerly
2015/06/09 14:51:52
let?
| |
| 1337 if (!lib) { | |
| 1338 throwRuntimeError('Library not available: ' + name); | |
| 1339 } | |
| 1340 results.push(handler(lib)); | |
| 1341 } | |
| 1342 return results; | |
| 1343 } | |
| 1344 | |
| 1345 load(inheritedPendingSet) { | |
| 1346 // Check for cycles | |
| 1347 if (this._state == LibraryLoader.LOADING) { | |
| 1348 throwRuntimeError('Circular dependence on library: ' + this._name); | |
| 1349 } else if (this._state >= LibraryLoader.LOADED) { | |
| 1350 return this._library; | |
| 1351 } | |
| 1352 this._state = LibraryLoader.LOADING; | |
| 1353 | |
| 1354 // Handle imports and record lazy imports | |
| 1355 let pendingSet = inheritedPendingSet ? inheritedPendingSet : new Set(); | |
| 1356 var args = this.loadImports(pendingSet); | |
|
Jennifer Messerly
2015/06/09 14:51:51
here too
| |
| 1357 args = args.concat(this.deferLazyImports(pendingSet)); | |
| 1358 | |
| 1359 // Load the library | |
| 1360 args.unshift(this._library); | |
| 1361 this._loader.apply(null, args); | |
| 1362 this._state = LibraryLoader.LOADED; | |
| 1363 | |
| 1364 // Handle lazy imports | |
| 1365 if (inheritedPendingSet === void 0) { | |
| 1366 // Drain the queue | |
| 1367 this.loadLazyImports(pendingSet); | |
| 1368 } | |
| 1369 this._state = LibraryLoader.READY; | |
| 1370 return this._library; | |
| 1371 } | |
| 1372 | |
| 1373 stub() { | |
| 1374 return this._library; | |
| 1375 } | |
| 1376 } | |
| 1377 LibraryLoader.NOT_LOADED = 0; | |
| 1378 LibraryLoader.LOADING = 1; | |
| 1379 LibraryLoader.LOADED = 2; | |
| 1380 LibraryLoader.READY = 3; | |
| 1381 | |
| 1382 // Map from name to LibraryLoader | |
| 1303 var libraries = new Map(); | 1383 var libraries = new Map(); |
| 1304 | 1384 |
| 1305 // Completed libraries. | 1385 function library(name, defaultValue, imports, lazyImports, loader) { |
| 1306 var loadedLibraries = new Set(); | 1386 libraries[name] = new LibraryLoader(name, defaultValue, imports, lazyImports , loader); |
|
Jennifer Messerly
2015/06/09 14:51:51
long line
| |
| 1387 } | |
| 1388 dart.library = library; | |
| 1307 | 1389 |
| 1308 // Import a library by name. | 1390 function import_(libraryName) { |
| 1309 // This is exported for REPL / JS interop convenience. | 1391 bootstrap(); |
| 1310 function import_(name) { | 1392 let loader = libraries[libraryName]; |
| 1311 let loaded = loadedLibraries.has(name); | 1393 let library = loader.load(); |
| 1312 let value = libraries[name]; | 1394 return library; |
|
Jennifer Messerly
2015/06/09 14:51:52
very minor, but, the webstorm editor has a hint fo
| |
| 1313 // TODO(vsm): Change this to a hard throw. | |
| 1314 // For now, we're missing some libraries. E.g., dart:js: | |
| 1315 // https://github.com/dart-lang/dev_compiler/issues/168 | |
| 1316 if (!loaded) { | |
| 1317 console.warn('Missing required module: ' + name); | |
| 1318 } else if (!value) { | |
| 1319 throwRuntimeError('Library import error: ' + name) | |
| 1320 } | |
| 1321 return value; | |
| 1322 } | 1395 } |
| 1323 dart.import = import_; | 1396 dart.import = import_; |
| 1324 | 1397 |
| 1325 function initializeLibraryStub(name) { | |
| 1326 // Create the library object if necessary. | |
| 1327 if (!libraries[name]) { | |
| 1328 libraries[name] = {}; | |
| 1329 } | |
| 1330 return libraries[name]; | |
| 1331 } | |
| 1332 const lazyImport = initializeLibraryStub; | |
| 1333 | |
| 1334 function defineLibrary(name, defaultValue) { | |
| 1335 if (loadedLibraries.has(name)) { | |
| 1336 throwRuntimeError('Library is already defined: ' + name); | |
| 1337 } | |
| 1338 var value; | |
| 1339 if (defaultValue) { | |
| 1340 var oldValue = libraries[name]; | |
| 1341 if (oldValue && oldValue != defaultValue) { | |
| 1342 throwRuntimeError( | |
| 1343 `Library ${name} cannot be redefined to ${defaultValue}`); | |
| 1344 } | |
| 1345 libraries[name] = value = defaultValue; | |
| 1346 } else { | |
| 1347 value = initializeLibraryStub(name); | |
| 1348 } | |
| 1349 loadedLibraries.add(name); | |
| 1350 return value; | |
| 1351 } | |
| 1352 | |
| 1353 function library(name, defaultValue, imports, lazyImports, module) { | |
| 1354 var args = []; | |
| 1355 var lib = defineLibrary(name, defaultValue); | |
| 1356 args.push(lib); | |
| 1357 for (var i = 0; i < imports.length; ++i) { | |
| 1358 lib = import_(imports[i]); | |
| 1359 args.push(lib); | |
| 1360 } | |
| 1361 for (var i = 0; i < lazyImports.length; ++i) { | |
| 1362 lib = lazyImport(lazyImports[i]); | |
| 1363 args.push(lib); | |
| 1364 } | |
| 1365 module.apply(null, args); | |
| 1366 } | |
| 1367 dart.library = library; | |
| 1368 | |
| 1369 function start(libraryName) { | 1398 function start(libraryName) { |
| 1370 let lib = import_(libraryName); | 1399 let library = import_(libraryName); |
| 1371 _isolate_helper.startRootIsolate(lib.main, []); | 1400 _isolate_helper.startRootIsolate(library.main, []); |
| 1372 } | 1401 } |
| 1373 dart.start = start; | 1402 dart.start = start; |
| 1374 | 1403 |
| 1375 let core = lazyImport('dart/core'); | 1404 // Libraries used in this file. |
| 1376 let collection = lazyImport('dart/collection'); | 1405 var core; |
|
Jennifer Messerly
2015/06/09 14:51:51
I think these could be `let` too?
Main benefit of
| |
| 1377 let async = lazyImport('dart/async'); | 1406 var collection; |
| 1378 let _interceptors = lazyImport('dart/_interceptors'); | 1407 var async; |
| 1379 let _isolate_helper = lazyImport('dart/_isolate_helper'); | 1408 var _interceptors; |
| 1380 let _js_helper = lazyImport('dart/_js_helper'); | 1409 var _isolate_helper; |
| 1381 _js_helper.checkNum = notNull; | 1410 var _js_helper; |
| 1382 let _js_primitives = lazyImport('dart/_js_primitives'); | 1411 var _js_primitives; |
| 1383 _js_primitives.printString = (s) => console.log(s); | |
| 1384 | 1412 |
| 1385 // TODO(vsm): DOM facades? | 1413 function bootstrap() { |
| 1386 // See: https://github.com/dart-lang/dev_compiler/issues/173 | 1414 if (core) return; |
| 1387 NodeList.prototype.get = function(i) { return this[i]; }; | |
| 1388 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | |
| 1389 DOMTokenList.prototype.get = function(i) { return this[i]; }; | |
| 1390 | 1415 |
| 1391 /** Dart extension members. */ | 1416 let lazyImport = (name) => libraries[name].stub(); |
| 1392 dartx = dartx || {}; | |
| 1393 | 1417 |
| 1418 core = lazyImport('dart/core'); | |
| 1419 collection = lazyImport('dart/collection'); | |
| 1420 async = lazyImport('dart/async'); | |
| 1421 _interceptors = lazyImport('dart/_interceptors'); | |
| 1422 _isolate_helper = lazyImport('dart/_isolate_helper'); | |
| 1423 _js_helper = lazyImport('dart/_js_helper'); | |
| 1424 _js_helper.checkNum = notNull; | |
| 1425 _js_primitives = lazyImport('dart/_js_primitives'); | |
| 1426 _js_primitives.printString = (s) => console.log(s); | |
| 1427 | |
| 1428 // TODO(vsm): DOM facades? | |
| 1429 // See: https://github.com/dart-lang/dev_compiler/issues/173 | |
| 1430 NodeList.prototype.get = function(i) { return this[i]; }; | |
| 1431 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | |
| 1432 DOMTokenList.prototype.get = function(i) { return this[i]; }; | |
| 1433 | |
| 1434 /** Dart extension members. */ | |
| 1435 dartx = dartx || {}; | |
| 1436 } | |
| 1394 })(dart || (dart = {})); | 1437 })(dart || (dart = {})); |
| OLD | NEW |