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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 ('names' in signature) ? signature.names : []; | 249 ('names' in signature) ? signature.names : []; |
250 $_setConstructorSignature($f, constructors); | 250 $_setConstructorSignature($f, constructors); |
251 $_setMethodSignature($f, methods); | 251 $_setMethodSignature($f, methods); |
252 $_setStaticSignature($f, statics); | 252 $_setStaticSignature($f, statics); |
253 $_setStaticTypes($f, names); | 253 $_setStaticTypes($f, names); |
254 $tagLazy($f, () => $Type); | 254 $tagLazy($f, () => $Type); |
255 })()'''); | 255 })()'''); |
256 | 256 |
257 hasMethod(obj, name) => JS('', '$getMethodType($obj, $name) !== void 0'); | 257 hasMethod(obj, name) => JS('', '$getMethodType($obj, $name) !== void 0'); |
258 | 258 |
| 259 /// Given a class and an initializer method name, creates a constructor |
| 260 /// function with the same name. |
259 /// | 261 /// |
260 /// Given a class and an initializer method name, creates a constructor | 262 /// After we define the named constructor, the class can be constructed with |
261 /// function with the same name. For example `new SomeClass.name(args)`. | 263 /// `new SomeClass.name(args)`. |
262 /// | |
263 defineNamedConstructor(clazz, name) => JS('', '''(() => { | 264 defineNamedConstructor(clazz, name) => JS('', '''(() => { |
264 let proto = $clazz.prototype; | 265 let proto = $clazz.prototype; |
265 let initMethod = proto[$name]; | 266 let initMethod = proto[$name]; |
266 let ctor = function() { return initMethod.apply(this, arguments); }; | 267 let ctor = function(...args) { initMethod.apply(this, args); }; |
267 ctor.prototype = proto; | 268 ctor.prototype = proto; |
268 // Use defineProperty so we don't hit a property defined on Function, | 269 // Use defineProperty so we don't hit a property defined on Function, |
269 // like `caller` and `arguments`. | 270 // like `caller` and `arguments`. |
270 $defineProperty($clazz, $name, { value: ctor, configurable: true }); | 271 $defineProperty($clazz, $name, { value: ctor, configurable: true }); |
271 })()'''); | 272 })()'''); |
272 | 273 |
| 274 |
273 final _extensionType = JS('', 'Symbol("extensionType")'); | 275 final _extensionType = JS('', 'Symbol("extensionType")'); |
274 | 276 |
275 getExtensionType(obj) => JS('', '#[#]', obj, _extensionType); | 277 getExtensionType(obj) => JS('', '#[#]', obj, _extensionType); |
276 | 278 |
277 final dartx = JS('', 'dartx'); | 279 final dartx = JS('', 'dartx'); |
278 | 280 |
279 getExtensionSymbol(name) { | 281 getExtensionSymbol(name) { |
280 var sym = JS('', 'dartx[#]', name); | 282 var sym = JS('', 'dartx[#]', name); |
281 if (sym == null) { | 283 if (sym == null) { |
282 sym = JS('', 'Symbol("dartx." + #.toString())', name); | 284 sym = JS('', 'Symbol("dartx." + #.toString())', name); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 JS('', '#.__proto__ = #', derived, base); | 433 JS('', '#.__proto__ = #', derived, base); |
432 } | 434 } |
433 | 435 |
434 /// Like [setBaseClass] but for generic extension types, e.g. `JSArray<E>` | 436 /// Like [setBaseClass] but for generic extension types, e.g. `JSArray<E>` |
435 setExtensionBaseClass(derived, base) { | 437 setExtensionBaseClass(derived, base) { |
436 // Mark the generic type as an extension type. | 438 // Mark the generic type as an extension type. |
437 JS('', '#.prototype[#] = #', derived, _extensionType, derived); | 439 JS('', '#.prototype[#] = #', derived, _extensionType, derived); |
438 // Link the prototype objects | 440 // Link the prototype objects |
439 JS('', '#.prototype.__proto__ = #.prototype', derived, base); | 441 JS('', '#.prototype.__proto__ = #.prototype', derived, base); |
440 } | 442 } |
| 443 |
| 444 /// Given a special constructor function that creates a function instances, |
| 445 /// and a class with a `call` method, merge them so the constructor function |
| 446 /// will have the correct methods and prototype. |
| 447 /// |
| 448 /// For example: |
| 449 /// |
| 450 /// lib.Foo = dart.callableClass( |
| 451 /// function Foo { function call(...args) { ... } ... return call; }, |
| 452 /// class Foo { call(x) { ... } }); |
| 453 /// ... |
| 454 /// let f = new lib.Foo(); |
| 455 /// f(42); |
| 456 callableClass(callableCtor, classExpr) { |
| 457 JS('', '#.prototype = #.prototype', callableCtor, classExpr); |
| 458 // We're not going to use the original class, so we can safely replace it to |
| 459 // point at this constructor for the runtime type information. |
| 460 JS('', '#.prototype.constructor = #', callableCtor, callableCtor); |
| 461 JS('', '#.__proto__ = #.__proto__', callableCtor, classExpr); |
| 462 return callableCtor; |
| 463 } |
| 464 |
| 465 /// Given a class and an initializer method name and a call method, creates a |
| 466 /// constructor function with the same name. |
| 467 /// |
| 468 /// For example it can be called with `new SomeClass.name(args)`. |
| 469 /// |
| 470 /// The constructor |
| 471 defineNamedConstructorCallable(clazz, name, ctor) => JS('', '''(() => { |
| 472 ctor.prototype = $clazz.prototype; |
| 473 // Use defineProperty so we don't hit a property defined on Function, |
| 474 // like `caller` and `arguments`. |
| 475 $defineProperty($clazz, $name, { value: ctor, configurable: true }); |
| 476 })()'''); |
OLD | NEW |