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; |
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? |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 dart.dload = dload; | 43 dart.dload = dload; |
44 | 44 |
45 function dput(obj, field, value) { | 45 function dput(obj, field, value) { |
46 field = _canonicalFieldName(obj, field, [value], field); | 46 field = _canonicalFieldName(obj, field, [value], field); |
47 // TODO(vsm): Implement NSM and type checks. | 47 // TODO(vsm): Implement NSM and type checks. |
48 // See: https://github.com/dart-lang/dev_compiler/issues/170 | 48 // See: https://github.com/dart-lang/dev_compiler/issues/170 |
49 obj[field] = value; | 49 obj[field] = value; |
50 } | 50 } |
51 dart.dput = dput; | 51 dart.dput = dput; |
52 | 52 |
| 53 function throwRuntimeError(message) { |
| 54 throw Error(message); |
| 55 } |
| 56 |
53 // TODO(jmesserly): this should call noSuchMethod, not throw. | 57 // TODO(jmesserly): this should call noSuchMethod, not throw. |
54 function throwNoSuchMethod(obj, name, args, opt_func) { | 58 function throwNoSuchMethod(obj, name, args, opt_func) { |
55 if (obj === void 0) obj = opt_func; | 59 if (obj === void 0) obj = opt_func; |
56 throw new core.NoSuchMethodError(obj, name, args); | 60 throw new core.NoSuchMethodError(obj, name, args); |
57 } | 61 } |
58 | 62 |
59 function checkAndCall(f, ftype, obj, args, name) { | 63 function checkAndCall(f, ftype, obj, args, name) { |
60 if (!(f instanceof Function)) { | 64 if (!(f instanceof Function)) { |
61 // We're not a function (and hence not a method either) | 65 // We're not a function (and hence not a method either) |
62 // Grab the `call` method if it's not a function. | 66 // Grab the `call` method if it's not a function. |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 | 423 |
420 function equals(x, y) { | 424 function equals(x, y) { |
421 if (x == null || y == null) return x == y; | 425 if (x == null || y == null) return x == y; |
422 let eq = x['==']; | 426 let eq = x['==']; |
423 return eq ? eq.call(x, y) : x == y; | 427 return eq ? eq.call(x, y) : x == y; |
424 } | 428 } |
425 dart.equals = equals; | 429 dart.equals = equals; |
426 | 430 |
427 /** Checks that `x` is not null or undefined. */ | 431 /** Checks that `x` is not null or undefined. */ |
428 function notNull(x) { | 432 function notNull(x) { |
429 if (x == null) throw 'expected not-null value'; | 433 if (x == null) throwRuntimeError('expected not-null value'); |
430 return x; | 434 return x; |
431 } | 435 } |
432 dart.notNull = notNull; | 436 dart.notNull = notNull; |
433 | 437 |
434 function _typeName(type) { | 438 function _typeName(type) { |
435 if (type === void 0) throw "Undefined type"; | 439 if (type === void 0) throwRuntimeError('Undefined type'); |
436 var name = type.name; | 440 var name = type.name; |
437 if (!name) throw 'Unexpected type: ' + type; | 441 if (!name) throwRuntimeError('Unexpected type: ' + type); |
438 return name; | 442 return name; |
439 } | 443 } |
440 | 444 |
441 class AbstractFunctionType { | 445 class AbstractFunctionType { |
442 constructor() { | 446 constructor() { |
443 this._stringValue = null; | 447 this._stringValue = null; |
444 } | 448 } |
445 | 449 |
446 /// Check that a function of this type can be applied to | 450 /// Check that a function of this type can be applied to |
447 /// actuals. | 451 /// actuals. |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). | 719 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). |
716 function defineLazyProperty(to, name, desc) { | 720 function defineLazyProperty(to, name, desc) { |
717 let init = desc.get; | 721 let init = desc.get; |
718 let writable = !!desc.set; | 722 let writable = !!desc.set; |
719 function lazySetter(value) { | 723 function lazySetter(value) { |
720 defineProperty(to, name, { value: value, writable: writable }); | 724 defineProperty(to, name, { value: value, writable: writable }); |
721 } | 725 } |
722 function lazyGetter() { | 726 function lazyGetter() { |
723 // Clear the init function to detect circular initialization. | 727 // Clear the init function to detect circular initialization. |
724 let f = init; | 728 let f = init; |
725 if (f === null) throw 'circular initialization for field ' + name; | 729 if (f === null) throwRuntimeError('circular initialization for field ' + n
ame); |
726 init = null; | 730 init = null; |
727 | 731 |
728 // Compute and store the value. | 732 // Compute and store the value. |
729 let value = f(); | 733 let value = f(); |
730 lazySetter(value); | 734 lazySetter(value); |
731 return value; | 735 return value; |
732 } | 736 } |
733 desc.get = lazyGetter; | 737 desc.get = lazyGetter; |
734 desc.configurable = true; | 738 desc.configurable = true; |
735 if (writable) desc.set = lazySetter; | 739 if (writable) desc.set = lazySetter; |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 } else if (typeof values === 'object') { | 940 } else if (typeof values === 'object') { |
937 for (let key of getOwnPropertyNames(values)) { | 941 for (let key of getOwnPropertyNames(values)) { |
938 map.set(key, values[key]); | 942 map.set(key, values[key]); |
939 } | 943 } |
940 } | 944 } |
941 return map; | 945 return map; |
942 } | 946 } |
943 dart.map = map; | 947 dart.map = map; |
944 | 948 |
945 function assert(condition) { | 949 function assert(condition) { |
946 // TODO(jmesserly): throw assertion error. | 950 if (!condition) throw new core.AssertionError(); |
947 if (!condition) throw 'assertion failed'; | |
948 } | 951 } |
949 dart.assert = assert; | 952 dart.assert = assert; |
950 | 953 |
951 function throw_(obj) { throw obj; } | 954 function throw_(obj) { throw obj; } |
952 dart.throw_ = throw_; | 955 dart.throw_ = throw_; |
953 | 956 |
954 /** | 957 /** |
955 * Given a class and an initializer method name, creates a constructor | 958 * Given a class and an initializer method name, creates a constructor |
956 * function with the same name. For example `new SomeClass.name(args)`. | 959 * function with the same name. For example `new SomeClass.name(args)`. |
957 */ | 960 */ |
(...skipping 13 matching lines...) Expand all Loading... |
971 } | 974 } |
972 dart.stackTrace = stackTrace; | 975 dart.stackTrace = stackTrace; |
973 | 976 |
974 /** The Symbol for storing type arguments on a specialized generic type. */ | 977 /** The Symbol for storing type arguments on a specialized generic type. */ |
975 dart.typeArguments = Symbol('typeArguments'); | 978 dart.typeArguments = Symbol('typeArguments'); |
976 dart.originalDeclaration = Symbol('originalDeclaration'); | 979 dart.originalDeclaration = Symbol('originalDeclaration'); |
977 | 980 |
978 /** Memoize a generic type constructor function. */ | 981 /** Memoize a generic type constructor function. */ |
979 function generic(typeConstructor) { | 982 function generic(typeConstructor) { |
980 let length = typeConstructor.length; | 983 let length = typeConstructor.length; |
981 if (length < 1) throw Error('must have at least one generic type argument'); | 984 if (length < 1) throwRuntimeError('must have at least one generic type argum
ent'); |
982 | 985 |
983 let resultMap = new Map(); | 986 let resultMap = new Map(); |
984 function makeGenericType(/*...arguments*/) { | 987 function makeGenericType(/*...arguments*/) { |
985 if (arguments.length != length && arguments.length != 0) { | 988 if (arguments.length != length && arguments.length != 0) { |
986 throw Error('requires ' + length + ' or 0 type arguments'); | 989 throwRuntimeError('requires ' + length + ' or 0 type arguments'); |
987 } | 990 } |
988 let args = slice.call(arguments); | 991 let args = slice.call(arguments); |
989 // TODO(leafp): This should really be core.Object for | 992 // TODO(leafp): This should really be core.Object for |
990 // consistency, but Object is not attached to core | 993 // consistency, but Object is not attached to core |
991 // until the entire core library has been processed, | 994 // until the entire core library has been processed, |
992 // which is too late. | 995 // which is too late. |
993 while (args.length < length) args.push(dart.dynamic); | 996 while (args.length < length) args.push(dart.dynamic); |
994 | 997 |
995 let value = resultMap; | 998 let value = resultMap; |
996 for (let i = 0; i < length; i++) { | 999 for (let i = 0; i < length; i++) { |
997 let arg = args[i]; | 1000 let arg = args[i]; |
998 if (arg == null) { | 1001 if (arg == null) { |
999 throw Error('type arguments should not be null: ' + typeConstructor); | 1002 throwRuntimeError('type arguments should not be null: ' + typeConstruc
tor); |
1000 } | 1003 } |
1001 let map = value; | 1004 let map = value; |
1002 value = map.get(arg); | 1005 value = map.get(arg); |
1003 if (value === void 0) { | 1006 if (value === void 0) { |
1004 if (i + 1 == length) { | 1007 if (i + 1 == length) { |
1005 value = typeConstructor.apply(null, args); | 1008 value = typeConstructor.apply(null, args); |
1006 // Save the type constructor and arguments for reflection. | 1009 // Save the type constructor and arguments for reflection. |
1007 if (value) { | 1010 if (value) { |
1008 value[dart.typeArguments] = args; | 1011 value[dart.typeArguments] = args; |
1009 value[dart.originalDeclaration] = makeGenericType; | 1012 value[dart.originalDeclaration] = makeGenericType; |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1272 | 1275 |
1273 // TODO(jmesserly): right now this is a sentinel. It should be a type object | 1276 // TODO(jmesserly): right now this is a sentinel. It should be a type object |
1274 // of some sort, assuming we keep around `dynamic` at runtime. | 1277 // of some sort, assuming we keep around `dynamic` at runtime. |
1275 dart.dynamic = { toString() { return 'dynamic'; }, get name() {return toString
();}}; | 1278 dart.dynamic = { toString() { return 'dynamic'; }, get name() {return toString
();}}; |
1276 dart.void = { toString() { return 'void'; }, get name() {return toString();}}; | 1279 dart.void = { toString() { return 'void'; }, get name() {return toString();}}; |
1277 dart.bottom = { toString() { return 'bottom'; }, get name() {return toString()
;}}; | 1280 dart.bottom = { toString() { return 'bottom'; }, get name() {return toString()
;}}; |
1278 | 1281 |
1279 dart.global = window || global; | 1282 dart.global = window || global; |
1280 dart.JsSymbol = Symbol; | 1283 dart.JsSymbol = Symbol; |
1281 | 1284 |
1282 function import_(value) { | 1285 // All libraries, including those that have referenced (lazyImport), |
| 1286 // but not yet loaded. |
| 1287 var libraries = new Map(); |
| 1288 |
| 1289 // Completed libraries. |
| 1290 var loadedLibraries = new Set(); |
| 1291 |
| 1292 // Import a library by name. |
| 1293 // This is exported for REPL / JS interop convenience. |
| 1294 function import_(name) { |
| 1295 let loaded = loadedLibraries.has(name); |
| 1296 let value = libraries[name]; |
1283 // TODO(vsm): Change this to a hard throw. | 1297 // TODO(vsm): Change this to a hard throw. |
1284 // For now, we're missing some libraries. E.g., dart:js: | 1298 // For now, we're missing some libraries. E.g., dart:js: |
1285 // https://github.com/dart-lang/dev_compiler/issues/168 | 1299 // https://github.com/dart-lang/dev_compiler/issues/168 |
1286 if (!value) { | 1300 if (!loaded) { |
1287 console.log('missing required module'); | 1301 console.warn('Missing required module: ' + name); |
| 1302 } else if (!value) { |
| 1303 throwRuntimeError('Library import error: ' + name) |
1288 } | 1304 } |
1289 return value; | 1305 return value; |
1290 } | 1306 } |
1291 dart.import = import_; | 1307 dart.import = import_; |
1292 | 1308 |
1293 function lazyImport(value) { | 1309 function initializeLibraryStub(name) { |
1294 return defineLibrary(value, {}); | 1310 // Create the library object if necessary. |
| 1311 if (!libraries[name]) { |
| 1312 libraries[name] = {}; |
| 1313 } |
| 1314 return libraries[name]; |
1295 } | 1315 } |
1296 dart.lazyImport = lazyImport; | 1316 const lazyImport = initializeLibraryStub; |
1297 | 1317 |
1298 function defineLibrary(value, defaultValue) { | 1318 function defineLibrary(name, defaultValue) { |
1299 return value ? value : defaultValue; | 1319 if (loadedLibraries.has(name)) { |
| 1320 throwRuntimeError('Library is already defined: ' + name); |
| 1321 } |
| 1322 var value; |
| 1323 if (defaultValue) { |
| 1324 var oldValue = libraries[name]; |
| 1325 if (oldValue && oldValue != defaultValue) { |
| 1326 throwRuntimeError( |
| 1327 `Library ${name} cannot be redefined to ${defaultValue}`); |
| 1328 } |
| 1329 libraries[name] = value = defaultValue; |
| 1330 } else { |
| 1331 value = initializeLibraryStub(name); |
| 1332 } |
| 1333 loadedLibraries.add(name); |
| 1334 return value; |
1300 } | 1335 } |
1301 dart.defineLibrary = defineLibrary; | |
1302 | 1336 |
1303 // TODO(jmesserly): hack to bootstrap the SDK | 1337 function library(name, defaultValue, imports, lazyImports, module) { |
1304 _js_helper = _js_helper || {}; | 1338 var args = []; |
| 1339 var lib = defineLibrary(name, defaultValue); |
| 1340 args.push(lib); |
| 1341 for (var i = 0; i < imports.length; ++i) { |
| 1342 lib = import_(imports[i]); |
| 1343 args.push(lib); |
| 1344 } |
| 1345 for (var i = 0; i < lazyImports.length; ++i) { |
| 1346 lib = lazyImport(lazyImports[i]); |
| 1347 args.push(lib); |
| 1348 } |
| 1349 module.apply(null, args); |
| 1350 } |
| 1351 dart.library = library; |
| 1352 |
| 1353 function start(libraryName) { |
| 1354 let lib = import_(libraryName); |
| 1355 _isolate_helper.startRootIsolate(lib.main, []); |
| 1356 } |
| 1357 dart.start = start; |
| 1358 |
| 1359 let core = lazyImport('dart/core'); |
| 1360 let collection = lazyImport('dart/collection'); |
| 1361 let async = lazyImport('dart/async'); |
| 1362 let _interceptors = lazyImport('dart/_interceptors'); |
| 1363 let _isolate_helper = lazyImport('dart/_isolate_helper'); |
| 1364 let _js_helper = lazyImport('dart/_js_helper'); |
1305 _js_helper.checkNum = notNull; | 1365 _js_helper.checkNum = notNull; |
1306 | 1366 let _js_primitives = lazyImport('dart/_js_primitives'); |
1307 _js_primitives = _js_primitives || {}; | |
1308 _js_primitives.printString = (s) => console.log(s); | 1367 _js_primitives.printString = (s) => console.log(s); |
1309 | 1368 |
1310 // TODO(vsm): DOM facades? | 1369 // TODO(vsm): DOM facades? |
1311 // See: https://github.com/dart-lang/dev_compiler/issues/173 | 1370 // See: https://github.com/dart-lang/dev_compiler/issues/173 |
1312 NodeList.prototype.get = function(i) { return this[i]; }; | 1371 NodeList.prototype.get = function(i) { return this[i]; }; |
1313 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | 1372 NamedNodeMap.prototype.get = function(i) { return this[i]; }; |
1314 DOMTokenList.prototype.get = function(i) { return this[i]; }; | 1373 DOMTokenList.prototype.get = function(i) { return this[i]; }; |
1315 | 1374 |
1316 /** Dart extension members. */ | 1375 /** Dart extension members. */ |
1317 dartx = dartx || {}; | 1376 dartx = dartx || {}; |
1318 | 1377 |
1319 })(dart || (dart = {})); | 1378 })(dart || (dart = {})); |
OLD | NEW |