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 a set of general javascript utilities for us | 5 /* This library defines a set of general javascript utilities for us |
6 * by the Dart runtime. | 6 * by the Dart runtime. |
7 */ | 7 */ |
8 | 8 |
9 var dart_utils = | 9 var dart_utils = |
10 typeof module != "undefined" && module.exports || {}; | 10 typeof module != "undefined" && module.exports || {}; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 function safeGetOwnProperty(obj, name) { | 42 function safeGetOwnProperty(obj, name) { |
43 let desc = getOwnPropertyDescriptor(obj, name); | 43 let desc = getOwnPropertyDescriptor(obj, name); |
44 if (desc) return desc.value; | 44 if (desc) return desc.value; |
45 } | 45 } |
46 dart_utils.safeGetOwnProperty = safeGetOwnProperty; | 46 dart_utils.safeGetOwnProperty = safeGetOwnProperty; |
47 | 47 |
48 /** | 48 /** |
49 * Defines a lazy property. | 49 * Defines a lazy property. |
50 * After initial get or set, it will replace itself with a value property. | 50 * After initial get or set, it will replace itself with a value property. |
51 */ | 51 */ |
52 // TODO(jmesserly): is this the best implementation for JS engines? | |
53 // TODO(jmesserly): reusing descriptor objects has been shown to improve | 52 // TODO(jmesserly): reusing descriptor objects has been shown to improve |
54 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). | 53 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill). |
55 function defineLazyProperty(to, name, desc) { | 54 function defineLazyProperty(to, name, desc) { |
56 let init = desc.get; | 55 let init = desc.get; |
57 let writable = !!desc.set; | 56 let value = null; |
58 function lazySetter(value) { | 57 |
59 defineProperty(to, name, { value: value, writable: writable }); | 58 function lazySetter(x) { |
| 59 init = null; |
| 60 value = x; |
| 61 } |
| 62 function circularInitError() { |
| 63 throwError('circular initialization for field ' + name); |
60 } | 64 } |
61 function lazyGetter() { | 65 function lazyGetter() { |
62 // Clear the init function to detect circular initialization. | 66 if (init == null) return value; |
| 67 |
| 68 // Compute and store the value, guarding against reentry. |
63 let f = init; | 69 let f = init; |
64 if (f === null) { | 70 init = circularInitError; |
65 throwError('circular initialization for field ' + name); | 71 lazySetter(f()); |
66 } | |
67 init = null; | |
68 | |
69 // Compute and store the value. | |
70 let value = f(); | |
71 lazySetter(value); | |
72 return value; | 72 return value; |
73 } | 73 } |
74 desc.get = lazyGetter; | 74 desc.get = lazyGetter; |
75 desc.configurable = true; | 75 desc.configurable = true; |
76 if (writable) desc.set = lazySetter; | 76 if (desc.set) desc.set = lazySetter; |
77 defineProperty(to, name, desc); | 77 return defineProperty(to, name, desc); |
78 } | 78 } |
79 dart_utils.defineLazyProperty = defineLazyProperty; | 79 dart_utils.defineLazyProperty = defineLazyProperty; |
80 | 80 |
81 function defineLazy(to, from) { | 81 function defineLazy(to, from) { |
82 for (let name of getOwnNamesAndSymbols(from)) { | 82 for (let name of getOwnNamesAndSymbols(from)) { |
83 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); | 83 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name)); |
84 } | 84 } |
85 } | 85 } |
86 dart_utils.defineLazy = defineLazy; | 86 dart_utils.defineLazy = defineLazy; |
87 | 87 |
88 function defineMemoizedGetter(obj, name, get) { | 88 function defineMemoizedGetter(obj, name, getter) { |
89 let cache = null; | 89 return defineLazyProperty(obj, name, {get: getter}); |
90 function getter() { | |
91 if (cache != null) return cache; | |
92 cache = get(); | |
93 get = null; | |
94 return cache; | |
95 } | |
96 defineProperty(obj, name, {get: getter, configurable: true}); | |
97 } | 90 } |
98 dart_utils.defineMemoizedGetter = defineMemoizedGetter; | 91 dart_utils.defineMemoizedGetter = defineMemoizedGetter; |
99 | 92 |
100 function copyTheseProperties(to, from, names) { | 93 function copyTheseProperties(to, from, names) { |
101 for (let name of names) { | 94 for (let name of names) { |
102 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); | 95 defineProperty(to, name, getOwnPropertyDescriptor(from, name)); |
103 } | 96 } |
104 return to; | 97 return to; |
105 } | 98 } |
106 dart_utils.copyTheseProperties = copyTheseProperties; | 99 dart_utils.copyTheseProperties = copyTheseProperties; |
107 | 100 |
108 /** | 101 /** |
109 * Copy properties from source to destination object. | 102 * Copy properties from source to destination object. |
110 * This operation is commonly called `mixin` in JS. | 103 * This operation is commonly called `mixin` in JS. |
111 */ | 104 */ |
112 function copyProperties(to, from) { | 105 function copyProperties(to, from) { |
113 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); | 106 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from)); |
114 } | 107 } |
115 dart_utils.copyProperties = copyProperties; | 108 dart_utils.copyProperties = copyProperties; |
116 | 109 |
117 | 110 /** Exports from one Dart module to another. */ |
118 function instantiate(type, args) { | 111 function export_(to, from, show, hide) { |
119 return new type(...args); | 112 if (show == void 0) { |
| 113 show = getOwnNamesAndSymbols(from); |
| 114 } |
| 115 if (hide != void 0) { |
| 116 var hideMap = new Map(hide); |
| 117 show = show.filter((k) => !hideMap.has(k)); |
| 118 } |
| 119 return copyTheseProperties(to, from, show); |
120 } | 120 } |
121 dart_utils.instantiate = instantiate; | 121 dart_utils.export = export_; |
122 | 122 |
123 })(dart_utils); | 123 })(dart_utils); |
OLD | NEW |