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 /// This library defines the operations that define and manipulate Dart | 5 /// This library defines the operations that define and manipulate Dart |
6 /// classes. Included in this are: | 6 /// classes. Included in this are: |
7 /// - Generics | 7 /// - Generics |
8 /// - Class metadata | 8 /// - Class metadata |
9 /// - Extension methods | 9 /// - Extension methods |
10 /// | 10 /// |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 /// Get the type of a method from an object using the stored signature | 147 /// Get the type of a method from an object using the stored signature |
148 getMethodType(obj, name) => JS('', '''(() => { | 148 getMethodType(obj, name) => JS('', '''(() => { |
149 let type = $obj == null ? $Object : $obj.__proto__.constructor; | 149 let type = $obj == null ? $Object : $obj.__proto__.constructor; |
150 return $getMethodTypeFromType(type, $name); | 150 return $getMethodTypeFromType(type, $name); |
151 })()'''); | 151 })()'''); |
152 | 152 |
153 /// Get the type of a method from a type using the stored signature | 153 /// Get the type of a method from a type using the stored signature |
154 getMethodTypeFromType(type, name) => JS('', '''(() => { | 154 getMethodTypeFromType(type, name) => JS('', '''(() => { |
155 let sigObj = $type[$_methodSig]; | 155 let sigObj = $type[$_methodSig]; |
156 if (sigObj === void 0) return void 0; | 156 if (sigObj === void 0) return void 0; |
157 let parts = sigObj[$name]; | 157 return sigObj[$name]; |
158 if (parts === void 0) return void 0; | |
159 return $definiteFunctionType.apply(null, parts); | |
160 })()'''); | 158 })()'''); |
161 | 159 |
162 /// Get the type of a constructor from a class using the stored signature | 160 /// Get the type of a constructor from a class using the stored signature |
163 /// If name is undefined, returns the type of the default constructor | 161 /// If name is undefined, returns the type of the default constructor |
164 /// Returns undefined if the constructor is not found. | 162 /// Returns undefined if the constructor is not found. |
165 classGetConstructorType(cls, name) => JS('', '''(() => { | 163 classGetConstructorType(cls, name) => JS('', '''(() => { |
166 if(!$name) $name = $cls.name; | 164 if(!$name) $name = $cls.name; |
167 if ($cls === void 0) return void 0; | 165 if ($cls === void 0) return void 0; |
168 if ($cls == null) return void 0; | 166 if ($cls == null) return void 0; |
169 let sigCtor = $cls[$_constructorSig]; | 167 let sigCtor = $cls[$_constructorSig]; |
170 if (sigCtor === void 0) return void 0; | 168 if (sigCtor === void 0) return void 0; |
171 let parts = sigCtor[$name]; | 169 return sigCtor[$name]; |
172 if (parts === void 0) return void 0; | |
173 return $definiteFunctionType.apply(null, parts); | |
174 })()'''); | 170 })()'''); |
175 | 171 |
176 /// Given an object and a method name, tear off the method. | 172 /// Given an object and a method name, tear off the method. |
177 /// Sets the runtime type of the torn off method appropriately, | 173 /// Sets the runtime type of the torn off method appropriately, |
178 /// and also binds the object. | 174 /// and also binds the object. |
179 /// | 175 /// |
180 /// If the optional `f` argument is passed in, it will be used as the method. | 176 /// If the optional `f` argument is passed in, it will be used as the method. |
181 /// This supports cases like `super.foo` where we need to tear off the method | 177 /// This supports cases like `super.foo` where we need to tear off the method |
182 /// from the superclass, not from the `obj` directly. | 178 /// from the superclass, not from the `obj` directly. |
183 /// TODO(leafp): Consider caching the tearoff on the object? | 179 /// TODO(leafp): Consider caching the tearoff on the object? |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // Set up the static signature field on the constructor | 215 // Set up the static signature field on the constructor |
220 _setStaticSignature(f, sigF) => | 216 _setStaticSignature(f, sigF) => |
221 JS('', '$defineMemoizedGetter($f, $_staticSig, $sigF)'); | 217 JS('', '$defineMemoizedGetter($f, $_staticSig, $sigF)'); |
222 | 218 |
223 // Set the lazily computed runtime type field on static methods | 219 // Set the lazily computed runtime type field on static methods |
224 _setStaticTypes(f, names) => JS('', '''(() => { | 220 _setStaticTypes(f, names) => JS('', '''(() => { |
225 for (let name of $names) { | 221 for (let name of $names) { |
226 // TODO(vsm): Need to generate static methods. | 222 // TODO(vsm): Need to generate static methods. |
227 if (!$f[name]) continue; | 223 if (!$f[name]) continue; |
228 $tagLazy($f[name], function() { | 224 $tagLazy($f[name], function() { |
229 let parts = $f[$_staticSig][name]; | 225 return $f[$_staticSig][name]; |
230 return $definiteFunctionType.apply(null, parts); | |
231 }) | 226 }) |
232 } | 227 } |
233 })()'''); | 228 })()'''); |
234 | 229 |
235 /// Set up the type signature of a class (constructor object) | 230 /// Set up the type signature of a class (constructor object) |
236 /// f is a constructor object | 231 /// f is a constructor object |
237 /// signature is an object containing optional properties as follows: | 232 /// signature is an object containing optional properties as follows: |
238 /// methods: A function returning an object mapping method names | 233 /// methods: A function returning an object mapping method names |
239 /// to method types. The function is evaluated lazily and cached. | 234 /// to method types. The function is evaluated lazily and cached. |
240 /// statics: A function returning an object mapping static method | 235 /// statics: A function returning an object mapping static method |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 setBaseClass(derived, base) { | 428 setBaseClass(derived, base) { |
434 JS('', '#.prototype.__proto__ = #.prototype', derived, base); | 429 JS('', '#.prototype.__proto__ = #.prototype', derived, base); |
435 } | 430 } |
436 | 431 |
437 /// Like [setBaseClass] but for generic extension types, e.g. `JSArray<E>` | 432 /// Like [setBaseClass] but for generic extension types, e.g. `JSArray<E>` |
438 setExtensionBaseClass(derived, base) { | 433 setExtensionBaseClass(derived, base) { |
439 // Mark the generic type as an extension type. | 434 // Mark the generic type as an extension type. |
440 JS('', '#.prototype[#] = #', derived, _extensionType, derived); | 435 JS('', '#.prototype[#] = #', derived, _extensionType, derived); |
441 setBaseClass(derived, base); | 436 setBaseClass(derived, base); |
442 } | 437 } |
OLD | NEW |