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 association between runtime objects and | 5 /// This library defines the association between runtime objects and |
6 /// runtime types. | 6 /// runtime types. |
7 part of dart._runtime; | 7 part of dart._runtime; |
8 | 8 |
9 /// Runtime type information. This module defines the mapping from | 9 /// Runtime type information. This module defines the mapping from |
10 /// runtime objects to their runtime type information. See the types | 10 /// runtime objects to their runtime type information. See the types |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 return JS('', '#', jsobject); | 144 return JS('', '#', jsobject); |
145 } | 145 } |
146 if (result == null) { | 146 if (result == null) { |
147 return JS('', '#', jsobject); | 147 return JS('', '#', jsobject); |
148 } | 148 } |
149 return result; | 149 return result; |
150 } | 150 } |
151 | 151 |
152 /// Given an internal runtime type object, wraps it in a `WrappedType` object | 152 /// Given an internal runtime type object, wraps it in a `WrappedType` object |
153 /// that implements the dart:core Type interface. | 153 /// that implements the dart:core Type interface. |
154 wrapType(type) { | 154 Type wrapType(type) { |
155 // If we've already wrapped this type once, use the previous wrapper. This | 155 // If we've already wrapped this type once, use the previous wrapper. This |
156 // way, multiple references to the same type return an identical Type. | 156 // way, multiple references to the same type return an identical Type. |
157 if (JS('bool', '#.hasOwnProperty(#)', type, _typeObject)) { | 157 if (JS('bool', '#.hasOwnProperty(#)', type, _typeObject)) { |
158 return JS('', '#[#]', type, _typeObject); | 158 return JS('', '#[#]', type, _typeObject); |
159 } | 159 } |
160 return JS('', '#[#] = #', type, _typeObject, new WrappedType(type)); | 160 return JS('Type', '#[#] = #', type, _typeObject, new WrappedType(type)); |
161 } | |
162 | |
163 var _lazyJSTypes = JS('', 'new Map()'); | |
164 | |
165 lazyJSType(getJSTypeCallback, name) { | |
166 var key = JS('String', '#.toString()', getJSTypeCallback); | |
167 if (JS('bool', '#.has(#)', _lazyJSTypes, key)) { | |
168 return JS('', '#.get(#)', _lazyJSTypes, key); | |
169 } | |
170 var ret = new LazyJSType(getJSTypeCallback, name); | |
171 JS('', '#.set(#, #)', _lazyJSTypes, key, ret); | |
172 return ret; | |
173 } | |
174 | |
175 // TODO(jacobr): do not use the same LazyJSType object for anonymous JS types | |
176 // from different libraries. | |
177 lazyAnonymousJSType(name) { | |
178 if (JS('bool', '#.has(#)', _lazyJSTypes, name)) { | |
179 return JS('', '#.get(#)', _lazyJSTypes, name); | |
180 } | |
181 var ret = new LazyJSType(null, name); | |
182 JS('', '#.set(#, #)', _lazyJSTypes, name, ret); | |
183 return ret; | |
184 } | 161 } |
185 | 162 |
186 /// Given a WrappedType, return the internal runtime type object. | 163 /// Given a WrappedType, return the internal runtime type object. |
187 unwrapType(WrappedType obj) => obj._wrappedType; | 164 unwrapType(WrappedType obj) => obj._wrappedType; |
188 | 165 |
189 _getRuntimeType(value) => JS('', '#[#]', value, _runtimeType); | 166 _getRuntimeType(value) => JS('', '#[#]', value, _runtimeType); |
190 | 167 |
191 /// Return the module name for a raw library object. | 168 /// Return the module name for a raw library object. |
192 getModuleName(value) => JS('', '#[#]', value, _moduleName); | 169 getModuleName(value) => JS('', '#[#]', value, _moduleName); |
193 | 170 |
194 /// Tag the runtime type of [value] to be type [t]. | 171 /// Tag the runtime type of [value] to be type [t]. |
195 void tag(value, t) { | 172 void tag(value, t) { |
196 JS('', '#[#] = #', value, _runtimeType, t); | 173 JS('', '#[#] = #', value, _runtimeType, t); |
197 } | 174 } |
198 | 175 |
199 void tagComputed(value, compute) { | 176 void tagComputed(value, compute) { |
200 JS('', '#(#, #, { get: # })', defineProperty, value, _runtimeType, compute); | 177 defineGetter(value, _runtimeType, compute); |
201 } | 178 } |
202 | 179 |
203 void tagLazy(value, compute) { | 180 void tagLazy(value, compute) { |
204 JS('', '#(#, #, { get: # })', defineLazyProperty, value, _runtimeType, | 181 defineMemoizedGetter(value, _runtimeType, compute); |
205 compute); | |
206 } | 182 } |
207 | 183 |
208 var _loadedModules = JS('', 'new Map()'); | 184 var _loadedModules = JS('', 'new Map()'); |
209 var _loadedSourceMaps = JS('', 'new Map()'); | 185 var _loadedSourceMaps = JS('', 'new Map()'); |
210 | 186 |
211 List getModuleNames() { | 187 List getModuleNames() { |
212 return JS('', 'Array.from(#.keys())', _loadedModules); | 188 return JS('', 'Array.from(#.keys())', _loadedModules); |
213 } | 189 } |
214 | 190 |
215 String getSourceMap(module) { | 191 String getSourceMap(module) { |
216 return JS('String', '#.get(#)', _loadedSourceMaps, module); | 192 return JS('String', '#.get(#)', _loadedSourceMaps, module); |
217 } | 193 } |
218 | 194 |
219 /// Return all library objects in the specified module. | 195 /// Return all library objects in the specified module. |
220 getModuleLibraries(String name) { | 196 getModuleLibraries(String name) { |
221 var module = JS('', '#.get(#)', _loadedModules, name); | 197 var module = JS('', '#.get(#)', _loadedModules, name); |
222 if (module == null) return null; | 198 if (module == null) return null; |
223 JS('', '#[#] = #', module, _moduleName, name); | 199 JS('', '#[#] = #', module, _moduleName, name); |
224 return module; | 200 return module; |
225 } | 201 } |
226 | 202 |
227 /// Track all libraries | 203 /// Track all libraries |
228 void trackLibraries(String moduleName, libraries, sourceMap) { | 204 void trackLibraries(String moduleName, libraries, sourceMap) { |
229 JS('', '#.set(#, #)', _loadedSourceMaps, moduleName, sourceMap); | 205 JS('', '#.set(#, #)', _loadedSourceMaps, moduleName, sourceMap); |
230 JS('', '#.set(#, #)', _loadedModules, moduleName, libraries); | 206 JS('', '#.set(#, #)', _loadedModules, moduleName, libraries); |
231 } | 207 } |
OLD | NEW |