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 let defineProperty = Object.defineProperty; | 9 let defineProperty = Object.defineProperty; |
10 let getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | 10 let getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 let result; | 203 let result; |
204 if (map) { | 204 if (map) { |
205 result = map.get(t2); | 205 result = map.get(t2); |
206 if (result !== void 0) return result; | 206 if (result !== void 0) return result; |
207 } else { | 207 } else { |
208 subtypeMap.set(t1, map = new Map()); | 208 subtypeMap.set(t1, map = new Map()); |
209 } | 209 } |
210 if (t2 == core.Type) { | 210 if (t2 == core.Type) { |
211 // Special case Types. | 211 // Special case Types. |
212 result = t1.prototype instanceof core.Type || | 212 result = t1.prototype instanceof core.Type || |
213 t1 instanceof AbstractFunctionType || | 213 t1 instanceof FunctionType || |
214 isSubtype_(t1, t2); | 214 isSubtype_(t1, t2); |
215 } else { | 215 } else { |
216 result = isSubtype_(t1, t2) | 216 result = isSubtype_(t1, t2) |
217 } | 217 } |
218 map.set(t2, result); | 218 map.set(t2, result); |
219 return result; | 219 return result; |
220 } | 220 } |
221 dart.isSubtype = isSubtype; | 221 dart.isSubtype = isSubtype; |
222 | 222 |
223 function _isBottom(type, dynamicIsBottom) { | 223 function _isBottom(type, dynamicIsBottom) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 | 332 |
333 return false; | 333 return false; |
334 } | 334 } |
335 | 335 |
336 // TODO(jmesserly): this isn't currently used, but it could be if we want | 336 // TODO(jmesserly): this isn't currently used, but it could be if we want |
337 // `obj is NonGroundType<T,S>` to be rejected at runtime instead of compile | 337 // `obj is NonGroundType<T,S>` to be rejected at runtime instead of compile |
338 // time. | 338 // time. |
339 function isGroundType(type) { | 339 function isGroundType(type) { |
340 // TODO(vsm): Cache this if we start using it at runtime. | 340 // TODO(vsm): Cache this if we start using it at runtime. |
341 | 341 |
342 if (type instanceof AbstractFunctionType) { | 342 if (type instanceof FunctionType) { |
343 if (!_isTop(type.returnType, false)) return false; | 343 if (!_isTop(type.returnType, false)) return false; |
344 for (let i = 0; i < type.args.length; ++i) { | 344 for (let i = 0; i < type.args.length; ++i) { |
345 if (!_isBottom(type.args[i], true)) return false; | 345 if (!_isBottom(type.args[i], true)) return false; |
346 } | 346 } |
347 for (let i = 0; i < type.optionals.length; ++i) { | 347 for (let i = 0; i < type.optionals.length; ++i) { |
348 if (!_isBottom(type.optionals[i], true)) return false; | 348 if (!_isBottom(type.optionals[i], true)) return false; |
349 } | 349 } |
350 var names = getOwnPropertyNames(type.named); | 350 var names = getOwnPropertyNames(type.named); |
351 for (let i = 0; i < names.length; ++i) { | 351 for (let i = 0; i < names.length; ++i) { |
352 if (!_isBottom(type.named[names[i]], true)) return false; | 352 if (!_isBottom(type.named[names[i]], true)) return false; |
(...skipping 30 matching lines...) Expand all Loading... | |
383 return x; | 383 return x; |
384 } | 384 } |
385 dart.notNull = notNull; | 385 dart.notNull = notNull; |
386 | 386 |
387 function _typeName(type) { | 387 function _typeName(type) { |
388 var name = type.name; | 388 var name = type.name; |
389 if (!name) throw 'Unexpected type: ' + type; | 389 if (!name) throw 'Unexpected type: ' + type; |
390 return name; | 390 return name; |
391 } | 391 } |
392 | 392 |
393 class AbstractFunctionType { | 393 // TODO(jmesserly): extends Type? |
394 constructor() { | 394 class FunctionType { |
395 this._stringValue = null; | 395 constructor(returnType, args, optionals, named, opt_typedefName) { |
396 this.returnType = returnType; | |
397 this.args = args; | |
398 this.optionals = optionals; | |
399 this.named = named; | |
400 this._stringValue = opt_typedefName; | |
Jennifer Messerly
2015/05/12 16:23:45
I merged the two subtypes, since the only differen
| |
396 } | 401 } |
397 | 402 |
398 get name() { | 403 get name() { |
399 if (this._stringValue) return this._stringValue; | 404 if (this._stringValue) return this._stringValue; |
400 | 405 |
401 var buffer = '('; | 406 var buffer = '('; |
402 for (let i = 0; i < this.args.length; ++i) { | 407 for (let i = 0; i < this.args.length; ++i) { |
403 if (i > 0) { | 408 if (i > 0) { |
404 buffer += ', '; | 409 buffer += ', '; |
405 } | 410 } |
(...skipping 21 matching lines...) Expand all Loading... | |
427 } | 432 } |
428 buffer += '}'; | 433 buffer += '}'; |
429 } | 434 } |
430 | 435 |
431 buffer += ') -> ' + _typeName(this.returnType); | 436 buffer += ') -> ' + _typeName(this.returnType); |
432 this._stringValue = buffer; | 437 this._stringValue = buffer; |
433 return buffer; | 438 return buffer; |
434 } | 439 } |
435 } | 440 } |
436 | 441 |
437 class FunctionType extends AbstractFunctionType { | |
438 constructor(returnType, args, optionals, named) { | |
439 super(); | |
440 this.returnType = returnType; | |
441 this.args = args; | |
442 this.optionals = optionals; | |
443 this.named = named; | |
444 } | |
445 } | |
446 | |
447 function functionType(returnType, args, extra) { | 442 function functionType(returnType, args, extra) { |
448 // TODO(vsm): Cache / memomize? | 443 // TODO(vsm): Cache / memomize? |
449 var optionals; | 444 var optionals; |
450 var named; | 445 var named; |
451 if (extra === void 0) { | 446 if (extra === void 0) { |
452 optionals = []; | 447 optionals = []; |
453 named = {}; | 448 named = {}; |
454 } else if (extra instanceof Array) { | 449 } else if (extra instanceof Array) { |
455 optionals = extra; | 450 optionals = extra; |
456 named = {}; | 451 named = {}; |
457 } else { | 452 } else { |
458 optionals = []; | 453 optionals = []; |
459 named = extra; | 454 named = extra; |
460 } | 455 } |
461 return new FunctionType(returnType, args, optionals, named); | 456 return new FunctionType(returnType, args, optionals, named); |
462 } | 457 } |
463 dart.functionType = functionType; | 458 dart.functionType = functionType; |
464 | 459 |
465 class Typedef extends AbstractFunctionType { | 460 function typedef(name, functionType) { |
466 constructor(name, closure) { | 461 let f = functionType; |
467 super(); | 462 return new FunctionType(f.returnType, f.args, f.optionals, f.named, name); |
468 this._name = name; | |
469 this._closure = closure; | |
470 this._functionType = null; | |
471 } | |
472 | |
473 get name() { | |
474 return this._name; | |
475 } | |
476 | |
477 get functionType() { | |
478 if (!this._functionType) { | |
479 this._functionType = this._closure(); | |
480 } | |
481 return this._functionType; | |
482 } | |
483 | |
484 get returnType() { | |
485 return this.functionType.returnType; | |
486 } | |
487 | |
488 get args() { | |
489 return this.functionType.args; | |
490 } | |
491 | |
492 get optionals() { | |
493 return this.functionType.optionals; | |
494 } | |
495 | |
496 get named() { | |
497 return this.functionType.named; | |
498 } | |
499 } | |
500 | |
501 function typedef(name, closure) { | |
502 return new Typedef(name, closure); | |
503 } | 463 } |
504 dart.typedef = typedef; | 464 dart.typedef = typedef; |
505 | 465 |
506 function isFunctionType(type) { | 466 function isFunctionType(type) { |
507 return isClassSubType(type, core.Function) || type instanceof AbstractFuncti onType; | 467 return isClassSubType(type, core.Function) || type instanceof FunctionType; |
508 } | 468 } |
509 | 469 |
510 function getFunctionType(obj) { | 470 function getFunctionType(obj) { |
511 // TODO(vsm): Encode this properly on the function for Dart-generated code. | 471 // TODO(vsm): Encode this properly on the function for Dart-generated code. |
512 var args = Array.apply(null, new Array(obj.length)).map(function(){return co re.Object}); | 472 var args = Array.apply(null, new Array(obj.length)).map(function(){return co re.Object}); |
513 return functionType(dart.bottom, args); | 473 return functionType(dart.bottom, args); |
514 } | 474 } |
515 | 475 |
516 function isFunctionSubType(ft1, ft2) { | 476 function isFunctionSubType(ft1, ft2) { |
517 if (ft2 == core.Function) { | 477 if (ft2 == core.Function) { |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
985 dart.defineLibrary = defineLibrary; | 945 dart.defineLibrary = defineLibrary; |
986 | 946 |
987 // TODO(jmesserly): hack to bootstrap the SDK | 947 // TODO(jmesserly): hack to bootstrap the SDK |
988 _js_helper = _js_helper || {}; | 948 _js_helper = _js_helper || {}; |
989 _js_helper.checkNum = notNull; | 949 _js_helper.checkNum = notNull; |
990 | 950 |
991 _js_primitives = _js_primitives || {}; | 951 _js_primitives = _js_primitives || {}; |
992 _js_primitives.printString = (s) => console.log(s); | 952 _js_primitives.printString = (s) => console.log(s); |
993 | 953 |
994 })(dart || (dart = {})); | 954 })(dart || (dart = {})); |
OLD | NEW |