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; |
| 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 var libraries = {}; |
|
Jennifer Messerly
2015/06/05 22:45:57
use a Map here?
vsm
2015/06/05 23:03:11
Done.
| |
| 1286 var loadedLibraries = new Set(); | |
|
Jennifer Messerly
2015/06/05 22:45:57
I think the reason this exists is to track librari
vsm
2015/06/05 23:03:11
Done.
| |
| 1287 | |
| 1288 function import_(name) { | |
| 1289 let loaded = loadedLibraries.has(name); | |
| 1290 let value = libraries[name]; | |
| 1283 // TODO(vsm): Change this to a hard throw. | 1291 // TODO(vsm): Change this to a hard throw. |
| 1284 // For now, we're missing some libraries. E.g., dart:js: | 1292 // For now, we're missing some libraries. E.g., dart:js: |
| 1285 // https://github.com/dart-lang/dev_compiler/issues/168 | 1293 // https://github.com/dart-lang/dev_compiler/issues/168 |
| 1286 if (!value) { | 1294 if (!loaded) { |
| 1287 console.log('missing required module'); | 1295 console.warn('Missing required module: ' + name); |
| 1296 } else if (!value) { | |
| 1297 throwRuntimeError('Library import error: ' + name) | |
| 1288 } | 1298 } |
| 1289 return value; | 1299 return value; |
| 1290 } | 1300 } |
| 1291 dart.import = import_; | 1301 dart.import = import_; |
|
Jennifer Messerly
2015/06/05 22:45:57
This is public for interop/REPL purposes? maybe wo
vsm
2015/06/05 23:03:11
Done.
| |
| 1302 | |
| 1303 function initializeLibraryStub(name) { | |
| 1304 // Create the library object if necessary. | |
| 1305 if (!libraries[name]) { | |
| 1306 libraries[name] = {}; | |
| 1307 } | |
| 1308 return libraries[name]; | |
| 1309 } | |
| 1292 | 1310 |
| 1293 function lazyImport(value) { | 1311 function lazyImport(name) { |
|
Jennifer Messerly
2015/06/05 22:45:57
maybe we don't need this? just rename the other on
vsm
2015/06/05 23:03:11
Done.
| |
| 1294 return defineLibrary(value, {}); | 1312 return initializeLibraryStub(name); |
| 1295 } | 1313 } |
| 1296 dart.lazyImport = lazyImport; | |
| 1297 | 1314 |
| 1298 function defineLibrary(value, defaultValue) { | 1315 function defineLibrary(name, defaultValue) { |
| 1299 return value ? value : defaultValue; | 1316 if (loadedLibraries.has(name)) { |
| 1317 throwRuntimeError('Library is already defined: ' + name); | |
| 1318 } | |
| 1319 var value; | |
| 1320 if (defaultValue) { | |
| 1321 var oldValue = libraries[name]; | |
| 1322 if (oldValue && oldValue != defaultValue) { | |
| 1323 throwRuntimeError( | |
| 1324 'Library ${name} cannot be redefined to ${defaultValue}'); | |
|
Jennifer Messerly
2015/06/05 22:45:57
backticks here?
vsm
2015/06/05 23:03:11
Done.
| |
| 1325 } | |
| 1326 libraries[name] = value = defaultValue; | |
| 1327 } else { | |
| 1328 value = initializeLibraryStub(name); | |
| 1329 } | |
| 1330 loadedLibraries.add(name); | |
| 1331 return value; | |
| 1300 } | 1332 } |
| 1301 dart.defineLibrary = defineLibrary; | 1333 |
| 1334 function library(name, defaultValue, imports, lazyImports, module) { | |
|
Jennifer Messerly
2015/06/05 22:45:57
for defaultValue, move last? since it's rarely sup
| |
| 1335 var args = []; | |
| 1336 var lib = defineLibrary(name, defaultValue); | |
| 1337 args.push(lib); | |
| 1338 for (var i = 0; i < imports.length; ++i) { | |
| 1339 lib = import_(imports[i]); | |
| 1340 args.push(lib); | |
| 1341 } | |
| 1342 for (var i = 0; i < lazyImports.length; ++i) { | |
| 1343 lib = lazyImport(lazyImports[i]); | |
| 1344 args.push(lib); | |
| 1345 } | |
| 1346 module.apply(dart.global, args); | |
|
Jennifer Messerly
2015/06/05 22:45:57
couple of thoughts here:
Maybe pass "null" instea
vsm
2015/06/05 23:03:11
Done.
I'll tackle ordering (lazy or eager) in a s
| |
| 1347 } | |
| 1348 dart.library = library; | |
| 1349 | |
| 1350 function start(libraryName) { | |
| 1351 let lib = import_(libraryName); | |
| 1352 _isolate_helper.startRootIsolate(lib.main, []); | |
| 1353 } | |
| 1354 dart.start = start; | |
| 1302 | 1355 |
| 1303 // TODO(jmesserly): hack to bootstrap the SDK | 1356 // TODO(jmesserly): hack to bootstrap the SDK |
|
Jennifer Messerly
2015/06/05 22:45:57
is this still needed?
vsm
2015/06/05 23:03:10
Done.
| |
| 1304 _js_helper = _js_helper || {}; | 1357 let core = lazyImport('dart/core.js'); |
| 1358 let collection = lazyImport('dart/collection.js'); | |
| 1359 let async = lazyImport('dart/async.js'); | |
| 1360 let _interceptors = lazyImport('dart/_interceptors.js'); | |
| 1361 let _isolate_helper = lazyImport('dart/_isolate_helper.js'); | |
| 1362 let _js_helper = lazyImport('dart/_js_helper.js'); | |
| 1305 _js_helper.checkNum = notNull; | 1363 _js_helper.checkNum = notNull; |
| 1306 | 1364 let _js_primitives = lazyImport('dart/_js_primitives.js'); |
| 1307 _js_primitives = _js_primitives || {}; | |
| 1308 _js_primitives.printString = (s) => console.log(s); | 1365 _js_primitives.printString = (s) => console.log(s); |
| 1309 | 1366 |
| 1310 // TODO(vsm): DOM facades? | 1367 // TODO(vsm): DOM facades? |
| 1311 // See: https://github.com/dart-lang/dev_compiler/issues/173 | 1368 // See: https://github.com/dart-lang/dev_compiler/issues/173 |
| 1312 NodeList.prototype.get = function(i) { return this[i]; }; | 1369 NodeList.prototype.get = function(i) { return this[i]; }; |
| 1313 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | 1370 NamedNodeMap.prototype.get = function(i) { return this[i]; }; |
| 1314 DOMTokenList.prototype.get = function(i) { return this[i]; }; | 1371 DOMTokenList.prototype.get = function(i) { return this[i]; }; |
| 1315 | 1372 |
| 1316 /** Dart extension members. */ | 1373 /** Dart extension members. */ |
| 1317 dartx = dartx || {}; | 1374 dartx = dartx || {}; |
| 1318 | 1375 |
| 1319 })(dart || (dart = {})); | 1376 })(dart || (dart = {})); |
| OLD | NEW |