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 library dart._utils; | 5 library dart._utils; |
6 | 6 |
7 import 'dart:_foreign_helper' show JS; | 7 import 'dart:_foreign_helper' show JS; |
8 | 8 |
9 /// This library defines a set of general javascript utilities for us | 9 /// This library defines a set of general javascript utilities for us |
10 /// by the Dart runtime. | 10 /// by the Dart runtime. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 90 } |
91 })(#, #)''', to, from); | 91 })(#, #)''', to, from); |
92 | 92 |
93 defineMemoizedGetter(obj, String name, getter) => | 93 defineMemoizedGetter(obj, String name, getter) => |
94 JS('', '''((obj, name, getter) => { | 94 JS('', '''((obj, name, getter) => { |
95 return defineLazyProperty(obj, name, {get: getter}); | 95 return defineLazyProperty(obj, name, {get: getter}); |
96 })(#, #, #)''', obj, name, getter); | 96 })(#, #, #)''', obj, name, getter); |
97 | 97 |
98 copyTheseProperties(to, from, names) => JS('', '''((to, from, names) => { | 98 copyTheseProperties(to, from, names) => JS('', '''((to, from, names) => { |
99 for (let name of names) { | 99 for (let name of names) { |
100 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); | 100 var desc = getOwnPropertyDescriptor(from, name); |
| 101 if (desc != void 0) { |
| 102 defineProperty(to, name, desc); |
| 103 } else { |
| 104 defineLazyProperty(to, name, () => from[name]); |
| 105 } |
101 } | 106 } |
102 return to; | 107 return to; |
103 })(#, #, #)''', to, from, names); | 108 })(#, #, #)''', to, from, names); |
104 | 109 |
105 /// Copy properties from source to destination object. | 110 /// Copy properties from source to destination object. |
106 /// This operation is commonly called `mixin` in JS. | 111 /// This operation is commonly called `mixin` in JS. |
107 copyProperties(to, from) => JS('', '''((to, from) => { | 112 copyProperties(to, from) => JS('', '''((to, from) => { |
108 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); | 113 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); |
109 })(#, #)''', to, from); | 114 })(#, #)''', to, from); |
110 | 115 |
111 /// Exports from one Dart module to another. | 116 /// Exports from one Dart module to another. |
112 // TODO(ochafik): Re-introduce a @JS annotation in the SDK (same as package:js) | 117 // TODO(ochafik): Re-introduce a @JS annotation in the SDK (same as package:js) |
113 // so that this is named 'export' in JavaScript. | 118 // so that this is named 'export' in JavaScript. |
114 export_(to, from, show, hide) => JS('', '''((to, from, show, hide) => { | 119 export_(to, from, show, hide) => JS('', '''((to, from, show, hide) => { |
115 if (show == void 0) { | 120 if (show == void 0 || show.length == 0) { |
116 show = getOwnNamesAndSymbols(from); | 121 show = getOwnNamesAndSymbols(from); |
117 } | 122 } |
118 if (hide != void 0) { | 123 if (hide != void 0) { |
119 var hideMap = new Set(hide); | 124 var hideMap = new Set(hide); |
120 show = show.filter((k) => !hideMap.has(k)); | 125 show = show.filter((k) => !hideMap.has(k)); |
121 } | 126 } |
122 return copyTheseProperties(to, from, show); | 127 return copyTheseProperties(to, from, show); |
123 })(#, #, #, #)''', to, from, show, hide); | 128 })(#, #, #, #)''', to, from, show, hide); |
OLD | NEW |