Index: lib/runtime/dart_runtime.js |
diff --git a/lib/runtime/dart_runtime.js b/lib/runtime/dart_runtime.js |
index 468ac5dd7aabdfe7c4bef2a83ea2643f3b83f548..8ca447fae1c3ebe3a1a2d78923bea2f9441c3e59 100644 |
--- a/lib/runtime/dart_runtime.js |
+++ b/lib/runtime/dart_runtime.js |
@@ -50,6 +50,10 @@ var dart, _js_helper, _js_primitives, dartx; |
} |
dart.dput = dput; |
+ function throwRuntimeError(message) { |
+ throw Error(message); |
+ } |
+ |
// TODO(jmesserly): this should call noSuchMethod, not throw. |
function throwNoSuchMethod(obj, name, args, opt_func) { |
if (obj === void 0) obj = opt_func; |
@@ -426,15 +430,15 @@ var dart, _js_helper, _js_primitives, dartx; |
/** Checks that `x` is not null or undefined. */ |
function notNull(x) { |
- if (x == null) throw 'expected not-null value'; |
+ if (x == null) throwRuntimeError('expected not-null value'); |
return x; |
} |
dart.notNull = notNull; |
function _typeName(type) { |
- if (type === void 0) throw "Undefined type"; |
+ if (type === void 0) throwRuntimeError('Undefined type'); |
var name = type.name; |
- if (!name) throw 'Unexpected type: ' + type; |
+ if (!name) throwRuntimeError('Unexpected type: ' + type); |
return name; |
} |
@@ -722,7 +726,7 @@ var dart, _js_helper, _js_primitives, dartx; |
function lazyGetter() { |
// Clear the init function to detect circular initialization. |
let f = init; |
- if (f === null) throw 'circular initialization for field ' + name; |
+ if (f === null) throwRuntimeError('circular initialization for field ' + name); |
init = null; |
// Compute and store the value. |
@@ -943,8 +947,7 @@ var dart, _js_helper, _js_primitives, dartx; |
dart.map = map; |
function assert(condition) { |
- // TODO(jmesserly): throw assertion error. |
- if (!condition) throw 'assertion failed'; |
+ if (!condition) throw new core.AssertionError(); |
} |
dart.assert = assert; |
@@ -978,12 +981,12 @@ var dart, _js_helper, _js_primitives, dartx; |
/** Memoize a generic type constructor function. */ |
function generic(typeConstructor) { |
let length = typeConstructor.length; |
- if (length < 1) throw Error('must have at least one generic type argument'); |
+ if (length < 1) throwRuntimeError('must have at least one generic type argument'); |
let resultMap = new Map(); |
function makeGenericType(/*...arguments*/) { |
if (arguments.length != length && arguments.length != 0) { |
- throw Error('requires ' + length + ' or 0 type arguments'); |
+ throwRuntimeError('requires ' + length + ' or 0 type arguments'); |
} |
let args = slice.call(arguments); |
// TODO(leafp): This should really be core.Object for |
@@ -996,7 +999,7 @@ var dart, _js_helper, _js_primitives, dartx; |
for (let i = 0; i < length; i++) { |
let arg = args[i]; |
if (arg == null) { |
- throw Error('type arguments should not be null: ' + typeConstructor); |
+ throwRuntimeError('type arguments should not be null: ' + typeConstructor); |
} |
let map = value; |
value = map.get(arg); |
@@ -1279,32 +1282,88 @@ var dart, _js_helper, _js_primitives, dartx; |
dart.global = window || global; |
dart.JsSymbol = Symbol; |
- function import_(value) { |
+ // All libraries, including those that have referenced (lazyImport), |
+ // but not yet loaded. |
+ var libraries = new Map(); |
+ |
+ // Completed libraries. |
+ var loadedLibraries = new Set(); |
+ |
+ // Import a library by name. |
+ // This is exported for REPL / JS interop convenience. |
+ function import_(name) { |
+ let loaded = loadedLibraries.has(name); |
+ let value = libraries[name]; |
// TODO(vsm): Change this to a hard throw. |
// For now, we're missing some libraries. E.g., dart:js: |
// https://github.com/dart-lang/dev_compiler/issues/168 |
- if (!value) { |
- console.log('missing required module'); |
+ if (!loaded) { |
+ console.warn('Missing required module: ' + name); |
+ } else if (!value) { |
+ throwRuntimeError('Library import error: ' + name) |
} |
return value; |
} |
dart.import = import_; |
- |
- function lazyImport(value) { |
- return defineLibrary(value, {}); |
+ |
+ function initializeLibraryStub(name) { |
+ // Create the library object if necessary. |
+ if (!libraries[name]) { |
+ libraries[name] = {}; |
+ } |
+ return libraries[name]; |
+ } |
+ const lazyImport = initializeLibraryStub; |
+ |
+ function defineLibrary(name, defaultValue) { |
+ if (loadedLibraries.has(name)) { |
+ throwRuntimeError('Library is already defined: ' + name); |
+ } |
+ var value; |
+ if (defaultValue) { |
+ var oldValue = libraries[name]; |
+ if (oldValue && oldValue != defaultValue) { |
+ throwRuntimeError( |
+ `Library ${name} cannot be redefined to ${defaultValue}`); |
+ } |
+ libraries[name] = value = defaultValue; |
+ } else { |
+ value = initializeLibraryStub(name); |
+ } |
+ loadedLibraries.add(name); |
+ return value; |
} |
- dart.lazyImport = lazyImport; |
- function defineLibrary(value, defaultValue) { |
- return value ? value : defaultValue; |
+ function library(name, defaultValue, imports, lazyImports, module) { |
+ var args = []; |
+ var lib = defineLibrary(name, defaultValue); |
+ args.push(lib); |
+ for (var i = 0; i < imports.length; ++i) { |
+ lib = import_(imports[i]); |
+ args.push(lib); |
+ } |
+ for (var i = 0; i < lazyImports.length; ++i) { |
+ lib = lazyImport(lazyImports[i]); |
+ args.push(lib); |
+ } |
+ module.apply(null, args); |
} |
- dart.defineLibrary = defineLibrary; |
+ dart.library = library; |
- // TODO(jmesserly): hack to bootstrap the SDK |
- _js_helper = _js_helper || {}; |
- _js_helper.checkNum = notNull; |
+ function start(libraryName) { |
+ let lib = import_(libraryName); |
+ _isolate_helper.startRootIsolate(lib.main, []); |
+ } |
+ dart.start = start; |
- _js_primitives = _js_primitives || {}; |
+ let core = lazyImport('dart/core'); |
+ let collection = lazyImport('dart/collection'); |
+ let async = lazyImport('dart/async'); |
+ let _interceptors = lazyImport('dart/_interceptors'); |
+ let _isolate_helper = lazyImport('dart/_isolate_helper'); |
+ let _js_helper = lazyImport('dart/_js_helper'); |
+ _js_helper.checkNum = notNull; |
+ let _js_primitives = lazyImport('dart/_js_primitives'); |
_js_primitives.printString = (s) => console.log(s); |
// TODO(vsm): DOM facades? |