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 /// |
11 | 11 |
12 // TODO(leafp): Consider splitting some of this out. | 12 // TODO(leafp): Consider splitting some of this out. |
13 part of dart._runtime; | 13 part of dart._runtime; |
14 | 14 |
15 /// | 15 /// |
16 /// Returns a new type that mixes members from base and all mixins. | 16 /// Returns a new type that mixes members from base and all mixins. |
17 /// | 17 /// |
18 /// Each mixin applies in sequence, with further to the right ones overriding | 18 /// Each mixin applies in sequence, with further to the right ones overriding |
19 /// previous entries. | 19 /// previous entries. |
20 /// | 20 /// |
21 /// For each mixin, we only take its own properties, not anything from its | 21 /// For each mixin, we only take its own properties, not anything from its |
22 /// superclass (prototype). | 22 /// superclass (prototype). |
23 /// | 23 /// |
24 mixin(base, @rest mixins) => JS('', '''(() => { | 24 mixin(base, @rest mixins) => JS('', '''(() => { |
25 // Create an initializer for the mixin, so when derived constructor calls | 25 // Create an initializer for the mixin, so when derived constructor calls |
26 // super, we can correctly initialize base and mixins. | 26 // super, we can correctly initialize base and mixins. |
27 | 27 |
28 // Create a class that will hold all of the mixin methods. | 28 // Create a class that will hold all of the mixin methods. |
29 class Mixin extends $base { | 29 class Mixin extends $base {} |
30 // Initializer method: run mixin initializers, then the base. | |
31 [$base.name](...args) { | |
32 // Run mixin initializers. They cannot have arguments. | |
33 // Run them backwards so most-derived mixin is initialized first. | |
34 for (let i = $mixins.length - 1; i >= 0; i--) { | |
35 let mixin = $mixins[i]; | |
36 let init = mixin.prototype[mixin.name]; | |
37 if (init) init.call(this); | |
38 } | |
39 // Run base initializer. | |
40 let init = $base.prototype[base.name]; | |
41 if (init) init.apply(this, args); | |
42 } | |
43 } | |
44 // Copy each mixin's methods, with later ones overwriting earlier entries. | 30 // Copy each mixin's methods, with later ones overwriting earlier entries. |
45 for (let m of $mixins) { | 31 for (let m of $mixins) { |
46 $copyProperties(Mixin.prototype, m.prototype); | 32 $copyProperties(Mixin.prototype, m.prototype); |
47 } | 33 } |
| 34 // Initializer method: run mixin initializers, then the base. |
| 35 Mixin.prototype.new = function(...args) { |
| 36 // Run mixin initializers. They cannot have arguments. |
| 37 // Run them backwards so most-derived mixin is initialized first. |
| 38 for (let i = $mixins.length - 1; i >= 0; i--) { |
| 39 $mixins[i].prototype.new.call(this); |
| 40 } |
| 41 // Run base initializer. |
| 42 $base.prototype.new.apply(this, args); |
| 43 }; |
48 | 44 |
49 // Set the signature of the Mixin class to be the composition | 45 // Set the signature of the Mixin class to be the composition |
50 // of the signatures of the mixins. | 46 // of the signatures of the mixins. |
51 $setSignature(Mixin, { | 47 $setSignature(Mixin, { |
52 methods: () => { | 48 methods: () => { |
53 let s = {}; | 49 let s = {}; |
54 for (let m of $mixins) { | 50 for (let m of $mixins) { |
55 $copyProperties(s, m[$_methodSig]); | 51 $copyProperties(s, m[$_methodSig]); |
56 } | 52 } |
57 return s; | 53 return s; |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 })()'''); | 417 })()'''); |
422 | 418 |
423 /// Sets the element type of a list literal. | 419 /// Sets the element type of a list literal. |
424 list(obj, elementType) => | 420 list(obj, elementType) => |
425 JS('', '$setType($obj, ${getGenericClass(JSArray)}($elementType))'); | 421 JS('', '$setType($obj, ${getGenericClass(JSArray)}($elementType))'); |
426 | 422 |
427 setBaseClass(derived, base) => JS('', '''(() => { | 423 setBaseClass(derived, base) => JS('', '''(() => { |
428 // Link the extension to the type it's extending as a base class. | 424 // Link the extension to the type it's extending as a base class. |
429 $derived.prototype.__proto__ = $base.prototype; | 425 $derived.prototype.__proto__ = $base.prototype; |
430 })()'''); | 426 })()'''); |
OLD | NEW |