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 part of dart._runtime; | 4 part of dart._runtime; |
5 | 5 |
6 /// This library defines a set of general javascript utilities for us | 6 /// This library defines a set of general javascript utilities for us |
7 /// by the Dart runtime. | 7 /// by the Dart runtime. |
8 // TODO(ochafik): Rewrite some of these in Dart when possible. | 8 // TODO(ochafik): Rewrite some of these in Dart when possible. |
9 | 9 |
10 defineProperty(obj, name, desc) => | 10 defineProperty(obj, name, desc) => |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 safeGetOwnProperty(obj, String name) { | 43 safeGetOwnProperty(obj, String name) { |
44 var desc = getOwnPropertyDescriptor(obj, name); | 44 var desc = getOwnPropertyDescriptor(obj, name); |
45 if (desc != null) return JS('', '#.value', desc); | 45 if (desc != null) return JS('', '#.value', desc); |
46 } | 46 } |
47 | 47 |
48 /// Defines a lazy property. | 48 /// Defines a lazy property. |
49 /// After initial get or set, it will replace itself with a value property. | 49 /// After initial get or set, it will replace itself with a value property. |
50 // TODO(jmesserly): reusing descriptor objects has been shown to improve | 50 // TODO(jmesserly): reusing descriptor objects has been shown to improve |
51 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). | 51 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). |
52 defineLazyProperty(to, name, desc) => JS('', '''(() => { | 52 defineLazyProperty(to, name, desc) => JS( |
| 53 '', |
| 54 '''(() => { |
53 let init = $desc.get; | 55 let init = $desc.get; |
54 let value = null; | 56 let value = null; |
55 | 57 |
56 function lazySetter(x) { | 58 function lazySetter(x) { |
57 init = null; | 59 init = null; |
58 value = x; | 60 value = x; |
59 } | 61 } |
60 function circularInitError() { | 62 function circularInitError() { |
61 $throwInternalError('circular initialization for field ' + $name); | 63 $throwInternalError('circular initialization for field ' + $name); |
62 } | 64 } |
63 function lazyGetter() { | 65 function lazyGetter() { |
64 if (init == null) return value; | 66 if (init == null) return value; |
65 | 67 |
66 // Compute and store the value, guarding against reentry. | 68 // Compute and store the value, guarding against reentry. |
67 let f = init; | 69 let f = init; |
68 init = circularInitError; | 70 init = circularInitError; |
69 lazySetter(f()); | 71 lazySetter(f()); |
70 return value; | 72 return value; |
71 } | 73 } |
72 $desc.get = lazyGetter; | 74 $desc.get = lazyGetter; |
73 $desc.configurable = true; | 75 $desc.configurable = true; |
74 if ($desc.set) $desc.set = lazySetter; | 76 if ($desc.set) $desc.set = lazySetter; |
75 return $defineProperty($to, $name, $desc); | 77 return $defineProperty($to, $name, $desc); |
76 })()'''); | 78 })()'''); |
77 | 79 |
78 void defineLazy(to, from) => JS('', '''(() => { | 80 void defineLazy(to, from) => JS( |
| 81 '', |
| 82 '''(() => { |
79 for (let name of $getOwnNamesAndSymbols($from)) { | 83 for (let name of $getOwnNamesAndSymbols($from)) { |
80 $defineLazyProperty($to, name, $getOwnPropertyDescriptor($from, name)); | 84 $defineLazyProperty($to, name, $getOwnPropertyDescriptor($from, name)); |
81 } | 85 } |
82 })()'''); | 86 })()'''); |
83 | 87 |
84 defineMemoizedGetter(obj, String name, getter) { | 88 defineMemoizedGetter(obj, String name, getter) { |
85 return defineLazyProperty(obj, name, JS('', '{get: #}', getter)); | 89 return defineLazyProperty(obj, name, JS('', '{get: #}', getter)); |
86 } | 90 } |
87 | 91 |
88 copyTheseProperties(to, from, names) => JS('', '''(() => { | 92 copyTheseProperties(to, from, names) => JS( |
| 93 '', |
| 94 '''(() => { |
89 for (let i = 0; i < $names.length; ++i) { | 95 for (let i = 0; i < $names.length; ++i) { |
90 $copyProperty($to, $from, $names[i]); | 96 $copyProperty($to, $from, $names[i]); |
91 } | 97 } |
92 return $to; | 98 return $to; |
93 })()'''); | 99 })()'''); |
94 | 100 |
95 copyProperty(to, from, name) { | 101 copyProperty(to, from, name) { |
96 var desc = getOwnPropertyDescriptor(from, name); | 102 var desc = getOwnPropertyDescriptor(from, name); |
97 if (JS('bool', '# == Symbol.iterator', name)) { | 103 if (JS('bool', '# == Symbol.iterator', name)) { |
98 // On native types, Symbol.iterator may already be present. | 104 // On native types, Symbol.iterator may already be present. |
(...skipping 12 matching lines...) Expand all Loading... |
111 } | 117 } |
112 | 118 |
113 @JSExportName('export') | 119 @JSExportName('export') |
114 exportProperty(to, from, name) => copyProperty(to, from, name); | 120 exportProperty(to, from, name) => copyProperty(to, from, name); |
115 | 121 |
116 /// Copy properties from source to destination object. | 122 /// Copy properties from source to destination object. |
117 /// This operation is commonly called `mixin` in JS. | 123 /// This operation is commonly called `mixin` in JS. |
118 copyProperties(to, from) { | 124 copyProperties(to, from) { |
119 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); | 125 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); |
120 } | 126 } |
OLD | NEW |