| 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; | 5 var dart, _js_helper; |
| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 78 dart.dcall = dcall; | 78 dart.dcall = dcall; |
| 79 | 79 |
| 80 function dsend(obj, method/*, ...args*/) { | 80 function dsend(obj, method/*, ...args*/) { |
| 81 let args = Array.prototype.slice.call(arguments, 2); | 81 let args = Array.prototype.slice.call(arguments, 2); |
| 82 return checkAndCall(obj[method], obj, args, method); | 82 return checkAndCall(obj[method], obj, args, method); |
| 83 } | 83 } |
| 84 dart.dsend = dsend; | 84 dart.dsend = dsend; |
| 85 | 85 |
| 86 function dindex(obj, index) { | 86 function dindex(obj, index) { |
| 87 // TODO(jmesserly): remove this special case once Array extensions are |
| 88 // hooked up. |
| 89 if (obj instanceof Array && getRuntimeType(index) == core.int) { |
| 90 return obj[index]; |
| 91 } |
| 87 return checkAndCall(obj.get, obj, [index], '[]'); | 92 return checkAndCall(obj.get, obj, [index], '[]'); |
| 88 } | 93 } |
| 89 dart.dindex = dindex; | 94 dart.dindex = dindex; |
| 90 | 95 |
| 91 function dsetindex(obj, index, value) { | 96 function dsetindex(obj, index, value) { |
| 92 return checkAndCall(obj.set, obj, [index, value], '[]='); | 97 return checkAndCall(obj.set, obj, [index, value], '[]='); |
| 93 } | 98 } |
| 94 dart.dsetindex = dsetindex; | 99 dart.dsetindex = dsetindex; |
| 95 | 100 |
| 96 function cast(obj, type) { | 101 function cast(obj, type) { |
| 97 // TODO(vsm): handle non-nullable types | 102 // TODO(vsm): handle non-nullable types |
| 98 if (obj == null) return obj; | 103 if (obj == null) return obj; |
| 99 let actual = getRuntimeType(obj); | 104 let actual = getRuntimeType(obj); |
| 100 if (isSubtype(actual, type)) return obj; | 105 if (isSubtype(actual, type)) return obj; |
| 101 throw new _js_helper.CastErrorImplementation(actual, type); | 106 throw new _js_helper.CastErrorImplementation(actual, type); |
| 102 } | 107 } |
| 103 dart.as = cast; | 108 dart.as = cast; |
| 104 | 109 |
| 110 |
| 111 // TODO(vsm): How should we encode the runtime type? |
| 112 let runtimeType = Symbol('runtimeType'); |
| 113 |
| 105 /** | 114 /** |
| 106 * Returns the runtime type of obj. This is the same as `obj.runtimeType` | 115 * Returns the runtime type of obj. This is the same as `obj.runtimeType` |
| 107 * but will not call an overridden getter. | 116 * but will not call an overridden getter. |
| 108 * | 117 * |
| 109 * Currently this will return null for non-Dart objects. | 118 * Currently this will return null for non-Dart objects. |
| 110 */ | 119 */ |
| 111 function getRuntimeType(obj) { | 120 function getRuntimeType(obj) { |
| 112 switch (typeof obj) { | 121 switch (typeof obj) { |
| 113 case "undefined": | 122 case "undefined": |
| 114 return core.Null; | 123 return core.Null; |
| 115 case "number": | 124 case "number": |
| 116 return Math.floor(obj) == obj ? core.int : core.double; | 125 return Math.floor(obj) == obj ? core.int : core.double; |
| 117 case "boolean": | 126 case "boolean": |
| 118 return core.bool; | 127 return core.bool; |
| 119 case "string": | 128 case "string": |
| 120 return core.String; | 129 return core.String; |
| 121 case "symbol": | 130 case "symbol": |
| 122 return Symbol; | 131 return Symbol; |
| 123 } | 132 } |
| 124 // Undefined is handled above. For historical reasons, | 133 // Undefined is handled above. For historical reasons, |
| 125 // typeof null == "object" in JS. | 134 // typeof null == "object" in JS. |
| 126 if (obj === null) return core.Null; | 135 if (obj === null) return core.Null; |
| 127 // TODO(vsm): Should we treat Dart and JS objects differently here? | 136 // TODO(vsm): Should we treat Dart and JS objects differently here? |
| 128 // E.g., we can check if obj instanceof core.Object to differentiate. | 137 // E.g., we can check if obj instanceof core.Object to differentiate. |
| 129 var result = obj[dart.runtimeType]; | 138 var result = obj[runtimeType]; |
| 130 if (result) return result; | 139 if (result) return result; |
| 131 result = obj.constructor; | 140 result = obj.constructor; |
| 132 if (result == Function) { | 141 if (result == Function) { |
| 133 return getFunctionType(obj); | 142 return getFunctionType(obj); |
| 134 } | 143 } |
| 135 return result; | 144 return result; |
| 136 } | 145 } |
| 137 dart.getRuntimeType = getRuntimeType; | 146 dart.getRuntimeType = getRuntimeType; |
| 138 | 147 |
| 139 function instanceOf(obj, type) { | 148 function instanceOf(obj, type) { |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 } | 768 } |
| 760 map.set(arg, value); | 769 map.set(arg, value); |
| 761 } | 770 } |
| 762 } | 771 } |
| 763 return value; | 772 return value; |
| 764 } | 773 } |
| 765 return makeGenericType; | 774 return makeGenericType; |
| 766 } | 775 } |
| 767 dart.generic = generic; | 776 dart.generic = generic; |
| 768 | 777 |
| 778 /** Sets the runtime type of `obj` to be `type` */ |
| 779 function setType(obj, type) { |
| 780 obj[runtimeType] = type; |
| 781 } |
| 782 dart.setType = setType; |
| 783 |
| 769 // TODO(jmesserly): right now this is a sentinel. It should be a type object | 784 // TODO(jmesserly): right now this is a sentinel. It should be a type object |
| 770 // of some sort, assuming we keep around `dynamic` at runtime. | 785 // of some sort, assuming we keep around `dynamic` at runtime. |
| 771 dart.dynamic = { toString() { return 'dynamic'; } }; | 786 dart.dynamic = { toString() { return 'dynamic'; } }; |
| 772 dart.void = { toString() { return 'void'; } }; | 787 dart.void = { toString() { return 'void'; } }; |
| 773 dart.bottom = { toString() { return 'bottom'; } }; | 788 dart.bottom = { toString() { return 'bottom'; } }; |
| 774 | 789 |
| 775 // TODO(vsm): How should we encode the runtime type? | |
| 776 dart.runtimeType = Symbol('runtimeType'); | |
| 777 | |
| 778 dart.JsSymbol = Symbol; | 790 dart.JsSymbol = Symbol; |
| 779 | 791 |
| 780 // TODO(jmesserly): hack to bootstrap the SDK | 792 // TODO(jmesserly): hack to bootstrap the SDK |
| 781 _js_helper = _js_helper || {}; | 793 _js_helper = _js_helper || {}; |
| 782 _js_helper.checkNum = notNull; | 794 _js_helper.checkNum = notNull; |
| 783 | 795 |
| 784 })(dart || (dart = {})); | 796 })(dart || (dart = {})); |
| OLD | NEW |