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; | 5 var dart, _js_helper, _js_primitives; |
| 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); | 46 field = _canonicalFieldName(obj, 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 console.error(message); | |
|
Jennifer Messerly
2015/06/05 16:22:27
what's the benefit of logging and throwing? Don't
vsm
2015/06/05 17:01:04
removed
| |
| 55 throw new _js_helper.RuntimeError(message); | |
|
Jennifer Messerly
2015/06/05 16:22:27
what happens if this isn't loaded? don't we have b
vsm
2015/06/05 17:01:04
Done.
| |
| 56 } | |
| 57 | |
| 53 // TODO(jmesserly): this should call noSuchMethod, not throw. | 58 // TODO(jmesserly): this should call noSuchMethod, not throw. |
| 54 function throwNoSuchMethod(obj, name, args, opt_func) { | 59 function throwNoSuchMethod(obj, name, args, opt_func) { |
| 55 if (obj === void 0) obj = opt_func; | 60 if (obj === void 0) obj = opt_func; |
| 56 throw new core.NoSuchMethodError(obj, name, args); | 61 throw new core.NoSuchMethodError(obj, name, args); |
| 57 } | 62 } |
| 58 | 63 |
| 59 function checkAndCall(f, ftype, obj, args, name) { | 64 function checkAndCall(f, ftype, obj, args, name) { |
| 60 if (!(f instanceof Function)) { | 65 if (!(f instanceof Function)) { |
| 61 // We're not a function (and hence not a method either) | 66 // We're not a function (and hence not a method either) |
| 62 // Grab the `call` method if it's not a function. | 67 // Grab the `call` method if it's not a function. |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 424 | 429 |
| 425 function equals(x, y) { | 430 function equals(x, y) { |
| 426 if (x == null || y == null) return x == y; | 431 if (x == null || y == null) return x == y; |
| 427 let eq = x['==']; | 432 let eq = x['==']; |
| 428 return eq ? eq.call(x, y) : x == y; | 433 return eq ? eq.call(x, y) : x == y; |
| 429 } | 434 } |
| 430 dart.equals = equals; | 435 dart.equals = equals; |
| 431 | 436 |
| 432 /** Checks that `x` is not null or undefined. */ | 437 /** Checks that `x` is not null or undefined. */ |
| 433 function notNull(x) { | 438 function notNull(x) { |
| 434 if (x == null) throw 'expected not-null value'; | 439 if (x == null) throwRuntimeError('expected not-null value'); |
| 435 return x; | 440 return x; |
| 436 } | 441 } |
| 437 dart.notNull = notNull; | 442 dart.notNull = notNull; |
| 438 | 443 |
| 439 function _typeName(type) { | 444 function _typeName(type) { |
| 440 if (type === void 0) throw "Undefined type"; | 445 if (type === void 0) throwRuntimeError('Undefined type'); |
| 441 var name = type.name; | 446 var name = type.name; |
| 442 if (!name) throw 'Unexpected type: ' + type; | 447 if (!name) throwRuntimeError('Unexpected type: ' + type); |
| 443 return name; | 448 return name; |
| 444 } | 449 } |
| 445 | 450 |
| 446 class AbstractFunctionType { | 451 class AbstractFunctionType { |
| 447 constructor() { | 452 constructor() { |
| 448 this._stringValue = null; | 453 this._stringValue = null; |
| 449 } | 454 } |
| 450 | 455 |
| 451 /// Check that a function of this type can be applied to | 456 /// Check that a function of this type can be applied to |
| 452 /// actuals. | 457 /// actuals. |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 720 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). | 725 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). |
| 721 function defineLazyProperty(to, name, desc) { | 726 function defineLazyProperty(to, name, desc) { |
| 722 let init = desc.get; | 727 let init = desc.get; |
| 723 let writable = !!desc.set; | 728 let writable = !!desc.set; |
| 724 function lazySetter(value) { | 729 function lazySetter(value) { |
| 725 defineProperty(to, name, { value: value, writable: writable }); | 730 defineProperty(to, name, { value: value, writable: writable }); |
| 726 } | 731 } |
| 727 function lazyGetter() { | 732 function lazyGetter() { |
| 728 // Clear the init function to detect circular initialization. | 733 // Clear the init function to detect circular initialization. |
| 729 let f = init; | 734 let f = init; |
| 730 if (f === null) throw 'circular initialization for field ' + name; | 735 if (f === null) throwRuntimeError('circular initialization for field ' + n ame); |
| 731 init = null; | 736 init = null; |
| 732 | 737 |
| 733 // Compute and store the value. | 738 // Compute and store the value. |
| 734 let value = f(); | 739 let value = f(); |
| 735 lazySetter(value); | 740 lazySetter(value); |
| 736 return value; | 741 return value; |
| 737 } | 742 } |
| 738 desc.get = lazyGetter; | 743 desc.get = lazyGetter; |
| 739 desc.configurable = true; | 744 desc.configurable = true; |
| 740 if (writable) desc.set = lazySetter; | 745 if (writable) desc.set = lazySetter; |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 899 } else if (typeof values === 'object') { | 904 } else if (typeof values === 'object') { |
| 900 for (let key of getOwnPropertyNames(values)) { | 905 for (let key of getOwnPropertyNames(values)) { |
| 901 map.set(key, values[key]); | 906 map.set(key, values[key]); |
| 902 } | 907 } |
| 903 } | 908 } |
| 904 return map; | 909 return map; |
| 905 } | 910 } |
| 906 dart.map = map; | 911 dart.map = map; |
| 907 | 912 |
| 908 function assert(condition) { | 913 function assert(condition) { |
| 909 // TODO(jmesserly): throw assertion error. | 914 if (!condition) throw new core.AssertionError(); |
| 910 if (!condition) throw 'assertion failed'; | |
| 911 } | 915 } |
| 912 dart.assert = assert; | 916 dart.assert = assert; |
| 913 | 917 |
| 914 function throw_(obj) { throw obj; } | 918 function throw_(obj) { throw obj; } |
| 915 dart.throw_ = throw_; | 919 dart.throw_ = throw_; |
| 916 | 920 |
| 917 /** | 921 /** |
| 918 * Given a class and an initializer method name, creates a constructor | 922 * Given a class and an initializer method name, creates a constructor |
| 919 * function with the same name. For example `new SomeClass.name(args)`. | 923 * function with the same name. For example `new SomeClass.name(args)`. |
| 920 */ | 924 */ |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 934 } | 938 } |
| 935 dart.stackTrace = stackTrace; | 939 dart.stackTrace = stackTrace; |
| 936 | 940 |
| 937 /** The Symbol for storing type arguments on a specialized generic type. */ | 941 /** The Symbol for storing type arguments on a specialized generic type. */ |
| 938 dart.typeArguments = Symbol('typeArguments'); | 942 dart.typeArguments = Symbol('typeArguments'); |
| 939 dart.originalDeclaration = Symbol('originalDeclaration'); | 943 dart.originalDeclaration = Symbol('originalDeclaration'); |
| 940 | 944 |
| 941 /** Memoize a generic type constructor function. */ | 945 /** Memoize a generic type constructor function. */ |
| 942 function generic(typeConstructor) { | 946 function generic(typeConstructor) { |
| 943 let length = typeConstructor.length; | 947 let length = typeConstructor.length; |
| 944 if (length < 1) throw Error('must have at least one generic type argument'); | 948 if (length < 1) throwRuntimeError('must have at least one generic type argum ent'); |
| 945 | 949 |
| 946 let resultMap = new Map(); | 950 let resultMap = new Map(); |
| 947 function makeGenericType(/*...arguments*/) { | 951 function makeGenericType(/*...arguments*/) { |
| 948 if (arguments.length != length && arguments.length != 0) { | 952 if (arguments.length != length && arguments.length != 0) { |
| 949 throw Error('requires ' + length + ' or 0 type arguments'); | 953 throwRuntimeError('requires ' + length + ' or 0 type arguments'); |
| 950 } | 954 } |
| 951 let args = Array.prototype.slice.call(arguments); | 955 let args = Array.prototype.slice.call(arguments); |
| 952 // TODO(leafp): This should really be core.Object for | 956 // TODO(leafp): This should really be core.Object for |
| 953 // consistency, but Object is not attached to core | 957 // consistency, but Object is not attached to core |
| 954 // until the entire core library has been processed, | 958 // until the entire core library has been processed, |
| 955 // which is too late. | 959 // which is too late. |
| 956 while (args.length < length) args.push(dart.dynamic); | 960 while (args.length < length) args.push(dart.dynamic); |
| 957 | 961 |
| 958 let value = resultMap; | 962 let value = resultMap; |
| 959 for (let i = 0; i < length; i++) { | 963 for (let i = 0; i < length; i++) { |
| 960 let arg = args[i]; | 964 let arg = args[i]; |
| 961 if (arg == null) { | 965 if (arg == null) { |
| 962 throw Error('type arguments should not be null: ' + typeConstructor); | 966 throwRuntimeError('type arguments should not be null: ' + typeConstruc tor); |
| 963 } | 967 } |
| 964 let map = value; | 968 let map = value; |
| 965 value = map.get(arg); | 969 value = map.get(arg); |
| 966 if (value === void 0) { | 970 if (value === void 0) { |
| 967 if (i + 1 == length) { | 971 if (i + 1 == length) { |
| 968 value = typeConstructor.apply(null, args); | 972 value = typeConstructor.apply(null, args); |
| 969 // Save the type constructor and arguments for reflection. | 973 // Save the type constructor and arguments for reflection. |
| 970 if (value) { | 974 if (value) { |
| 971 value[dart.typeArguments] = args; | 975 value[dart.typeArguments] = args; |
| 972 value[dart.originalDeclaration] = makeGenericType; | 976 value[dart.originalDeclaration] = makeGenericType; |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1223 | 1227 |
| 1224 // TODO(jmesserly): right now this is a sentinel. It should be a type object | 1228 // TODO(jmesserly): right now this is a sentinel. It should be a type object |
| 1225 // of some sort, assuming we keep around `dynamic` at runtime. | 1229 // of some sort, assuming we keep around `dynamic` at runtime. |
| 1226 dart.dynamic = { toString() { return 'dynamic'; }, get name() {return toString ();}}; | 1230 dart.dynamic = { toString() { return 'dynamic'; }, get name() {return toString ();}}; |
| 1227 dart.void = { toString() { return 'void'; }, get name() {return toString();}}; | 1231 dart.void = { toString() { return 'void'; }, get name() {return toString();}}; |
| 1228 dart.bottom = { toString() { return 'bottom'; }, get name() {return toString() ;}}; | 1232 dart.bottom = { toString() { return 'bottom'; }, get name() {return toString() ;}}; |
| 1229 | 1233 |
| 1230 dart.global = window || global; | 1234 dart.global = window || global; |
| 1231 dart.JsSymbol = Symbol; | 1235 dart.JsSymbol = Symbol; |
| 1232 | 1236 |
| 1233 function import_(value) { | 1237 var loadedLibraries = {}; |
|
Jennifer Messerly
2015/06/05 16:22:27
How will this work with JS interop? It seems we ar
vsm
2015/06/05 17:01:04
Not really assuming a closed world. For JS code,
| |
| 1238 | |
| 1239 function import_(name) { | |
| 1240 let value = loadedLibraries[name]; | |
| 1234 // TODO(vsm): Change this to a hard throw. | 1241 // TODO(vsm): Change this to a hard throw. |
| 1235 // For now, we're missing some libraries. E.g., dart:js: | 1242 // For now, we're missing some libraries. E.g., dart:js: |
| 1236 // https://github.com/dart-lang/dev_compiler/issues/168 | 1243 // https://github.com/dart-lang/dev_compiler/issues/168 |
| 1237 if (!value) { | 1244 if (!value) { |
| 1238 console.log('missing required module'); | 1245 console.warn('Missing required module: ' + name); |
| 1246 } else if (value != dart.global[name]) { | |
| 1247 throwRuntimeError('Library name mismatch: ' + name) | |
| 1239 } | 1248 } |
| 1240 return value; | 1249 return value; |
| 1241 } | 1250 } |
| 1242 dart.import = import_; | 1251 dart.import = import_; |
| 1252 | |
| 1253 function initializeLibraryStub(name) { | |
| 1254 // Create the library object if necessary. | |
| 1255 if (!dart.global[name]) { | |
| 1256 dart.global[name] = {}; | |
| 1257 } | |
| 1258 return dart.global[name]; | |
| 1259 } | |
| 1243 | 1260 |
| 1244 function lazyImport(value) { | 1261 function lazyImport(name) { |
| 1245 return defineLibrary(value, {}); | 1262 return initializeLibraryStub(name); |
| 1246 } | 1263 } |
| 1247 dart.lazyImport = lazyImport; | 1264 dart.lazyImport = lazyImport; |
| 1248 | 1265 |
| 1249 function defineLibrary(value, defaultValue) { | 1266 function defineLibrary(name, defaultValue) { |
| 1250 return value ? value : defaultValue; | 1267 if (loadedLibraries[name]) { |
| 1268 throwRuntimeError('Library is already defined: ' + name); | |
| 1269 } | |
| 1270 var value; | |
| 1271 if (defaultValue) { | |
| 1272 var oldValue = dart.global[name]; | |
| 1273 if (oldValue && oldValue != defaultValue) { | |
| 1274 throwRuntimeError('Library ' + name + ' cannot be redefined to ' + defau ltValue); | |
|
Jennifer Messerly
2015/06/05 16:22:27
long line.
also could use interpolation:
`Library
vsm
2015/06/05 17:01:04
Done.
| |
| 1275 } | |
| 1276 dart.global[name] = value = defaultValue; | |
| 1277 } else { | |
| 1278 value = initializeLibraryStub(name); | |
| 1279 } | |
| 1280 loadedLibraries[name] = value; | |
| 1281 return value; | |
| 1251 } | 1282 } |
| 1252 dart.defineLibrary = defineLibrary; | 1283 dart.defineLibrary = defineLibrary; |
| 1253 | 1284 |
| 1254 // TODO(jmesserly): hack to bootstrap the SDK | 1285 // TODO(jmesserly): hack to bootstrap the SDK |
| 1255 _js_helper = _js_helper || {}; | 1286 _js_helper = _js_helper || {}; |
| 1256 _js_helper.checkNum = notNull; | 1287 _js_helper.checkNum = notNull; |
| 1257 | 1288 |
| 1258 _js_primitives = _js_primitives || {}; | 1289 _js_primitives = _js_primitives || {}; |
| 1259 _js_primitives.printString = (s) => console.log(s); | 1290 _js_primitives.printString = (s) => console.log(s); |
| 1260 | 1291 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1292 Number.prototype['>'] = function(arg) { return this.valueOf() > arg; }; | 1323 Number.prototype['>'] = function(arg) { return this.valueOf() > arg; }; |
| 1293 Number.prototype['+'] = function(arg) { return this.valueOf() + arg; }; | 1324 Number.prototype['+'] = function(arg) { return this.valueOf() + arg; }; |
| 1294 | 1325 |
| 1295 // TODO(vsm): DOM facades? | 1326 // TODO(vsm): DOM facades? |
| 1296 // See: https://github.com/dart-lang/dev_compiler/issues/173 | 1327 // See: https://github.com/dart-lang/dev_compiler/issues/173 |
| 1297 NodeList.prototype.get = function(i) { return this[i]; }; | 1328 NodeList.prototype.get = function(i) { return this[i]; }; |
| 1298 NamedNodeMap.prototype.get = function(i) { return this[i]; }; | 1329 NamedNodeMap.prototype.get = function(i) { return this[i]; }; |
| 1299 DOMTokenList.prototype.get = function(i) { return this[i]; }; | 1330 DOMTokenList.prototype.get = function(i) { return this[i]; }; |
| 1300 | 1331 |
| 1301 })(dart || (dart = {})); | 1332 })(dart || (dart = {})); |
| OLD | NEW |